【问题标题】:Charge local pickup costs per item quantity in WooCommerce在 WooCommerce 中按商品数量收取本地取货费用
【发布时间】:2017-12-22 08:35:07
【问题描述】:

我这样计算统一运费:x * [qty]

我也想将此方法用于我的local pickup 方法,但使用如上所示的函数不起作用。

如何根据商品数量计算运费?

WordPress:4.8.4 / WooCommerce:3.1.1

页面链接:http://www.minimoto.me/

更新:

在第一个有用的答案之后,这是我正在使用的代码:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ##  -----  1. Hiding shipping methods based on shipping class 92  -----  ##

    // HERE define your shipping class to find
    $class = 92;

    // HERE define the shipping methods you want to hide
    $method_key_ids = array('local_pickup:8');

    // Checking in cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class_id() == $class ){
            foreach( $method_key_ids as $method_key_id ){
                unset($rates[$method_key_id]); // Remove the targeted methods
            }
            break; // Stop the loop
        }
    }

    ##  ----- 2. Hiding shipping methods based on shipping class 132  -----  ##

    // HERE define your shipping class to find
    $class = 132;

    // HERE define the shipping methods you want to hide
    $method_key_ids = array('local_pickup:2', 'local_pickup:3', 'local_pickup:4');

    // Checking in cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class_id() == $class ){
            foreach( $method_key_ids as $method_key_id ){
                unset($rates[$method_key_id]); // Remove the targeted methods
            }
            break; // Stop the loop
        }
    }

    ##  -------  3. Charge local pickup costs per item quantity  -------  ##

    $cart_items_count = WC()->cart->get_cart_contents_count(); // Cart items count

    // Iterating through Shipping Methods
    foreach ( $rates as $rate_key => $rate ) {
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;

        // For "Local pickup" Shipping" Method only
        if ( 'local_pickup' === $method_id ) {
            if( ! empty( $rates[$rate_id]->cost && $rates[$rate_id]->cost > 0 ) ) {
                // Set the rate calculated cost based on cart items count
                $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cart_items_count, 2);
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                        $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cart_items_count, 2 );
                        $has_taxes = true;
                    } else {
                        $has_taxes = false;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_id]->taxes = $taxes;
            }
        }
    }

    return $rates;
}

【问题讨论】:

    标签: php wordpress woocommerce cart shipping


    【解决方案1】:

    您可以使用 woocommerce_package_rates 操作挂钩中挂钩的自定义函数,在来自 this answer 的现有自定义代码中进行此操作。

    使用以下代码,您的本地取货运输方式费用将乘以购物车商品总数:

    add_filter( 'woocommerce_package_rates', 'customizing_shipping_methods', 10, 2 );
    function customizing_shipping_methods( $rates, $package )
    {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        ##  -----  1. Hiding shipping methods based on shipping class  -----  ##
    
        // HERE define your shipping class to find
        $class = 92;
    
        // HERE define the shipping methods you want to hide
        $method_key_ids = array('flat_rate:7', 'local_pickup:3');
    
        // Checking in cart items
        foreach( WC()->cart->get_cart() as $cart_item ){
            // If we find the shipping class
            if( $cart_item['data']->get_shipping_class_id() == $class ){
                foreach( $method_key_ids as $method_key_id ){
                    unset($rates[$method_key_id]); // Remove the targeted methods
                }
                break; // Stop the loop
            }
        }
    
        ##  -------  2. Charge local pickup costs per item quantity  -------  ##
    
        $cart_items_count = WC()->cart->get_cart_contents_count(); // Cart items count
    
        // Iterating through Shipping Methods
        foreach ( $rates as $rate_key => $rate_values ) {
            $method_id = $rate_values->method_id;
            $rate_id = $rate_values->id;
    
            // For "Local pickup" Shipping" Method only
            if ( 'local_pickup' === $method_id ) {
                if( ! empty( $rates[$rate_id]->cost && $rates[$rate_id]->cost > 0 ) ) {
                    // Set the rate calculated cost based on cart items count
                    $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cart_items_count, 2);
                    // Taxes rate cost (if enabled)
                    foreach ($rates[$rate_id]->taxes as $key => $tax){
                        if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                            $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cart_items_count, 2 );
                            $has_taxes = true;
                        } else {
                            $has_taxes = false;
                        }
                    }
                    if( $has_taxes )
                        $rates[$rate_id]->taxes = $taxes;
                }
            }
        }
    
        return $rates;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且可以工作

    有时,您可能需要刷新前往送货区域的送货方式,然后禁用/保存并重新启用/保存您的“统一费率”和“本地取货”送货方式。

    【讨论】:

    • 谢谢!我试过你的代码,上面的部分工作得很好。不幸的是,“本地取货部分”没有按预期工作。你有什么建议吗?
    • 我认为代码可能没有按预期工作,因为我们搜索 local_pickup 方法,但方法名称后总是有一个数字,例如 local_pickup:3 所以我将其更改为 local_pickup:3看看这是否可行,但不幸的是它没有
    • @NiklasBuschner 我已经更新了代码,经过测试并且可以工作... 但是之前(一旦保存了新代码)不要忘记刷新运输方式,正如我在答案末尾所建议的那样……它现在应该也适用于您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2020-10-12
    • 2018-05-27
    • 2021-05-16
    • 1970-01-01
    • 2021-07-24
    • 2021-05-15
    相关资源
    最近更新 更多