php Woocommerce,隐藏基于航运类的航运方法

b5lpy0ml  于 2023-01-16  发布在  PHP
关注(0)|答案(2)|浏览(195)

我尝试隐藏所有基于运输类的运输方法,但只保留一种,这实际上是在选择属于特定类的产品时强制使用FedEx隔夜运输方法。
我从this code开始,并按如下所示对其进行修改:

add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_class' ,    10, 1 );

function check_cart_for_share() {

// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;

$found = false;

// loop through the array looking for the tag you set. Switch to true if the tag is     found.
foreach ($cart as $array_item) {
$term_list = wp_get_post_terms( $array_item['product_id'], 'product_shipping_class', array( "fields" => "names" ) );

if (in_array("Frozen",$term_list)) {

      $found = true;
      break;
    }
}

return $found;

}

function hide_shipping_based_on_class( $available_methods ) {

// use the function above to check the cart for the tag.
if ( check_cart_for_share() ) {

// remove the rate you want
unset( $available_methods['canada_post,purolator,fedex:FEDEX_GROUND,fedex:GOUND_HOME_DELIVERY'] ); 
}

// return the available methods without the one you unset.
return $available_methods;

}

它似乎没有隐藏任何运输方式虽然。不知道我错过了什么...
这是一个多站点安装,我在加拿大的http://stjeans.harbourcitydevelopment.com测试它
我正在运行表率航运模块,以及联邦快递,Purolator和加拿大邮政模块。

irtuqstp

irtuqstp1#

我有同样的问题,修改你的代码有帮助。一个问题是“woocommerce_available_shipping_methods”过滤器从WooCommerce 2.1中被弃用。所以你必须使用新的:“woocommerce_package_rates"。类似的任务也有WooCommerce tutorial
因此,我修改了filter hook,当条件为真时,我迭代所有的运输方式/费率,找到我想向客户显示的方式,从中创建新数组并返回该数组(只有一个项目)。
我认为你的问题(除了不赞成的钩子)主要是错误的unset($available_methods[...])行。它不能像那样工作。
下面是我的代码:

add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_class' ,    10, 2 );
function hide_shipping_based_on_class( $available_methods ) {
    if ( check_cart_for_share() ) {
        foreach($available_methods as $key=>$method) {
            if( strpos($key,'YOUR_METHOD_KEY') !== FALSE ) {
                $new_rates = array();
                $new_rates[$key] = $method;
                return $new_rates;
            }
        }
    }
    return $available_methods;
}

警告!我发现woocommerce_package_rates钩子不是每次都被触发,而是只有当您更改购物车中的项目或项目数量时才被触发。或者对我来说是这样。也许这些可用的价格以某种方式缓存了购物车内容。

nhn9ugyo

nhn9ugyo2#

下面的代码片段允许您隐藏基于航运类的航运方式。详细说明可here

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);

function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
{
$hide_when_shipping_class_exist = array(
    42 => array(
        'free_shipping'
    )
);

$hide_when_shipping_class_not_exist = array(
    42 => array(
        'wf_shipping_ups:03',
        'wf_shipping_ups:02',
         'wf_shipping_ups:01'
    ),
    43 => array(
        'free_shipping'
    )
);

$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;
}

相关问题