问题描述
我目前正在使用插件和摘要的组合来向客户显示运送方式。我想删除该插件并将所有条件组合成一个片段。但是我需要一些帮助。
这是我要实现的目标:
先检查规则1(中断),再检查规则2(中断),然后再检查规则3。我已经写出了所有逻辑,只是不确定如何将它们全部组合成一个片段。
规则1:隐藏第1类运输的运输方法(基于Hide shipping methods for specific shipping class in WooCommerce)
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 = 150;
// HERE define the shipping methods you want to hide
$method_key_ids = array(
'wf_shipping_ups:07','wf_shipping_ups:08','wf_shipping_ups:11','wf_shipping_ups:54','wf_shipping_ups:65','wf_shipping_ups:70','wf_shipping_ups:74','free_shipping:2','request_shipping_quote'
);
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $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;
}
规则2:如果有5种或更多产品,则隐藏第2类运输的运输方式
add_filter( 'woocommerce_package_rates',$package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$targeted_class_ids = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$shipping_rates_ids1 = array( // Shipping Method rates Ids To Hide if more than 4 items are in cart
'wf_shipping_ups:07','request_shipping_quote'
);
$shipping_rates_ids2 = array( // Shipping Method rates Ids to Hide if 4 or less items are in cart
'flat_rate:20','flat_rate:21'
);
$related_total_qty = 0; // Initializing
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(),$targeted_class_ids ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods (more than 4 items)
foreach( $shipping_rates_ids1 as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
}
return $rates;
}
规则3:隐藏第3类运输的运输方法
add_filter( 'woocommerce_package_rates',$package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 151;
// HERE define the shipping methods you want to hide
$method_key_ids = array(
'flat_rate:20','flat_rate:21'
);
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $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;
}
这是我的尝试:
add_filter( 'woocommerce_package_rates',$package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class for rule 1 to find
$class1 = 150;
// HERE define the shipping methods you want to hide for rule 1
$method_key_ids1 = array(
'wf_shipping_ups:07','request_shipping_quote'
);
// HERE define your shipping class for rule 2 to find
$targeted_class_ids2 = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class rule 2
$shipping_rates_ids1 = array( // Shipping Method rates Ids To Hide for rule 2 if more than 4 items are in cart
'wf_shipping_ups:07','request_shipping_quote'
);
$shipping_rates_ids2 = array( // Shipping Method rates Ids to Hide for rule 2 if 4 or less items are in cart
'flat_rate:20','flat_rate:21'
);
$related_total_qty = 0; // Initializing
// HERE define your shipping class for rule 3 to find
$class3 = 151;
// HERE define the shipping methods you want to hide for rule 3
$method_key_ids3 = array(
'flat_rate:20','flat_rate:21'
);
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class1 ){
foreach( $method_key_ids1 as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(),$targeted_class_ids2 ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods (more than 4 items)
foreach( $shipping_rates_ids1 as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
break; // Stop the loop
}
}
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class3 ){
foreach( $method_key_ids3 as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)