晚上好。我写了一些代码来改变购物车中一些特定文章的总重量(由于快递员的特定要求)。在你问之前..。不,我不能修改单件重量,因为我必须计算包装的特殊重量。是的,说来话长,
我的代码看起来像这样(简化版本):
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基于重量的运输“插件来管理我的运输成本。
为什么插件会忽略新的权重?
谢谢
发布于 2020-06-09 13:35:19
由于WooCommerce基于重量的运输是一个第三方插件,因此有不同的方法来计算基于购物车重量的成本变化。
下面是两种改变购物车重量的方法。
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
}
代码放在活动子主题(或活动主题)的functions.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
}
代码放在活动子主题(或活动主题)的functions.php文件中。经过测试,效果良好。
现在,如果基于WooCommerce重量的装运插件从每个产品获得重量,您将无法更改购物车重量,因此运输成本。
发布于 2020-12-04 21:48:45
如果您正在使用此WooCommerce Weight Based Shipping插件,
如果你在使用钩子woocommerce_checkout_update_order_review
的WC()->cart->calculate_shipping()
之前更改购物车数据,@LoicTheAztec的答案可能会起作用。
add_action( 'woocommerce_checkout_update_order_review', 'conditionally_change_tax_class', 10000 );
https://stackoverflow.com/questions/62281414
复制