【问题标题】:Change Flat Rate shipping cost based on attribute values count in WooCommerce根据 WooCommerce 中的属性值计数更改统一运费
【发布时间】:2017-08-13 09:37:03
【问题描述】:

我正在尝试在不使用插件的情况下指定 2 种不同的统一运费方式的运费:

  • 如果购物车中只有一个供应商的产品,则统一运费需要为 19 英镑。
  • 如果购物车中有多个来自多个供应商的产品,则统一运费需要为 39 英镑。

我尝试了各种插件,但它们侧重于基于尺寸、重量、数量、位置、类别的运费,而不是属性或条款。

我有一个名为 Vendor 的属性,其中包含 8 个术语。每个术语都是不同的供应商/供应商。

这是我想要实现的 PHP 逻辑类型:

if product attribute term quantity = 1

then flat rate = £19

else

if product attribute term quantity > 1

then flat rate = £39

当购物车中有超过 1 个属性供应商条款时,如何更改此“统一费率”运费?

【问题讨论】:

标签: php wordpress woocommerce cart shipping


【解决方案1】:

这个过程需要两个步骤:一些代码和一些设置……

1) 代码 - 您可以使用挂在 woocommerce_package_rates 过滤器挂钩中的自定义函数,当购物车商品来自多个供应商时,针对“统一费率”运输方式:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{

    // SET BELOW your attribute slug… always begins by "pa_"
    $attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")


    // Iterating through each cart item to get the number of different vendors
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        // The attribute value for the current cart item
        $attr_value = $cart_item[ 'data' ]->get_attribute( $attribute_slug );

        // We store the values in an array: Each different value will be stored only one time
        $attribute_values[ $attr_value ] = $attr_value;
    }
    // We count the "different" attribute values stored
    $count = count($attribute_values);

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

        // Targeting "Flat Rate" shipping method
        if ( 'flat_rate' === $method_id ) {
            // For more than 1 vendor (count)
            if( $count > 1 ){
                // Get the original rate cost
                $orig_cost = $rates[$rate_id]->cost;
                // Calculate the new rate cost
                $new_cost = $orig_cost + 20; // 19 + 20 = 39
                // Set the new rate cost
                $rates[$rate_id]->cost = $new_cost;
                // Calculate the conversion rate (for below taxes)
                $conversion_rate = $new_cost / $orig_cost;
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){
                        $new_tax_cost = number_format( $rates[$rate_id]->taxes[$key]*$conversion_rate, 2 );
                        $rates[$rate_id]->taxes[$key] = $new_tax_cost; // set the cost
                    }
                }
            }
        }
    }
    return $rates;
}

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

此代码已使用 woocommerce 版本 3+ 进行测试并且可以正常工作

2) 设置 - 将上述代码保存在您的活动主题的 function.php 文件中后,您将需要设置(对于您的所有运输区域)“统一费率”运输19 (19 英镑) 的方法成本(并节省)。

重要提示:要刷新运输方式缓存,您需要禁用“统一费率”,然后保存,然后启用返回“统一费率”,然后保存

现在这应该可以按预期为您工作了。

【讨论】:

  • 完美运行。那是@LoicTheAztec - 非常感谢。
  • 如果需要动态更改速率怎么办?如果费率取决于购物时间怎么办?并且通过前端切换运输方式 - 运费仍然相同。
猜你喜欢
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 2016-10-22
  • 2023-01-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
相关资源
最近更新 更多