Php Woocommerce中的自定义购物车项目重量
我创造的产品在大小和重量上都有很大的差异。我没有使用WooCommerce产品变体来帮助我,因为我的产品是大量定制的。我正在使用第三方插件从许多变体选项中进行选择,我计划检查这些选项以确定产品重量 问题是我想在将产品添加到购物车之前设置产品重量。然后,购物车上的装运计算器将根据购物车的总重量和基于重量的装运计算装运 我已经尝试了2个非常广泛的钩子来做这件事,但没有运气Php Woocommerce中的自定义购物车项目重量,php,wordpress,woocommerce,properties,cart,Php,Wordpress,Woocommerce,Properties,Cart,我创造的产品在大小和重量上都有很大的差异。我没有使用WooCommerce产品变体来帮助我,因为我的产品是大量定制的。我正在使用第三方插件从许多变体选项中进行选择,我计划检查这些选项以确定产品重量 问题是我想在将产品添加到购物车之前设置产品重量。然后,购物车上的装运计算器将根据购物车的总重量和基于重量的装运计算装运 我已经尝试了2个非常广泛的钩子来做这件事,但没有运气 add_filter( 'woocommerce_add_cart_item_data', 'update_weight_on_
add_filter( 'woocommerce_add_cart_item_data', 'update_weight_on_add_to_cart', 10, 3);
及
我可以使用添加过滤器设置自定义字段。这样做,我可以看到我成功地设置了新的产品重量。但一旦我进入购物车页面,重量就会恢复到编辑产品页面上设置的重量
这是我当前用于检查以下各项的函数:
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;
}
然后,我在购物车上显示这些字段,以查看是否使用以下钩子和函数成功地更改了数据。我从博客帖子或其他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
任何帮助都将不胜感激!如果不能这样做,有没有更好的方法来解决这个问题?可能需要在购物车上完成此操作?存在一些错误、错误和缺少的部分…以下是所有产品类型(包括产品变体)执行此操作的方法: 代码进入活动子主题活动主题的function.php文件。测试和工作
又来了!有没有办法使用set_weight更改实际重量?你的代码能做到吗?我想确保这段代码与我计划实现的名为WooCommerce Weight-Based Shipping的插件很好地匹配。再次感谢。新的$weight值=3.45;是您在第一个函数中计算的重量…它在第三个函数中设置:$cart_item['data']->set_weight$cart_item['weight']['new']…所以在第一个函数中,你计算了重量……嘿,我不是故意让你挂着的,但我上周休假,直到今天早上才有机会尝试你的代码。此外,我想再次感谢你的工作。代码完全按照预期工作,并与我提到的插件一起工作。再次感谢,马修斯这个答案很好-我用它来计算体积重量,与原始重量相比,使用产品的尺寸。但是,我需要将其更改为使用客户在产品页面上输入的重量、长度、宽度和高度,而不是产品详细信息中的内容,就像我有一个“命名您的价格”插件一样。这些字段在产品页上还不存在。。。这件事你能帮我吗?如果没有,那就没问题了。再次感谢!
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;
}
// 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']);
}
}
}