Remove Woocommerce “place order” button for a spec

2019-08-02 10:23发布

问题:

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

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.

回答1:

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

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

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