在我的WooCommerce网站上,每种产品都有四个产品属性(SizeColorSeasonBrand).属性SizeColor用于变化,而SeasonBrand不用于变化.

其目的是隐藏"S24"Season产品的免运费.我从以下代码开始,但它只适用于用于变体的属性:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_variation_product_attribute', 10, 2 );
function hide_shipping_method_based_on_variation_product_attribute( $rates, $package ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE define the Product Attibute taxonomy (starts always with "pa_")
    $taxonomy = 'pa_season'; // Example for "Color"

    // HERE define shipping method rate ID to be removed from product attribute term(s) slug(s) (pairs) in this array
    $data_array = array(
        'free_shipping:8'      => array('s24'),
        'flat_rate:17'   => array('f23'),
    );

    // Loop through cart items
    foreach( $package['contents'] as $cart_item ){
        if( isset($cart_item['variation']['attribute_'.$taxonomy]) ) {
            // The product attribute selected term slug
            $term_slug = $cart_item['variation']['attribute_'.$taxonomy];

            // Loop through our data array
            foreach( $data_array as $rate_id => $term_slugs ) {
                if( in_array($term_slug, $term_slugs) && isset($rates[$rate_id]) ) {
                    // We remove the shipping method corresponding to product attribute term as defined
                    unset($rates[$rate_id]);
                }
            }
        }
    }
    return $rates;
}

如何使其适用于不用于变体的产品属性?

推荐答案

对于不在变体中使用的产品属性,当购物车项目是产品变体时,您需要首先获取父变量产品.

然后,您可以使用两种方法:

  1. 使用WC_Product get_attributes()方法(the more secure way):
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_product_attribute', 10, 2 );
function hide_shipping_method_based_on_product_attribute( $rates, $package ) {
    // Product Attribute taxonomy (start always with "pa_")
    $taxonomy = 'pa_season'; // Example for "Season"

    // Array of Shipping method rate ID and term(s) slug(s) pairs
    $data_array = array(
        'free_shipping:8' => array('s24'),
        'flat_rate:17'    => array('f23'),
    );

    // Loop through items in the current shipping package
    foreach( $package['contents'] as $item ){
        // Get the parent variable product if it's a product variation
        $product = $item['variation_id'] > 0 ? wc_get_product($item['product_id']) : $item['data'];
            
        // Loop through our data array
        foreach( $data_array as $rate_id => $term_slugs ) {
            // loop through product attributes
            foreach($product->get_attributes() as $attribute ) {
                if( $attribute->get_taxonomy() === $taxonomy && isset($rates[$rate_id]) 
                && count( array_intersect($attribute->get_slugs(), $term_slugs) ) > 0 ) {
                    unset($rates[$rate_id]);
                }
            }
        }
    }
    return $rates;
}

代码位于您的子主题的unctions.php文件中(或在插件中).应该能行得通.


  1. 使用WC_Product get_attribute( $attribute )方法,(where 102 can be the attribute taxonomy or the attribute label name):

因为方法get_attribute()总是返回产品term(s) name(s)string,所以我们将使用它only when the targeted attribute has a unique term设置在产品中.

然后在下面的代码中,我们将使用术语名称而不是术语slug:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_product_attribute', 10, 2 );
function hide_shipping_method_based_on_product_attribute( $rates, $package ) {
    // Product Attribute taxonomy (start always with "pa_")
    $taxonomy = 'pa_season'; // Example for "Season"

    // Array of Shipping method rate ID and term(s) NAME(s) pairs
    $data_array = array(
        'free_shipping:8' => array('S24'),
        'flat_rate:17'    => array('F23'),
    );

    // Loop through items in the current shipping package
    foreach( $package['contents'] as $item ){
        // Get the parent variable product if it's a product variation
        $product = $item['variation_id'] > 0 ? wc_get_product($item['product_id']) : $item['data'];
            
        // Loop through our data array
        foreach( $data_array as $rate_id => $term_names ) {
            if( in_array($product->get_attribute($taxonomy), $term_names) && isset($rates[$rate_id]) ) {
                unset($rates[$rate_id]);
            }
        }
    }
    return $rates;
}

代码位于您的子主题的unctions.php文件中(或在插件中).应该能行得通.

相关:Hide shipping methods based on Variation product attribute in WooCommerce

Php相关问答推荐

打印SQL表内容时在DIV内添加断点

根据特定运输类别增加WooCommerce购物车总重量

以编程方式更新现有Yith WooCommerce Booking的状态

如何修复条带元素中的错误&&客户端_机密&

如何在函数中定位WooCommerce产品循环

将部分产品排除在WooCommerce的计算附加费之外

在PHP中处理Linux输入事件(/dev/input/Event*)

如果日期是过go 日期,则隐藏事件发布类型

ANTLR4-lexer如何消耗更多的 token 并停止现有规则?

更改特定产品标签的 Woocommerce 产品名称

批量更新 WooCommerce 中所有产品的特定自定义字段

PHP 解析 XML 以获取多个深度的所有字符串值

Golang - Html 模板:如何创建导航项 - 子菜单?我找不到怎么做

仅在管理员新订单通知中显示WooCommerce订单商品自定义元数据

docker |管道失败的 ubuntu 源列表

按前 3 列对二维数组中的行数据进行分组,然后用逗号连接每组中的其余列

如何在 WooCommerce 中显示每个自定义选项卡的内容

Eloquent 的模型更新了它应该 laravel php 的更多行

使用 GROUP_CONCAT 规范化/转置来自查询的数据

为什么 PDO 允许使用带命名占位符的索引数组,但仅在禁用仿真时才允许?