【发布时间】:2021-02-04 05:39:36
【问题描述】:
基本上,当购物车中只有特定的运输类别(包括 ID 40 到 46)时,我试图隐藏统一费率运输方式“flat_rate:8”。但是,当购物车中有任何运输类别为 47、48、49 的产品时,我想隐藏除此“flat_rate:8”之外的所有其他运输方式。 以下所有详细信息。
我有 9 种运输类别(从 ID 40 到 46):XXS、XS、S、... L、XL、XXL、XXXL 和 5 种运输方式(flate_rate3、local_pickup、flat_rate6、flat_rate7、flat_rate8)。
当我在购物车中时:
- 混合产品,其中至少有一个是 XL、XXL 或 XXXL
- 仅限 XL、XXL 或 XXXL 产品(一件或多件)
我只希望提供 2 种运输方式(local_pickup 和 flate_rate8)。基本上,如果购物车中有任何 >= XL,我只希望这两种方法出现。 我能够使用这段代码来完成这 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. 47 = shipping class XL
$class = 47;
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');
// 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', '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. 48 = shipping class XXL
$class = 48;
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');
// 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', '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. 49 = shipping class XXXL
$class = 49;
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:3', 'flat_rate:6', 'flat_rate:7');
// 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 种运输方式(local_pickup 和 flat_rate8)。
那么,我想做的是:
当我在购物车中时:
- 仅限运输等级为 XXXS (id=40)、XXS (41)、XS (43)、S (44)、M (45)、L (46) 的商品
我想删除 flat_rate8 并保留所有其他运输方式。 使用当前代码和设置,现在,当我的购物车中有 =
我一直在尝试复制上面显示的代码以仅在购物车中有“小”产品时隐藏此 flat_rate8,但显然它不起作用,因为当我有一个混合购物车(例如 XXXL 和 S)时将从选项中删除 flat_rate8。
我试过了,除了上面显示的那段代码添加这个:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_classd', 10, 2 );
function hide_shipping_method_based_on_shipping_classd( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package
$product_id = $package_item['product_id']; // Grab product_id
$_product = wc_get_product( $product_id ); // Get product info using that id
if( $_product->get_shipping_class_id() != 47 ){ // If we DON'T find this shipping class ID (XL)
unset($rates['flat_rate:8']); // Then remove this shipping method
break; // Stop the loop, since we've already removed the shipping method from this package
}
}
return $rates;
}
但它不起作用:
- 当我在购物车中有 XL 产品 (id=47) 和 S 产品时,flat_rate8 不可见(应该是,它是大小产品的混合)
- 当我只有 XL 产品时 flat_rate8 在这里(很好)
- 当我有小型产品(XS、S、M 等)时,flat_rate8 不在这里(很好)
我一直在寻找许多主题,其中包括:
- Hide shipping methods for specific shipping class in WooCommerce
- Conditionally Hide WooCommerce Shipping methods based on shipping class
但我无法找到解决问题的方法。
【问题讨论】:
标签: php wordpress woocommerce cart shipping-method