[英]Woocommerce Add to Cart Condition
I recently implement the code in function.php to redirect url when the price is 0 but it seems its only lacking and the price condition is not working.我最近在 function.php 中实现了在价格为 0 时重定向 url 的代码,但它似乎只是缺少并且价格条件不起作用。 See my codes.
看我的代码。
/**
* Set a custom add to cart URL to redirect to
* @return string
**/
function custom_add_to_cart_redirect() {
$_product = wc_get_product( $product_id );
if( $product->get_price() == 0 || $product->get_price() == '') {
return 'http://www.nichegas.ads-tracker.com/contact/';
}
}
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );
It seems I am lacking on some code or something.看来我缺少一些代码或其他东西。 Our objective are: 1. IF price is 0 it will redirect to out custom url 2 also when click the add to card and its zero its will not add the item to the cart.
我们的目标是: 1. 如果价格为 0,它将重定向到自定义 url 2 也当点击添加到卡片时,它的零不会将项目添加到购物车。 How to disable the addition of product?
如何禁用添加产品?
Your help will very much appreciated.您的帮助将不胜感激。 Have a nice day!
祝你今天过得愉快!
UPDATE更新
Alternatively you can use also
woocommerce_add_to_cart
hook instead, and **here is possible to remove the freshly added item, this way:或者,您也可以使用
woocommerce_add_to_cart
钩子代替,**这里可以删除新添加的项目,如下所示:
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 10, 2 );
function custom_add_to_cart( $cart_item_key, $product_id ) {
$_product = wc_get_product($product_id);
$_product_price = $_product->get_price();
if( $_product_price == 0 || empty($_product_price) ) {
// Removing this freshly added cart item
WC()->cart->remove_cart_item($cart_item_key);
// Redirecting to some url
wp_redirect( 'http://www.nichegas.ads-tracker.com/contact/' );
exit;
}
}
Code goes in function.php file of your active child theme (or theme).代码位于活动子主题(或主题)的 function.php 文件中。 Or also in any plugin php files.
或者也可以在任何插件 php 文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.