在我的WooCommerce网站上,每种产品都有四个属性(尺寸、 colored颜色 、季节和品牌).Size和Colour属性用于变化,而Season和Brand不用于变化.

我设置了3种送货方式:免费送货(ID 17),没有最低订单,免费送货(ID 8),最低订单100欧元,最后支付送货(ID 1)9.99欧元.

我希望在从S24季节购买产品时有最低订单100欧元的免费送货(ID 17)和9.99欧元的付费送货(ID 1),而如果您从F23季节购买产品,则必须激活最低订单100欧元和付费送货(ID 1)9.99欧元的免费送货(ID 8).在最后一种情况下,如果您购买了至少一个S24季节的产品和至少一个F23季节的产品,则必须只激活没有最低订单的免费送货(ID 17).

以下是开始代码,但它只适用于用于变体的属性:

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

    // 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'),
        'free_shipping:17'    => array('f23'),
        'flat_rate:1'         => 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;
}

推荐答案

以下将处理您所需的所有 case :

  • "S24"术语找到:免费送货(ID 17,最低小计为100欧元)和付费送货激活,
  • 找到"F23"条款:免费送货(ID 8,最小小计为100欧元)和付费送货激活,
  • 找到"S24"和"F23"条款:免费送货(ID 17,最低小计为100欧元)已激活.

Important:您将需要从两个免费送货方式设置remove,最小订单金额限制,因为它是在代码中处理.

try 以下操作:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_product_attribute', 90000, 2 );
function hide_shipping_method_based_on_product_attribute( $rates, $package ) {
    $free_min_subtotal = 100; // Set the minimal amount for free shipping
    $taxonomy = 'pa_season'; // Product Attribute taxonomy (start always with "pa_")
    $has_s24 = $has_f23 = false; // Initializing

    $cart_subtotal = $package['cart_subtotal']; // Or use $package['contents_cost'] to exclude taxes

    // 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 product attributes
        foreach($product->get_attributes() as $attribute ) {
            // Check for "Season" PA
            if( $attribute->get_taxonomy() === $taxonomy ) {
                $term_slugs = $attribute->get_slugs();
                
                if ( in_array( 's24', $attribute->get_slugs() ) ) {
                    $has_s24 = true;
                } elseif ( in_array( 'f23', $attribute->get_slugs() ) ) {
                    $has_f23 = true;
                }
            }
        }
    }

    // Case 1 (term "s24" found)
    if ( isset($rates['free_shipping:17']) && isset($rates['flat_rate:1']) && $has_s24 && ! $has_f23 ) {
        if ( isset($rates['free_shipping:8']) ) {
            unset($rates['free_shipping:8']); // remove Free Shipping 8
        }
        // Handle minimum cart amount for free shipping
        if ( isset($rates['free_shipping:17']) && floatval($cart_subtotal) < $free_min_subtotal ) {
            unset($rates['free_shipping:17']); // remove Free Shipping 17 (required subtotal is lower)
        }
    } 
    // Case 2 (term "f23" found)
    elseif ( isset($rates['free_shipping:8']) && isset($rates['flat_rate:1']) && $has_f23 && ! $has_s24 ) {
        if ( isset($rates['free_shipping:17']) ) {
            unset($rates['free_shipping:17']); // remove Free Shipping 17
        }
        // Handle minimum cart amount for free shipping
        if ( isset($rates['free_shipping:8']) && floatval($cart_subtotal) < $free_min_subtotal ) {
            unset($rates['free_shipping:8']); // remove Free Shipping 8 (required subtotal is lower)
        }
    } 
    // Case 3 (terms "s24" and "f23" found)
    elseif ( isset($rates['free_shipping:17']) && $has_s24 && $has_f23 ) {
        if ( isset($rates['free_shipping:8']) ) {
            unset($rates['free_shipping:8']); // remove Free Shipping 8 
        }
        if ( isset($rates['flat_rate:1']) ) {
            unset($rates['flat_rate:1']); // remove Flat rate 1 
        }
    } 
    // Fallback: For security regarding free shipping and minimal required subtotal
    elseif ( floatval($cart_subtotal) < $free_min_subtotal ) {
        if ( isset($rates['free_shipping:8']) ) {
            unset($rates['free_shipping:8']); // remove Free Shipping 8 
        }
        if ( isset($rates['free_shipping:17']) ) {
            unset($rates['free_shipping:17']); // remove Free Shipping 17
        }
    }

    return $rates;
}

代码放在子主题的functions.php文件中(或插件中).应该能用

注意:不要忘记清空购物车,以刷新运输缓存数据.

Php相关问答推荐

ngnix php8.0-fPM主要脚本未知

条纹 checkout :订阅试用版和额外付款(add_invoice_items)错误

为WooCommerce中的特定用户角色指定促销价格

PHP cUrl扩展与v8.2.12更新损坏

在特定订单状态更改时自定义WooCommerce订单发货项目

如何在WooCommerce产品子类别中显示特定内容

筛选器具有多个查询之一

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

莫德克斯.当我try 使用pThumb为手机压缩图像时,没有任何react

PHP -将字符串拆分为两个相等的部分,但第二个字符串中的单词更多

使用随机生成器在MYSQL中创建唯一ID-定义一个数组来存储已知ID是否安全

如何配置我的.env文件以在laravel的一个项目中连接两个不同的数据库?

在具有Angular 的 Laravel 应用程序中,不显示图像

PHP 中使用 JSON 的自定义会话处理程序会因错误而 destruct 会话

向元素添加类时添加循环错误

使用 PHP 的 OAuth 2.0 MAC

Laravel的Storage::exists返回false,而file_exists返回true.

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

我试图在我的视图上显示从数据库中获取的数据,但我无法显示它.拉维尔 10.x /

同时执行odbc_execute、odbc_fetch_row和odbc_result