• Resolved allgoodnamesaretaken

    (@allgoodnamesaretaken)


    I want to prevent logged out users from purchasing a particular product on WooCommerce

    The code below prevents all products from being purchased.

    My question is how can I enable it for just one product or product category?

    // Replacing add-to-cart button in shop pages and archives pages (for non logged in users)
    add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
    function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
        if ( ! is_user_logged_in() ) {
                $link = get_permalink($product_id);
                        $button_text = __( "View product", "woocommerce" );
                                $html = '<a href="'.$link.'" class="button alt add_to_cart_button">'.$button_text.'</a>';
                                    }
                                        return $html;
                                        }
    // Avoid add to cart for non logged user (or not registered)
    add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
    function logged_in_customers_validation( $passed, $product_id, $quantity) {
        if( ! is_user_logged_in() ) {
                $passed = false;
            // Displaying a custom message
                    $message = __("You need to be logged in to be able to fund your EZYCoin wallet…", "woocommerce");
                            $button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
                                    $button_text = __("Sign in", "woocommerce");
                                            $message .= ' <a href="'.$button_link.'" class="login-register button" style="float:right;">'.$button_text.'</a>';
            wc_add_notice( $message, 'error' );
                }
                    return $passed;
                    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi there,

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

    Cheers.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution and the above resources for developers were helpful. If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Disabling Add to Cart Button for Specific WooCommerce Products’ is closed to new replies.