Change total woocommerce order weight

前端 未结 2 2099
-上瘾入骨i
-上瘾入骨i 2021-01-14 09:58

I need change total weight of order in woocommerce website.

For example: I have a 3 product in a cart: 1 - 30g; 2 - 35; 3 - 35g; total = 30+35+35 = 100g, but I want

相关标签:
2条回答
  • 2021-01-14 10:09

    Hook in the right filter action

    Let's have a look on the function get_cart_contents_weight():

    public function get_cart_contents_weight() {
        $weight = 0;
    
        foreach ( $this->get_cart() as $cart_item_key => $values ) {
            $weight += $values['data']->get_weight() * $values['quantity'];
        }
    
        return apply_filters( 'woocommerce_cart_contents_weight', $weight );
    }
    

    There is a filter hook we can use: woocommerce_cart_contents_weight

    So we can add a function to this filter:

    add_filter('woocommerce_cart_contents_weight', 'add_package_weight_to_cart_contents_weight');
    
    function add_package_weight_to_cart_contents_weight( $weight ) {        
        $weight = $weight * 1.3; // add 30%     
        return $weight;     
    }
    

    To add the package weight to every product separately, you can try this:

    add_filter('woocommerce_product_get_weight', 'add_package_to_product_get_weight');
    
    function add_package_to_product_get_weight( $weight ) {
        return $weight * 1.3;
    }
    

    But do not use both solutions together.

    0 讨论(0)
  • 2021-01-14 10:15

    It's working at my end. Update total weight to new weight value.

    add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
    function myprefix_cart_extra_info() {
        global $woocommerce;
        echo '<div class="cart-extra-info">';
        echo '<p class="total-weight">' . __('Total Weight:', 'woocommerce');
        echo ($woocommerce->cart->cart_contents_weight*0.3)+$woocommerce->cart->cart_contents_weight;   
        echo '</p>';
        echo '</div>';
    }
    
    0 讨论(0)
提交回复
热议问题