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

1 Answer 1

0

Finally 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>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.