WooCommerce - 修改后的购物车重量不会影响运输成本



晚上好。 我写了一些代码来改变购物车中某些特定物品的总重量(由于快递员的特定要求(。在你问之前...不,我不能修改单件重量,因为我必须计算包装的特殊重量。是的,这是一个很长的故事,

我的代码看起来像这样(简化版(:

function set_my_weight($weight) {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart(); 
$addedWeight = 0;
echo "prev: ". $weight;
foreach($cart_items as $prod => $values):
if(conditions):
$addedWeight += 1.5;
endif;
endforeach;
$weight += $addedWeight;
echo "final: ". $weight;
return $weight;
}
add_filter('woocommerce_cart_contents_weight', 'set_my_weight',10,1);

当我回显值调用时echo WC()->cart->cart_contents_weight;一切似乎都很好,但由于某种原因,运输成本没有改变。 我正在使用"WooCommerce基于重量的运输"插件来管理我的运输成本。

为什么插件忽略了新权重?

谢谢

由于WooCommerce基于重量的运输是第三方插件,因此有不同的方法可以根据购物车重量计算成本变化。

以下是改变购物车重量的 2 种方法。

1( 更改购物车内容总重量:

add_filter( 'woocommerce_cart_contents_weight', 'filter_wc_cart_contents_weight', 10000 );
function filter_wc_cart_contents_weight( $weight ) {
$condition    = true; // Only for testing
$extra_weight = 1.5;
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$quantity = $cart_item['quantity']; // If needed
if( $condition ) {
$weight += $extra_weight;
}
}
return $weight;
}
// For testing: display the total weight after order total in cart page (Ajax updated)
add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
?>
<tr class="order-total">
<th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
</tr>
<?php
}

代码进入活动子主题(或活动主题(的函数.php文件。经过测试并工作。


2(更改购物车物品重量:

add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_tax_class', 10000 );
function conditionally_change_tax_class( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$condition    = true; // Only for testing
$extra_weight = 1.5;
// Looo through our specific cart item keys
foreach ( $cart->get_cart() as $cart_item ) {
// get product weight
$weight = (float) $cart_item['data']->get_weight();
// Set the new product weight
$cart_item['data']->set_weight( $weight + ( $extra_weight / $cart_item['quantity'] ) );
}
}
// For testing display the total weight after order total
add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
?>
<tr class="order-total">
<th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
</tr>
<?php
}

代码进入活动子主题(或活动主题(的函数.php文件。经过测试并工作。


现在,如果WooCommerce基于重量的运输插件从每个产品中获得重量,您将无法更改购物车重量,因此运输成本。

如果您正在使用这个WooCommerce基于重量的运输插件,

如果您之前尽早更改购物车数据,@LoicTheAztec答案可能会起作用WC()->cart->calculate_shipping()哪个正在使用钩子woocommerce_checkout_update_order_review.

add_action( 'woocommerce_checkout_update_order_review', 'conditionally_change_tax_class', 10000 );

来源:woocommerce/include/class-wc-ajax.php

过滤器woocommerce_cart_contents_weight不可用,因为get_cart_contents_weigh不用于设置运费。一种解决方案是修改购物车中每种产品的重量。

最新更新