基于自定义尺寸字段的WooCommerce购物车商品价格自定义计算
实现WooCommerce基于height/width字段的自定义价格计算
嘿,我来帮你搞定这个自定义价格计算的需求!基于你已经设置好的height和width输入字段,我们可以通过几个WooCommerce钩子来实现动态价格计算,下面是具体步骤和代码:
1. 确保自定义字段值正确关联到购物车项
首先要确认用户输入的尺寸值能被作为元数据绑定到购物车项中(如果你的现有代码已经实现这一步,可以跳过,但还是贴出来供参考):
// 保存自定义尺寸字段到购物车项数据 add_filter('woocommerce_add_cart_item_data', 'save_custom_dimensions_to_cart', 10, 3); function save_custom_dimensions_to_cart($cart_item_data, $product_id, $variation_id) { // 捕获并清洗height字段值 if(isset($_POST['height']) && !empty($_POST['height'])) { $cart_item_data['custom_height'] = sanitize_text_field($_POST['height']); } // 捕获并清洗width字段值 if(isset($_POST['width']) && !empty($_POST['width'])) { $cart_item_data['custom_width'] = sanitize_text_field($_POST['width']); } // 添加唯一键,确保用户输入不同尺寸时生成独立购物车项 if(!empty($cart_item_data['custom_height']) || !empty($cart_item_data['custom_width'])) { $cart_item_data['unique_key'] = md5(microtime().rand()); } return $cart_item_data; }
2. 核心:自定义价格计算逻辑
这一步是实现动态定价的关键,我以「基础价格 × 高度 × 宽度」为例(你可以根据实际需求修改公式,比如按面积加价、固定单价×面积等):
// 根据自定义尺寸计算商品价格 add_action('woocommerce_before_calculate_totals', 'calculate_custom_dimension_price', 10, 1); function calculate_custom_dimension_price($cart) { // 后台环境跳过,避免干扰 if(is_admin() && !defined('DOING_AJAX')) return; // 遍历购物车中的每个商品项 foreach($cart->get_cart() as $cart_item_key => $cart_item) { // 检查是否存在有效的尺寸数据 if(isset($cart_item['custom_height']) && isset($cart_item['custom_width'])) { $height = (float)$cart_item['custom_height']; $width = (float)$cart_item['custom_width']; $product = $cart_item['data']; // 获取商品基础价格(可切换为get_regular_price()或get_sale_price()) $base_price = (float)$product->get_price(); // 自定义计算规则:这里示例为「基础价 × 高度 × 宽度」 $custom_price = $base_price * $height * $width; // 更新购物车项的价格 $product->set_price($custom_price); } } }
3. 可选:在购物车/结账页面显示价格计算明细
如果需要让用户清楚看到价格是怎么算出来的,可以添加这段代码,在购物车项下方显示尺寸和计算说明:
// 在购物车页面展示价格计算明细 add_filter('woocommerce_cart_item_name', 'display_custom_dimension_price_note', 10, 3); function display_custom_dimension_price_note($name, $cart_item, $cart_item_key) { if(isset($cart_item['custom_height']) && isset($cart_item['custom_width'])) { $height = $cart_item['custom_height']; $width = $cart_item['custom_width']; $name .= '<br><small>尺寸:height='.$height.' width='.$width.' | 价格计算:基础价 × '.$height.' × '.$width.'</small>'; } return $name; }
4. 保存数据到订单
最后要确保这些自定义尺寸和最终价格能同步保存到订单里,方便后续订单管理:
// 将自定义尺寸保存到订单元数据 add_action('woocommerce_checkout_create_order_line_item', 'save_dimensions_to_order_item', 10, 4); function save_dimensions_to_order_item($item, $cart_item_key, $cart_item, $order) { if(isset($cart_item['custom_height'])) { $item->add_meta_data('自定义高度', $cart_item['custom_height'], true); } if(isset($cart_item['custom_width'])) { $item->add_meta_data('自定义宽度', $cart_item['custom_width'], true); } }
小提示
- 代码可以添加到主题的
functions.php文件,或者用专门的代码片段插件(比如Code Snippets)来管理,避免主题更新丢失代码。 - 请根据你的实际定价规则修改
calculate_custom_dimension_price函数里的计算逻辑,比如改成「基础价 + (高度×宽度×单价)」这种模式。 - 如果你的自定义字段name不是
height和width,要对应替换代码里的$_POST['height']和$_POST['width']。
内容的提问来源于stack exchange,提问作者syner




