我的网站中的产品由两个装运插件之一处理:用于WooCommerce的Printful集成和用于WooCommerce装运的Printify。当每个配送插件中有混合项目时。当存在混合项目时(这是一个冲突和问题),这些插件将每个装运包一分为二。
所以我已经添加了一个运输类'printful'
(id是548
)到由Printful插件处理的产品,并试图调整隐藏运输方法为特定的运输类在WOCommerce答案代码由@LoicTheAzec(欢呼),仅从特定的重复运输包(id 2和3)中删除运输方法,因为运输插件之间存在冲突...
这是我的实际代码:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 548; //CAMDEN HARBOR CHART MUG is in shipping class
// HERE define the shipping methods you want to hide
$method_key_ids = array('printify_shipping_s', 'printify_shipping_e');
// 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_id() == $class ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
但它不起作用,我仍然得到4个运输包,而不是两个:
感谢您的帮助。
这里的问题与在购物车中混合商品时,两个装运插件之间的拆分包冲突有关。在这种情况下,每个插件都会拆分发货包,这将添加4个拆分包,而不是2个。
这些插件正在使用woocommerce\u cart\u shipping\u packages
来拆分具有未知优先级的装运包(因此我将设置非常高的优先级)
。
以下代码将保留购物车中的前2个拆分包(以及签出):
add_filter( 'woocommerce_cart_shipping_packages', 'remove_split_packages_based_on_items_shipping_class', 100000, 1 );
function remove_split_packages_based_on_items_shipping_class( $packages ) {
$has_printful = $has_printify = false; // Initializing
// Lopp through cart items
foreach( WC()->cart->get_cart() as $item ){
// Check items for shipping class "printful"
if( $item['data']->get_shipping_class() === 'printful' ){
$has_printful = true;
} else {
$has_printify = true;
}
}
// When cart items are mixed (using both shipping plugins)
if( $has_printful && $has_printify ){
// Loop through split shipping packages
foreach( $packages as $key => $package ) {
// Keeping only the 2 first split shipping packages
if( $key >= 2 ){
// Removing other split shipping packages
unset($packages[$key]);
}
}
}
return $packages;
}
代码进入函数。活动子主题(活动主题)的php文件。它应该工作,并显示只有两个运输包时,购物车项目混合。