首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce -基于X个数量步骤的统一运价?

WooCommerce -基于X个数量步骤的统一运价?
EN

WordPress Development用户
提问于 2019-04-05 08:33:57
回答 3查看 2.9K关注 0票数 2

我们需要根据购物车中的每一个X项运行WooCommerce统一费率运费,例如:

  • 1-5项:交货=£5
  • 6-10项:交货=£10
  • 11-15项:交货=£15
  • …等

它本质上是每个项目的be 1,但它在be 5的括号中,因此3项将是be 5,而7项将是be 10。

我希望通过开箱即用的运费可以实现这一点,或者说,如果需要的话,我会相对比较满意地编写一个主题函数--但是客户在这方面的预算有限。

我已经尝试寻找更多的参考,我可以使用在先进的运输选项,但似乎找不到一个方法来实现上述的可用的短代码。

EN

回答 3

WordPress Development用户

回答已采纳

发布于 2019-04-11 22:59:28

为此,您需要在woocommerce_package_rates过滤器钩子中使用一个挂钩函数,目标是“固定费率”传送方法。

flat费率传送方法的传递设置中,您将set的成本为 5

每5项步骤,运费将从1:·增加5:·从1项增加到5项:交货= 5·从6项增加到10项:交货= 10·从11项增加到15项:交货= 15··等等。

以下是代码:

代码语言:javascript
复制
add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_steps', 10, 2 );
function different_rates_based_on_quantity_steps( $rates, $package ){

    $items_count  = WC()->cart->get_cart_contents_count(); // Cart item count
    $items_change = 5; // number of items needed to increase the cost each time
    $rate_operand = ceil( $items_count / $items_change ); // Operand increase each 5 items here

    foreach ( $rates as $rate_key => $rate ){
        // Targetting "Flat rate"
        if( 'flat_rate' === $rate->method_id ) {
            $has_taxes = false;

            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $rate_operand;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    // New tax calculated cost
                    $taxes[$key] = $tax * $rate_operand;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

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

Refresh运输缓存:(必需)

  1. 此代码已保存在活动主题的function.php文件中。
  2. 车是空的
  3. 在传送区域设置中,禁用/保存任何传送方法,然后启用回/保存。
票数 3
EN

WordPress Development用户

发布于 2020-05-07 15:08:07

仍然不能评论,所以我需要张贴这个修复以上可怕的答案。

要使速率计算正常工作,return $rates;行必须位于cart循环之外。

完整的职能应该是:

代码语言:javascript
复制
$step_change = 5; // Steps for item
$item_count = 0;

// 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() == $slug_class_shipping ){

$item_count += $cart_item['quantity']; // Cart item count
$rate_operand = ceil( $item_count / $step_change ); // Operand increase each # items here

foreach ( $rates as $rate_key => $rate ){
    // Targetting "Flat rate"
    if( 'flat_rate' === $rate->method_id ) {
        $has_taxes = false;

        // Set the new cost
        $rates[$rate_key]->cost = $rate->cost * $rate_operand;

        // Taxes rate cost (if enabled)
        foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $tax > 0 ){
                // New tax calculated cost
                $taxes[$key] = $tax * $rate_operand;
                $has_taxes = true;
            }
        }
        // Set new taxes cost
        if( $has_taxes )
            $rates[$rate_key]->taxes = $taxes;
     }
   }    
 }
  return $rates;
}
票数 1
EN

WordPress Development用户

发布于 2019-12-28 19:33:07

要添加特定的类,您必须检查购物车中的每一项,并以这样的方式检查条目对应的类的段塞:

首先添加woocommerce_package_rates过滤器

代码语言:javascript
复制
add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_steps', 10, 2 );

然后插入函数

代码语言:javascript
复制
function different_rates_based_on_quantity_steps ( $rates, $package ){

$slug_class_shipping = 'books'; // Slug of the shipping class
$step_change = 5; // Steps for item
$item_count = 0;

// 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() == $slug_class_shipping ){

$item_count += $cart_item['quantity']; // Cart item count
$rate_operand = ceil( $item_count / $step_change ); // Operand increase each # items here

foreach ( $rates as $rate_key => $rate ){
    // Targetting "Flat rate"
    if( 'flat_rate' === $rate->method_id ) {
        $has_taxes = false;

        // Set the new cost
        $rates[$rate_key]->cost = $rate->cost * $rate_operand;

        // Taxes rate cost (if enabled)
        foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $tax > 0 ){
                // New tax calculated cost
                $taxes[$key] = $tax * $rate_operand;
                $has_taxes = true;
            }
        }
        // Set new taxes cost
        if( $has_taxes )
            $rates[$rate_key]->taxes = $taxes;
    }
}    return $rates;
} break; } } 
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/333554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档