Sye*_*Ali 4 php wordpress checkout categories woocommerce
想知道是否有人可以帮助我自定义此代码。我想更改此代码中的应用条件:
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;
}
Run Code Online (Sandbox Code Playgroud)
对于属于 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;
}
Run Code Online (Sandbox Code Playgroud)
此代码位于您的插件文件或活动子主题或主题的 function.php 文件中
参考:
| 归档时间: |
|
| 查看次数: |
3380 次 |
| 最近记录: |