WooCommerce Checkout url 挂钩 - 根据产品类别更改条件

WooCommerce Checkout url hook - Change condition based on product category

想知道是否有人可以帮助我自定义此代码。我想更改此代码中的应用条件:

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

对于属于 WooCommerce 中某个类别的产品,是否可以进行自定义结帐 url?

2020 年更新:仅适用于 WooCommerce 3+

是的,有可能,做一些改变:

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

此代码位于您的插件文件或您的活动子主题或主题的 function.php 文件中

参考文献: