根据 WooCommerce 中的属性值计数更改固定运费
Change Flat Rate shipping cost based on attribute values count in WooCommerce
我试图在不使用插件的情况下指定 2 种不同的统一费率运输方式成本:
- 如果购物车中只有一个供应商的产品,统一运费需要 19 英镑。
- 如果购物车中有来自多个供应商的多种产品,统一运费需要 39 英镑。
我尝试过各种插件,但它们主要关注基于尺寸、重量、数量、位置、类别的运费,而不是属性或条款。
我有一个名为 Vendor 的属性,有 8 个术语。每个 Term 都是不同的 Vendor/supplier.
这是我想要实现的 PHP 逻辑类型:
if product attribute term quantity = 1
then flat rate = £19
else
if product attribute term quantity > 1
then flat rate = £39
当购物车中有超过 1 个属性供应商条款时,如何更改此 "Flat rate" 运输方式成本?
这个过程需要 2 个步骤:一些代码和一些设置…
1) CODE - 您可以使用挂钩在 woocommerce_package_rates
过滤器挂钩中的自定义函数,当购物车商品来自更多时,针对 "Flat rate" 送货方式超过 1 个供应商:
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) SETTINGS - 将上述代码保存到活动主题的 function.php 文件后,您需要设置(对于所有送货区域) "Flat rate" 送货方式的成本为 19
(£19)(并节省)。
IMPORTANT: To refresh Shipping method caches you will need to disable "flat rate" then save, and enable back "flat rate" then save.
现在这应该可以正常工作了。
我试图在不使用插件的情况下指定 2 种不同的统一费率运输方式成本:
- 如果购物车中只有一个供应商的产品,统一运费需要 19 英镑。
- 如果购物车中有来自多个供应商的多种产品,统一运费需要 39 英镑。
我尝试过各种插件,但它们主要关注基于尺寸、重量、数量、位置、类别的运费,而不是属性或条款。
我有一个名为 Vendor 的属性,有 8 个术语。每个 Term 都是不同的 Vendor/supplier.
这是我想要实现的 PHP 逻辑类型:
if product attribute term quantity = 1
then flat rate = £19
else
if product attribute term quantity > 1
then flat rate = £39
当购物车中有超过 1 个属性供应商条款时,如何更改此 "Flat rate" 运输方式成本?
这个过程需要 2 个步骤:一些代码和一些设置…
1) CODE - 您可以使用挂钩在 woocommerce_package_rates
过滤器挂钩中的自定义函数,当购物车商品来自更多时,针对 "Flat rate" 送货方式超过 1 个供应商:
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) SETTINGS - 将上述代码保存到活动主题的 function.php 文件后,您需要设置(对于所有送货区域) "Flat rate" 送货方式的成本为 19
(£19)(并节省)。
IMPORTANT: To refresh Shipping method caches you will need to disable "flat rate" then save, and enable back "flat rate" then save.
现在这应该可以正常工作了。