4

Wondering if anyone could help me to customize this code. I would like to change the applied condition in this code:

add_filter( 'woocommerce_get_checkout_url', 'change_checkout_url', 30 );
function change_checkout_url( $url ) {
    $allowed_countries = array('NO');
    $customer_country = WC()->customer->get_default_country();
    if( !in_array( $customer_country , $allowed_countries ) ) {
        $url = wc_get_page_permalink( 'checkout' );
    }
    return $url;
}

Is it possible instead, for products that belongs to some category in WooCommerce, to have a custom checkout url?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Syed Reza Ali
  • 65
  • 2
  • 5

1 Answers1

6

2020 Update: Only for WooCommerce 3+

Yes it's possible, making some changes:

add_filter( 'woocommerce_get_checkout_url', 'custom_checkout_url', 30 );
function custom_checkout_url( $checkout_url ) {

    // Define your product categories (term Id, slug or name)
    $categories = array('Cat name1', 'Cat name2'); 
    $custom_url = 'http://my_custom_url.com/checkout/'; // <= custom URL

    $cart_items = WC()->cart->get_cart();

    if ( sizeof($cart_items) > 0 ) {
        foreach ( $cart_items as $cart_item ) {
            if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
                 return $custom_url;
            }
        }
    }
    return $checkout_url;
}

This code goes in your plugin file or on function.php file of your active child theme or theme

References:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Great! Exactly what I am looking for. Unfortunately it's not working as some of the code is deprecated or removed. I tried to update but I'm still not getting it to work. [Change Checkout Url in WooCommerce depneding on cart-content (product category)](https://stackoverflow.com/questions/62465027/change-checkout-url-in-woocommerce-depending-on-cart-content-product-category) – Romeo Patrick Jun 21 '20 at 16:37
  • Works well. Thank you – Romeo Patrick Jun 22 '20 at 18:51