【问题标题】:Shipping Rates based on Product Quantity on woocommercewoocommerce 上基于产品数量的运费
【发布时间】:2016-05-02 04:59:50
【问题描述】:

我想在我的 woocommerce 主题上明智地设置运费数量。我想做这个选项:

对于 1 到 5 件产品,运费为 15%。 超过 5 种产品的鞭打成本为 6.99 美元。

我可以在没有插件的情况下添加此运输选项吗?

【问题讨论】:

    标签: wordpress woocommerce shipping wordpress-ecommerce


    【解决方案1】:

    您需要将一个函数与woocommerce_calculate_totals 操作挂钩,该操作在计算最终购物车总数之前触发。 woocommerce_calculate_totals 操作提供了 WC_Cart 实例,您可以根据需要对其进行操作。

    add_action('woocommerce_calculate_totals', 'modify_shipping_totals');
    
    function modify_shipping_totals($cart) {
        if($cart->get_cart_contents_count() < 6) {
            $cart->shipping_total = ( 15/100 ) * $this->cart_contents_total; 
            // shipping cost will be 15% of cart content total
    
            // you may also want to modify the shipping tax.
    
            $cart->shipping_tax_total = 0; 
        } else {
            $cart->shipping_total = 6.99;
            $cart->shipping_tax_total = 0;
        }
    
    
    }
    

    有关可变变量的更多参考,请参阅WC_Cart documentation

    【讨论】:

    • 您好 Pranav,我不是高级开发人员。我可以用表格运费插件制作这个系统吗?因为客户想随时更改费用。我正面临这个问题的麻烦。没有人能给我一个正确的答案。你能帮帮我吗?
    • 这对我不起作用。这可能是由于新版本的 woocommerce 造成的。
    【解决方案2】:
    // **Note**: This code is working only when you set Flat rate Settings 
    // cost value is 1
    
    add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
     function custom_package_rates( $rates, $packages ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
        $cart_count = WC()->cart->get_cart_contents_count();
        $cart_total =  WC()->cart->cart_contents_total;
    
        foreach($rates as $rate_key => $rate_values ) {
            $method_id = $rate_values->method_id;
            $rate_id = $rate_values->id;
    
            if( $method_id == 'flat_rate' ){
                if( $cart_count < 99 ){
                    $flat_rate_value = 4.95; //"Applay Flat rate less then 99 quatity"
                    $cart_10_percent = 0; // No percent discount
                }   
                if( $cart_count > 99 ){
                    $flat_rate_value = 9.95; // "Applay Flat rate greater then 99 quatity"
                    $cart_10_percent = 0; // No percent discount
                }
                $rate_cost = $flat_rate_value > $cart_10_percent ? $flat_rate_value - $cart_10_percent : 0;
    
                // Set the new calculated rate cost
                $rates[$rate_id]->cost = number_format( $rates[$rate_id]->cost * $rate_cost, 2 );
    
            }
        }
        return $rates;
    } 
    

    【讨论】:

      【解决方案3】:

      如果您在 init 钩子中调用 add_action,Pranav 解决方案将起作用,如下所示:

      function init_shop(){
          add_action('woocommerce_calculate_totals', 'modify_shipping_totals', 10);
      }
      add_action( 'init', 'init_shop');
      

      【讨论】:

        猜你喜欢
        • 2018-01-30
        • 1970-01-01
        • 2018-03-12
        • 2018-01-04
        • 2018-05-04
        • 2017-03-09
        • 1970-01-01
        • 2020-08-30
        • 2019-12-19
        相关资源
        最近更新 更多