【问题标题】:Remove Woocommerce "place order" button for a specific shipping class删除特定运输类别的 Woocommerce“下订单”按钮
【发布时间】:2018-10-25 12:44:56
【问题描述】:

我有一个场景,我需要删除 Woo-commerce 结帐屏幕上的“下订单”按钮。

目前我有两种运输方式:灵活运输和货运

如果客户将运输类别为“Freight”的商品添加到他们的购物车,我当前的代码会禁用灵活运输方式,然后运费方式会显示“Call for current rates”消息。

问题是他们基本上仍然可以在不支付任何运费的情况下结帐,这就是为什么如果运费是唯一可用的运输方式,我需要删除或更换下订单按钮。

这是我目前正在使用并尝试修改失败的代码:

add_filter( 'woocommerce_package_rates', 'wc_hide_free_shipping_for_shipping_class', 10, 2 );

function wc_hide_free_shipping_for_shipping_class( $rates, $package ) {
    $shipping_class_target = 332; 
    $in_cart = false;

    foreach( WC()->cart->cart_contents as $key => $values ) {
        if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
        } 
    }
    if( $in_cart ) {
        unset( $rates['flexible_shipping_7_2'] );
    }
    return $rates;
}

是否有一个简单的钩子或我缺少的东西?

我已经搞砸了一段时间,现在正在碰壁。

【问题讨论】:

    标签: php wordpress woocommerce checkout shipping


    【解决方案1】:

    尝试以下操作,当在购物车物品中找到特定的运输类别时,将输出一个无效的灰色“下订单”订单按钮:

    add_filter('woocommerce_order_button_html', 'inactive_order_button_html' );
    function inactive_order_button_html( $button ) {
        // HERE define your targeted shipping class
        $targeted_shipping_class = 332;
        $found = false;
    
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
                $found = true; // The targeted shipping class is found
                break; // We stop the loop
            }
        }
    
        // If found we replace the button by an inactive greyed one
        if( $found ) {
            $style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important;"';
            $button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
            $button = '<a class="button" '.$style.'>' . $button_text . '</a>';
        }
        return $button;
    }
    

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


    要完全删除“下订单”按钮,您将使用类似的代替:

    add_filter('woocommerce_order_button_html', 'remove_order_button_html' );
    function remove_order_button_html( $button ) {
        // HERE define your targeted shipping class
        $targeted_shipping_class = 332;
        $found = false;
    
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            if( $cart_item['data']->get_shipping_class_id() == $targeted_shipping_class ) {
                $found = true; // The targeted shipping class is found
                break; // We stop the loop
            }
        }
    
        // If found we remove the button
        if( $found )
            $button = '';
    
        return $button;
    }
    

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

    【讨论】:

    • 感谢您的详细帮助!我一直遇到的问题似乎要么是“下单”按钮,要么什么都没有,这绝对有助于澄清我做错了什么!
    猜你喜欢
    • 2019-12-10
    • 2021-06-22
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多