• Resolved sydneywu

    (@sydneywu)


    Hi, I am using the following code so users have to log in to purchase products.

    However, certain products (very randomly) still become non-purchasable for log-in users. This means the “add to cart” button is unable to click (login users). When this happens, I’ll refresh the page and then the “add to cart” button will work. How do I fix this problem?

    Because I couldn’t find a way to hide the “add to cart” button for non-login users on a product page. That’s why I am using the following code.

    Hope anyone can help me, thank you.

    add_filter(‘woocommerce_is_purchasable’, ‘my_woocommerce_is_purchasable’, 10, 2);
    function my_woocommerce_is_purchasable($is_purchasable, $product) {

    $isLoggedIn = is_user_logged_in();
    if(true == $isLoggedIn){
    //Make product purchasable to logged in user
    return true;
    }

    //Make product not purchasable to unlogged in user
    return false;
    }

    https://glasslab303.com/wp-content/uploads/2021/06/purchasable.jpg
    https://glasslab303.com/wp-content/uploads/2021/06/nonpurchasable.jpg

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there 👋

    Upon searching on the internet, I could find a custom PHP code that will allow you to hide the Add to Cart button to non-login users. I’ve tried it and it worked great! Here is a direct link to the code:

    https://stackoverflow.com/a/59118057/15592226

    After applying the code, it should look like this:

    https://www.screencast.com/t/woNYGOBVVr

    And, it looks like this when clicking on the read more button:

    https://www.screencast.com/t/As2mppVZ

    Also, you could use the Catalog Visibility Options plugin for this and much more!

    https://docs.woocommerce.com/document/catalog-visibility-options/

    Cheers! 🙂

    Thread Starter sydneywu

    (@sydneywu)

    Hi, thanks for the message.

    I have tried the code.

    This code seems to only work for single product (no variations). The “add to cart” button did disappear when I am logout.

    However, I also have products with variations, the “add to cart” button is still there.

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    However, I also have products with variations, the “add to cart” button is still there.

    Yes, it shows the add to cart button for variations, but it doesn’t allow you to purchase the item. It will display a prompt “Sorry, this product is unavailable.”

    https://www.screencast.com/t/3QKBPlpVY

    Perhaps, there is some kind of conflict going on your site that prevents that the code works for the variable products.

    Well, this is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.`

    If you do require more help with the actual coding, we’d recommend you hire a developer who can take a look at this, quote you for their services, and help you add this feature to your site. We’d recommend getting in touch with a web developer or one of the customization experts listed at https://woocommerce.com/customizations/.

    Cheers.

    Thread Starter sydneywu

    (@sydneywu)

    Okay, Thank You!

    Thread Starter sydneywu

    (@sydneywu)

    solved.

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We are happy to help 😃

    Great! If you have any further questions, you can start a new thread.

    Cheers!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘custom code caused conflict’ is closed to new replies.