dongzong8110 2019-05-22 05:07
浏览 168
已采纳

在WooCommerce中隐藏基于购物车商品总数的所有送货方式

I need to hide all shipping methods when total in cart is below $40. I basically want to disable all shipping methods as I will be charging a $4 flat fee for each sample for all my zones.

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);    
}               

need to remove the options below:

<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>
  • 写回答

1条回答 默认 最新

  • doudansui6650 2019-05-22 07:49
    关注

    You can use the following, that will remove all shipping options when cart items total is under $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;
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备