标签:wordpress php woocommerce cart shipping
使用This site (here)上的WooCommerce v3.2.4(WP v4.9)和11种产品,其中包含适用于他们的固定费率的超重/超大的运费等级:加拿大20美元,美国25美元.
所有其他产品的统一运费为10美元(加拿大)和15美元(美国),除非订单超过100美元,然后自动运送免费送货.
如果购物车中有超重/超大商品,我的客户希望免费送货.问题是购物车说当购物车中有常规和超大尺寸物品混合时没有可用的运输方法,并且没有运输方法.
我正在使用XAdapter Woocommerce Shipping Table Rate插件将更高的成本应用于“超重”运输类.
UPDATE
我停用了此插件,因为我意识到我可以使用WooCommerce Shipping Zone设置为特定的运输类别设置固定费率.见下面的截图:
我正在使用一些代码:
>购物车中存在“超重”运输类别时,隐藏免运费和统一费率
>如果该类不存在,则隐藏“超重”运输方法(163为运输类的ID)…
这是代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package){
$hide_when_shipping_class_exist = array(
163 => array(
'flat_rate:1',
'flat_rate:2',
'free_shipping:3',
'free_shipping:5'
)
);
$hide_when_shipping_class_not_exist = array(
163 => array( 'wf_woocommerce_shipping_pro:overweightoversizeoverweight')
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
if(!in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
编辑
以下是每个送货区的费率ID列表:
加拿大
>常规统一费率| ID:flat_rate:1
>免费送货| ID:free_shipping:3
>本地提货| ID:local_pickup:4
美国
>常规统一费率| ID:flat_rate:2
>免费送货| ID:free_shipping:5
解决方法:
更新2 :(没有任何插件需要,只需设置和代码)
以下功能将始终显示加拿大的“本地取件”运费,并将:
>当“超大”装运类在购物车商品中时,隐藏免费送货方式.对于“统一费率”运输方式,成本将是“超大”运输类的成本.
>如果购物车项目中未设置“超大”装运等级:
>如果购物车金额低于目标免费送货金额:隐藏“免运费”.
>如果购物车金额超过目标免费送货金额:隐藏“统一费率”送货方式.
这是代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($rates, $package){
// Defining & Initializing variables
$free_shipping_rates = array(
'free_shipping:3',
'free_shipping:5'
);
// Defining & Initializing variables
$shipping_class_id = 163;
$free = array();
$over_found = $has_free = false;
// Check if "Oversize" shipping class (163) is in cart items
foreach(WC()->cart->get_cart() as $key => $cart_item){
if($cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$over_found = true;
break;
}
}
// 1. Hiding free shipping but always show Local pickup for Canada
if( $over_found ){
foreach($free_shipping_rates as $rate_id) {
unset( $rates[$rate_id] );
}
}
// 2. Hiding Flat rate OR Free shipping --> depending on cart amount
// (but always show Local pickup for Canada)
else {
foreach ( $rates as $rate_id => $rate ) {
// Hide all "Flat rates" when "Free Shipping" is available
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
$has_free = true;
} elseif ( 'local_pickup' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
}
}
return $has_free ? $free : $rates;
}
return $rates;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.
在WooCommerce 3上测试并且有效.
Refresh the shipping caches (needed sometimes):
1) First empty your cart.
2) This code is already saved on your function.php file.
3) Go in a shipping zone settings and disable one “flat rate” (for example) and “save”. Then re-enable that “flat rate” and “save”. You are done and you can test it.
标签:wordpress,php,woocommerce,cart,shipping 来源: https://codeday.me/bug/20190701/1348389.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。