在我的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相关问答推荐

我如何知道BaseController中当前运行的是哪个控制器?

如何将对我的域的请求重定向到子文件夹中的索引,同时保留域URL并使用.htaccess?

从WooCommerce购物车和 checkout 页面自定义发货标签

PHP中的圆生成器在国际日期变更线上失败

Laveel Livewire Get Groupby姓名列表

根据未用于变体的产品属性隐藏WooCommerce发货方式

模式 bootstrap 未显示

Codeigniter 4中的未定义变量$HOME

Laravel路由到控制器传输变量解决方案需要

使用HPOS过滤WooCommerce中的订单列表

是否有条件判断是否发布了另一个Wordpress页面?

如何使用 PHP 创建简化的 twig 替换?

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

如何在 Woocommerce 中自动删除旧的已完成订单

Docker-用于部署的 PHP 应用程序映像无法在本地运行

产品页面自定义复选框可对 WooCommerce 购物车小计启用百分比折扣

在WooCommerce单产品页面中显示特价产品的节省金额

如何在PHP中对多个脚本调用进行排队?

.htaccess环境变量条件不被判断

无法按今天加上 7 天显示数据库项目