【问题标题】:Hide all shipping methods based on cart items total in WooCommerce根据 WooCommerce 中的购物车项目总数隐藏所有运输方式
【发布时间】:2019-05-22 05:07:50
【问题描述】:

当购物车中的总金额低于 40 美元时,我需要隐藏所有运输方式。 我基本上想禁用所有运输方式,因为我将为我所有区域的每个样品收取 4 美元的固定费用。

if ($_SESSION["sample_count"] == $_SESSION["cart_items_count"]){

    $sample_count = $_SESSION["sample_count"];
    $sample_shipping_cost = 4;
    $woocommerce->cart->add_fee( 'Samples Postage', ($sample_count * 4), true, '' );
    set_cart_contents_weight(0);    
}               

需要删除以下选项:

<td data-title="Transport Costs">
    <ul id="shipping_method">
        <li>
            <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_wbs10065ab3a2_delivery" value="wbs:10:065ab3a2_delivery" class="shipping_method"  checked='checked' />
            <label for="shipping_method_0_wbs10065ab3a2_delivery">Delivery: <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">&#36;</span>6.28</span></label>
        </li>
        <li>
            <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_local_pickup16" value="local_pickup:16" class="shipping_method"  />
            <label for="shipping_method_0_local_pickup16">Local pickup</label>
        </li>
    </ul>

【问题讨论】:

  • 运费:6.28 美元是由我想删除的基于重量的运输选项创建的。 weightbasedshipping.com

标签: php wordpress woocommerce hook-woocommerce shipping-method


【解决方案1】:

您可以使用以下方法,当购物车商品总数低于 $40 时,这将删除所有运输选项:

add_filter( 'woocommerce_cart_needs_shipping', 'show_hide_shipping_methods' );
function show_hide_shipping_methods( $needs_shipping ) {
    $cart_items_total = WC()->cart->get_cart_contents_total();

    if ( $cart_items_total < 40 ) {
        $needs_shipping = false;

        // Optional: Enable shipping address form
        add_filter('woocommerce_cart_needs_shipping_address', '__return_true' );
    }

    return $needs_shipping;
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2020-01-30
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多