Woocommerce中针对自定义购物车项目数据值的强制免费送货方法 [英] Force free shipping method for custom cart item data value in Woocommerce

查看:92
本文介绍了Woocommerce中针对自定义购物车项目数据值的强制免费送货方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里得到了一位用户的帮助,并帮助我正确修复了代码以使其正常运行,尽管以上内容适用于自定义字段为90天的所有内容,以隐藏所有货运并仅显示免费货运如果可供使用的话。但是,我的客户也将免费送货设置为仅在特定金额上显示。

I've had a user from here help me and assist in getting my code fixed correctly to function correctly and while the above does work for everything with custom field of 90 days to hide all shipping and display only "Free Shipping" if available. However, my client also set free shipping to be shown only on certain amount.

我想知道是否可以添加以下内容;

I'm wondering if I can add the following;

我正在使用的原始代码

add_filter( 'woocommerce_package_rates', 'show_only_free_shipping_for_autodelivery', 100, 2 );
function show_only_free_shipping_for_autodelivery ( $rates, $package ) {
    // Loop through cart items
    foreach( $package['contents'] as $cart_item ){
        if( $cart_item['data']->get_meta('auto_delivery_default') == '90 Days' ) {
            $found = true;
            break; // Stop the loop
        }
    }

    if( ! ( isset($found) && $found ) )
        return $rates; // Exit

    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}

我试图强制免费送货覆盖免费送货的最低金额,同时仍隐藏所有基于元键'auto_delivery_default'的元值的运输选项。

I'm trying to force Free Shipping overwriting the minimum amount for free shipping, while still hiding all shipping options based on the meta value for the meta key 'auto_delivery_default'.


编辑:

最后我发现它是自定义购物车项目数据

目标键为 delivery_options ,值为 3个月

Finally I have discovered that it is custom cart item data:
The targeted key is delivery_options for a value of 3 months.


推荐答案

更新2:

以下代码将当具有自定义数据 delivery_options 的购物车商品的值为 3个月时,强制执行免费送货方法

The following code will force "Free shipping" method when a cart item with a custom data delivery_options has a value of 3 months:

// Conditionally hide others shipping methods when free shipping is available
add_filter( 'woocommerce_package_rates', 'show_only_free_shipping_for_autodelivery', 100, 2 );
function show_only_free_shipping_for_autodelivery ( $rates, $package ) {
    // Loop through cart items
    foreach( $package['contents'] as $cart_item ){
        if( isset($cart_item['delivery_options']) && $cart_item['delivery_options'] == '3 months' ) {
            $found = true;
            break; // Stop the loop
        }
    }

    if( ! ( isset($found) && $found ) )
        return $rates; // Exit

    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}


// Force free shipping for a specific custom field value
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'filter_free_shipping_is_available', 20, 3 );
function filter_free_shipping_is_available ( $is_available, $package, $free_shipping ) {
    // Loop through cart items
    foreach( $package['contents'] as $cart_item ){
        if( isset($cart_item['delivery_options']) && $cart_item['delivery_options'] == '3 months' ) {
            $is_available = true;
            break; // Stop the loop
        }
    }

    return $is_available;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并可以使用。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


需要临时刷新运输缓存,从而启用 调试模式运输选项标签下的 strong>标签。
一旦测试并正常工作,只需禁用它即可,一切仍将正常进行。

Refreshing shipping caches temporarily is needed, enabling the "Debug mode" in Woocommerce global shipping settings under "Shipping options" tab.
Once tested and working, just disable it and everything will still work.

这篇关于Woocommerce中针对自定义购物车项目数据值的强制免费送货方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆