繁体   English   中英

删除特定运输类别的Woocommerce“下订单”按钮

[英]Remove Woocommerce “place order” button for a specific shipping class

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

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

如果客户在购物车中添加了一个运输类别为“ Freight”的商品,我当前的代码将禁用该灵活的运输方式,然后该货运方式将显示一条消息“ Call for current rate”。

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

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

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

有没有简单的钩子或我缺少的东西?

我已经把这个弄乱了一段时间,撞墙了。

尝试以下操作,当在购物车项目中找到特定的运输类别时,该按钮将输出不活动的灰色“下订单”订购按钮:

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文件中。 经过测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM