wordpress WooCommerce -修改购物车重量不影响运费

50few1ms  于 2023-02-18  发布在  WordPress
关注(0)|答案(3)|浏览(152)

晚上好。我写了一些代码来修改我的购物车中的一些特定物品的总重量(由于快递的特定要求)。在你问之前...不,我不能修改单件重量,因为我必须计算一个特殊的 Package 重量。是的,这是一个很长的故事,
我的代码看起来像这样(简化版):

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基于重量的运费”插件来管理我的运费。
为什么新的权重被插件忽略?
谢谢

vnzz0bqm

vnzz0bqm1#

由于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基于重量的运输插件从每个产品获得重量,您将无法更改购物车重量,因此运费。

k97glaaz

k97glaaz2#

如果您使用的是WooCommerce Weight Based Shipping插件,
如果您在WC()->cart->calculate_shipping()(使用挂钩woocommerce_checkout_update_order_review)之前更改购物车数据,@LoicTheAztec答案可能有效。

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

资料来源:woocommerce/包含/class-wc-ajax. php

hvvq6cgz

hvvq6cgz3#

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

相关问题