• I’m not sure if this plugin is still supported or abandoned – it appears that anyone who asks just gets their comment thread closed so maybe we all have our answer?

    Support status for HPOS tables for WC have not been declared, and support for recent WP and WC versions has not been clarified which would be courteous to users.

    Pretty useful plugin though which is why I’m here to provide some assistance and suggestion for improvement.

    As some commenters have asked about there are some holes in this method – no shade on author, he’s just using a builtin hook in woo but implementing it nicely as a plugin.

    The holes are as stated about yith wishlist – and I can confirm also with CommerceGurus Commercekit wishlist function (and probably others) – that if a customer adds a “non-purchasable” product to their wishlist, then visits the wishlist page, they will be presented with an “add to cart” button, which dutifully and blindly bypasses the plugins intended purpose, and allows purchase of ostensibly ‘non-purchasable’ products.

    The second hole is of course that by inspecting a product page using developer tools in any browser, a sneaky customer could still retrieve the product ID (eg:59) and then visit “www.exampleshop.com/?add-to-cart=59&quantity=1” whereby the product will be added to cart anyway.

    Although I have personally patched my CommerceKit plugin to render a “Non-purchasable” button instead of rendering the add to cart button in the wishlist table, I thought I might also share a solution which may “head em off at the pass” so to speak.

    The below code will run a check when visiting the cart page and remove any “non-purchasable” products that may’ve been added to the cart via either of the above (or other) means.

    // This function checks for items that are non-purchasable but have been added to the cart via sneaky methods (wishlist plugs and or direct url additions).
    function check_for_sneaky_non_purchasable_products_added() {
            if ( WC()->cart->is_empty() ) {
                return;
            }
            $removed_products = [];
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $product_obj = $cart_item['data'];
    						$nonbuyablecheck = get_post_meta($product_obj->get_id(), '_not_buyable', yes);
                if ( $nonbuyablecheck == 'yes' ) {
                    WC()->cart->remove_cart_item( $cart_item_key );
                    $removed_products[] = $product_obj;
                }
            }
            if (!empty($removed_products)) {
                wc_clear_notices(); // remove any WC notice about sorry about out of stock products to be removed from cart.
                foreach ( $removed_products as $idx => $product_obj ) {
                    $product_name = $product_obj->get_title();
                    $msg = sprintf( __( "The product '%s' was removed from your cart because it is non-purchasable online. Not sure how you added it to cart, but it is now removed.", 'woocommerce' ), $product_name);
                    wc_add_notice( $msg, 'error' );
                }
            }
    }
    
    add_action('woocommerce_before_cart', 'check_for_sneaky_non_purchasable_products_added');
    
    add_action('woocommerce_review_order_before_cart_contents', 'check_for_sneaky_non_purchasable_products_added');

    The above code is a quick hacky solution, but is confirmed working as of time of writing. It can be implemented using a code snippet plugin, a standalone snippet plugin of you own making, or by adding to functions.php of your theme.

    I hope this might help out anyone scratching their head as to how a customer ordered a product which they defined as not buyable.

    Please use it with caution – if you’re not comfortable with code snippets at least, then i recommend passing it to a developer to implement in a safe way – you really don’t wanna be breaking cart or checkout pages so please be careful.

    Of course if anyone has additions to the code or suggestions or something I’ve missed please do reply and help everyone out – me included!

    Hope this helps someone, have a great day everyone! ??

    • This topic was modified 1 year, 3 months ago by gasserstance.
  • The topic ‘Still supported? Added checks recommended.’ is closed to new replies.