仅限英国和 Woocommerce 3 中的特定产品的定制免费送货
Custom free shipping for only UK and a specific product in Woocommerce 3
我一直在尝试为客户的促销产品创建免费送货选项,该产品将在全球范围内发货,但只有来自英国的订单才具有免费送货选项。因此,当有人从美国或香港订购时,将采用通常的费率。但不知何故,我无法将这些国家/地区排除在航运 Class 之外。 (我没有使用免费送货方式,因为当我尝试时,它适用于所有产品,这就是为什么我为此创建了送货方式 Class)
有人可以帮我解决这个问题吗?
非常感谢
抱歉,德国截图,但是否无法设置送货区和送货 类?
您可以设置 2 个送货区域:英国和世界其他地区,并按照您想要的方式为您的产品添加运费 类。如果您尝试此操作但仍然不起作用,您能否附上一些代码示例?
对于特定情况,您不需要在特定产品上使用运输方式。相反,下面的这个自定义函数可以解决问题,在其中定义您的特定产品 ID。
当来自英国的客户仅将您的特定产品添加 到购物车时,该代码会将您的固定费率送货方式重命名为"Free shipping",并将成本设置为零。
To test that code, you should first enable the debug mode in Woocommerce shipping settings under "Shipping options" tab.
此外,您还必须重新设置 "flat rate" 送货方式,或者从您的特定产品中删除送货方式 class。
代码:
add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
// ==> HERE set your targeted product IDs in a coma separated array
$products_ids = array(37);
if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
return $rates; // If 'destination' country is not defined, we exit
// Only for United kingdom customers
if( $package['destination']['country'] != 'GB' )
return $rates; // Non UK customers we exit.
// Loop through cart items and checking if there is any other products than the targeted ones
$found = false;
foreach( $package['contents'] as $item ) {
if( in_array( $item['data']->get_id(), $products_ids ) ){
$found = true;
} else {
return $rates; // Other items found in cart, we exit.
}
}
// When the customer is in UK and the target product is alone in cart we set the flat rate price to zero.
foreach ( $rates as $rate_key => $rate ){
// Targetting flat rate method
if( $rate->method_id == 'flat_rate' && $found ){
// We change the shipping label name
$rates[$rate_key]->label = __("Free shipping", "woocommerce");
// Set the rate cost to zero
$rates[$rate_key]->cost = 0;
// Taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
$taxes[$key] = 0;
$has_taxes = true;
}
}
if( isset($has_taxes) && $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
购物车中有此独特商品的英国客户 - 免运费
购物车中有此商品和其他商品的英国客户 - 正常运输
购物车中有此独特商品的其他国家/地区的客户 - 正常送货
Once you get that working, don't forget to disable the debug mode in Woocommerce shipping settings under "Shipping options" tab.
我一直在尝试为客户的促销产品创建免费送货选项,该产品将在全球范围内发货,但只有来自英国的订单才具有免费送货选项。因此,当有人从美国或香港订购时,将采用通常的费率。但不知何故,我无法将这些国家/地区排除在航运 Class 之外。 (我没有使用免费送货方式,因为当我尝试时,它适用于所有产品,这就是为什么我为此创建了送货方式 Class)
有人可以帮我解决这个问题吗?
非常感谢
抱歉,德国截图,但是否无法设置送货区和送货 类? 您可以设置 2 个送货区域:英国和世界其他地区,并按照您想要的方式为您的产品添加运费 类。如果您尝试此操作但仍然不起作用,您能否附上一些代码示例?
对于特定情况,您不需要在特定产品上使用运输方式。相反,下面的这个自定义函数可以解决问题,在其中定义您的特定产品 ID。
当来自英国的客户仅将您的特定产品添加 到购物车时,该代码会将您的固定费率送货方式重命名为"Free shipping",并将成本设置为零。
To test that code, you should first enable the debug mode in Woocommerce shipping settings under "Shipping options" tab.
此外,您还必须重新设置 "flat rate" 送货方式,或者从您的特定产品中删除送货方式 class。
代码:
add_filter( 'woocommerce_package_rates', 'disable_shipping_methods', 20, 2 );
function disable_shipping_methods( $rates, $package ) {
// ==> HERE set your targeted product IDs in a coma separated array
$products_ids = array(37);
if( ! ( isset($package['destination']['country']) && isset($package['contents']) ) )
return $rates; // If 'destination' country is not defined, we exit
// Only for United kingdom customers
if( $package['destination']['country'] != 'GB' )
return $rates; // Non UK customers we exit.
// Loop through cart items and checking if there is any other products than the targeted ones
$found = false;
foreach( $package['contents'] as $item ) {
if( in_array( $item['data']->get_id(), $products_ids ) ){
$found = true;
} else {
return $rates; // Other items found in cart, we exit.
}
}
// When the customer is in UK and the target product is alone in cart we set the flat rate price to zero.
foreach ( $rates as $rate_key => $rate ){
// Targetting flat rate method
if( $rate->method_id == 'flat_rate' && $found ){
// We change the shipping label name
$rates[$rate_key]->label = __("Free shipping", "woocommerce");
// Set the rate cost to zero
$rates[$rate_key]->cost = 0;
// Taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
$taxes[$key] = 0;
$has_taxes = true;
}
}
if( isset($has_taxes) && $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
购物车中有此独特商品的英国客户 - 免运费
购物车中有此商品和其他商品的英国客户 - 正常运输
购物车中有此独特商品的其他国家/地区的客户 - 正常送货
Once you get that working, don't forget to disable the debug mode in Woocommerce shipping settings under "Shipping options" tab.