WooCommerce - 修改后的购物车重量不会影响运费。

问题描述 投票:1回答:1

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

我的代码是这样的(简化版)。

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 Weight Based Shipping "插件来管理我的运费。

为什么新的重量会被插件忽略?

谅谅

php wordpress woocommerce cart hook-woocommerce
1个回答
0
投票

由于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
}

代码在您的活动子主题(或活动主题)的function.php文件。经过测试,可以使用。


2) 改变购物车物品的权重:代码放在你的活动主题(或活动主题)的function.php文件中。

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
}

代码在您的活动儿童主题(或活动主题)的function.php文件中。经过测试和工作。


现在,如果WooCommerce基于重量的航运插件从每个产品的重量,你将无法改变购物车的重量,所以运费。

© www.soinside.com 2019 - 2024. All rights reserved.