admin管理员组文章数量:1389836
I created (copied) a custom post status for my old/discontinued product in my function.php and everything is working fine.
Now, I want to make those old product unpurchasable by removing the cart button based and the status product, in this case "expired".
I think i have to use get_post_status()
and
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
or by using $is_purchasable=false
.
But to be frank, i'm a begginer and i don't know how i can create it from scratch...
Edit :
I tried two different options but none of them work...
function remove_cart_button_expired_product (){
global $product;
$id = get_the_ID();
$post_status = get_post_status($id);
if ($post_status === 'expired') {
//echo $id; // working
//echo $post_status; // working
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart',1);
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',1);
}
}
add_action('wp', 'remove_cart_button_expired_product');
This one doesn't work maybe because of my Divi theme...
function product_not_purchasable(){
global $product;
$id = $product->get_id();
$post_status = get_post_status($id);
if ($post_status === 'expired') {
//echo $id; // working
//echo $post_status; // working
$purchasable = false;
return $purchasable;
}
}
add_filter('woocommerce_is_purchasable', 'product_not_purchasable');
I saw that this line of code is preferable over the precedent one but it brokes my website when i try to add a product in the cart (no mather the post status ).
I created (copied) a custom post status for my old/discontinued product in my function.php and everything is working fine.
Now, I want to make those old product unpurchasable by removing the cart button based and the status product, in this case "expired".
I think i have to use get_post_status()
and
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
or by using $is_purchasable=false
.
But to be frank, i'm a begginer and i don't know how i can create it from scratch...
Edit :
I tried two different options but none of them work...
function remove_cart_button_expired_product (){
global $product;
$id = get_the_ID();
$post_status = get_post_status($id);
if ($post_status === 'expired') {
//echo $id; // working
//echo $post_status; // working
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart',1);
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',1);
}
}
add_action('wp', 'remove_cart_button_expired_product');
This one doesn't work maybe because of my Divi theme...
function product_not_purchasable(){
global $product;
$id = $product->get_id();
$post_status = get_post_status($id);
if ($post_status === 'expired') {
//echo $id; // working
//echo $post_status; // working
$purchasable = false;
return $purchasable;
}
}
add_filter('woocommerce_is_purchasable', 'product_not_purchasable');
I saw that this line of code is preferable over the precedent one but it brokes my website when i try to add a product in the cart (no mather the post status ).
Share Improve this question edited Oct 10, 2019 at 19:44 Kentin.R asked Sep 24, 2019 at 8:41 Kentin.RKentin.R 13 bronze badges1 Answer
Reset to default 0Finally find my solution after a few day.
In case someone try to do the same
function test_expired_product(){
$id = get_the_ID();
$post_status = get_post_status($id);
$terms= array('cursus-annuel');
if ($post_status === 'expired' || $post_status === 'canceled') {
//echo $id; // working
//echo $post_status; // working
add_filter('woocommerce_is_purchasable', '__return_false');
add_action( 'woocommerce_single_product_summary', 'expired_product_message', 20 );
return;
}
if( has_term( $terms, 'product_cat' ) ){
add_filter('woocommerce_is_purchasable', '__return_false');
add_action( 'woocommerce_single_product_summary', 'unpurchasable_product_message', 20 );
return;
}
}
add_action('wp','test_expired_product');
function expired_product_message() {
echo '<p style="color:#e00000;">Ce produit n\'est plus disponible.</p>';
}
function unpurchasable_product_message() {
echo '<p style="color:#e00000;">Ce produit ne peut pas être acheté en ligne. Veuillez nous contacter afin de reserver ce cours.</p>';
}
本文标签: customizationWoocommerceRemove cart button depending product (post) status
版权声明:本文标题:customization - Woocommerce - Remove cart button depending product (post) status 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744707081a2620903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论