删除Woocommerce的“下订单"特定运输类别的按钮 [英] Remove Woocommerce "place order" button for a specific shipping class

查看:32
本文介绍了删除Woocommerce的“下订单"特定运输类别的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,需要删除Woo-commerce的结帐屏幕上的下订单"按钮.

I have a scenario where I need to remove the "Place order" button on the checkout screen for Woo-commerce.

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

Currently I have 2 shipping methods: Flexible shipping and Freight

如果客户在购物车中添加了一个运输类别为运费"的商品,我当前的代码将禁用该灵活运输方式,然后该运费方式将显示一条消息要求当前汇率".

If a customer adds an item with the shipping class of "Freight" to their cart, my current code disables the flexible shipping method and then the freight method displays a message of "Call for current rates".

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

The issue is that they can still checkout essentially without paying anything for shipping which is why if freight is the only shipping method available I need the place order button to be removed or replaced.

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

Here is the code I am currently using and trying to modify unsuccessfully:

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

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

Is there a simple hook or something I'm missing?

我已经把这个弄得一阵子了,撞墙了.

I've been messing with this for a while and am hitting a wall.

推荐答案

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

Try the following, that will output an inactive greyed "Place Order" order button when a specific shipping class is found in cart items:

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文件中.经过测试,可以正常工作.

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

要完全删除下订单"按钮,请改用以下类似方法:

To remove completely the "Place order" button, you will use this similar instead:

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文件中.经过测试,可以正常工作.

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

这篇关于删除Woocommerce的“下订单"特定运输类别的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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