我正在创造尺寸和重量差异很大的产品。我没有使用 WooCommerce 产品变体来帮助我解决此问题,因为我的产品是高度定制的。我正在使用第三方插件从许多变体选项中进行选择,我计划检查这些选项以确定产品重量。
问题是我想在将产品添加到购物车之前设置产品重量。然后,购物车上的运费计算器将根据购物车的总重量(WooCommerce 基于重量的运费)计算运费。
我已经尝试了 2 个 WooCommerce 钩子(Hook)来完成此操作,但没有成功。
add_filter( 'woocommerce_add_cart_item_data', 'update_weight_on_add_to_cart', 10, 3);
和
add_action( 'woocommerce_add_to_cart', 'update_weight_on_add_to_cart', 10, 6 );
我可以使用 add_filter 设置自定义字段。这样做我可以看到我成功地将产品重量设置为新的值。但是一旦我进入购物车页面,重量就会恢复到编辑产品页面上设置的值。
这是我当前用来检查的函数:
function update_weight_on_add_to_cart( $cart_item_data, $product_id, $variation_id )
{
$product = wc_get_product( $product_id );
$weight1 = $product->get_weight();
$product->set_weight(3.45);
$weight2 = $product->get_weight();
$cart_item_data['weight1'] = $weight1;
$cart_item_data['weight2'] = $weight2;
return $cart_item_data;
}
然后,我在购物车上显示这些字段,以查看是否使用以下 Hook 和函数成功更改了数据。 (我从博客文章或其他 StackOverflow 帖子中获取了大部分内容)
add_filter( 'woocommerce_get_item_data', 'display_weight', 10, 2 );
function display_weight( $item_data, $cart_item ) {
$item_id = $cart_item['variation_id'];
if($item_id == 0) $item_id = $cart_item['product_id'];
$product_qty = $cart_item['quantity'];
$product = wc_get_product($item_id);
$weight3 = $product->get_weight();
$item_data[] = array(
'key' => __('Weight3', 'woocommerce'),
'value' => wc_clean( $weight3 ),
'display' => ''
);
$item_data[] = array(
'key' => __( 'Weight1', 'woocommerce' ),
'value' => wc_clean( $cart_item['weight1'] ),
'display' => ''
);
$item_data[] = array(
'key' => __( 'Weight2', 'woocommerce' ),
'value' => wc_clean( $cart_item['weight2'] ),
'display' => ''
);
return $item_data;
}
我最近使用上述代码的努力在购物车页面上产生了以下结果。
权重3: 1
权重1: 1
重量2: 3.45
如有任何帮助,我们将不胜感激!如果不能以这种方式完成,是否有更好的方法来解决这个问题?也许这需要在购物车上完成?
最佳答案
存在一些错误、错误和缺失部分...这是执行此操作的方法(适用于所有产品类型,包括产品变体):
// make and save a calculated weight as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_weight_custom_cart_item_data', 10, 3 );
function add_weight_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){
// For product variations handling
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// Get an instance of the product object
$product = wc_get_product( $product_id );
// The default product weight
$cart_item_data['weight']['default'] = $product->get_weight();
## ====> HERE YOU CAN MAKE YOUR WEIGHT CALCULATIONS <==== ##
$new_weight_value = 3.45;
// Set the new calculated weight
$cart_item_data['weight']['new'] = 3.45;
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_cart_item_weight', 10, 2 );
function display_cart_item_weight( $item_data, $cart_item ) {
if ( isset($cart_item['weight']) ) {
// Display original weight
if ( isset($cart_item['weight']['default']) ) {
$item_data[] = array(
'key' => __('Weight (original)', 'woocommerce'),
'value' => wc_format_weight( $cart_item['weight']['default'] ),
);
}
// Display calculated weight
if ( isset($cart_item['weight']['new']) ) {
$item_data[] = array(
'key' => __( 'Weight (new)', 'woocommerce' ),
'value' => wc_format_weight( $cart_item['weight']['new'] ),
);
}
}
return $item_data;
}
// Set the new weight in cart items
add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_weight', 25, 1 );
function set_custom_cart_item_weight( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach( $cart->get_cart() as $cart_item ){
if ( isset($cart_item['weight']['new']) ) {
$cart_item['data']->set_weight($cart_item['weight']['new']);
}
}
}
代码位于事件子主题(事件主题)的 function.php 文件中。经过测试并有效。
关于php - Woocommerce 中的自定义购物车商品重量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53401178/