WooCommerce结账URL挂钩 - 根据产品类别更改条件

4

我希望有人能够帮助我定制这段代码。我想要更改此代码中应用的条件:

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某个类别的产品,是否可能有自定义结账网址?

1个回答

6

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文件中

参考资料:


1
太好了!正是我想要的。不幸的是,由于某些代码已被弃用或删除,它无法正常工作。我尝试更新,但仍然无法使其正常工作。根据购物车内容(产品类别)更改WooCommerce中的结帐网址 - Romeo Patrick
工作良好。谢谢。 - Romeo Patrick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接