0

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 ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.