• We have an online woocommerce store aside from our physical store. My problem is that some products should not be available to buy online, they should only be available through our physical store. I still want to show them online however, and also show stock status along with variable prices.

    I’ve seen a lot of “catalog mode” plugins out there, however none of them does the job I am looking for. Basically here’s what I want to do:

    View post on imgur.com

    Have an option under general to disable add to cart button and quantity.

    When checked, the add to cart button will be removed from site on both archive pages and single product pages, including the quantity option. It will also add some information text instead, something like “This product is not available online, please contact us if you want to buy this item.”

    I have many different solutions, but it’s the code I have problems with 😉

    Here’s what I have today:

    // Add checkbox in admin
    add_action( 'woocommerce_product_options_general_product_data', 'woo_single_product_catalog_mode' );
    function woo_single_product_catalog_mode() {
       global $woocommerce, $post;
       echo '<div class="options_group">';
        woocommerce_wp_checkbox(
                    array(
                        'id' => 'custom_remove_add_to_cart',
                        'wrapper_class' => 'checkbox_class',
                        'label' => 'Disable add to cart',
                        'description' => 'Enable to remove quantity/add to cart and show a message instead'
                    )
                );
        echo '</div>';
    }
    
    // Save fields
    add_action( 'woocommerce_process_product_meta', 'woo_single_product_catalog_mode_save' );
    function woo_single_product_catalog_mode_save( $post_id ){
        $woo_checkbox = $_POST['custom_remove_add_to_cart'];
        if( !empty( $woo_checkbox ) ) {
            update_post_meta( $post_id, 'custom_remove_add_to_cart', 'yes' );
        }
        else {
            delete_post_meta( $post_id, 'custom_remove_add_to_cart' );
        }
    }

    All this code does is to add the option as shown in image, when checked it will add a custom field ‘custom_remove_add_to_cart’ with the value ‘yes’ and if unchecked it will remove that custom field. Next up is to add some functions if a product i checked.

    Since there doesn’t seem to be a way (at least not that I’ve found) to remove the add to cart button, we could add a class to the product post instead, and then by css simply hide it:

    .woocommerce .catalog-mode form.cart button.button,
    .woocommerce .catalog-mode form.cart div.quantity
    {display: none !important;}

    My trouble here is I don’t know how to create a function to add a class to the post if custom field value ‘custom_remove_add_to_cart’ is ‘yes’

    Next up I want to add some text instead of the add to cart button. I have some code ready, but again, I only want it to show if custom field value ‘custom_remove_add_to_cart’ is ‘yes’.

    // Add text instead
    add_filter('woocommerce_before_add_to_cart_button','woo_single_product_catalog_mode_front_text');
    function woo_single_product_catalog_mode_front_text($catalog_text) {
        if ( is_woocommerce()){
    ?>
    
    <div class="catalog-text">
    <p>This product is not available online, please contact us if you want to buy this item.</p>
    </div>
    <?php
    
    return $catalog_text;
    
    }
    else {
            return $catalog_text;
        }
    }

    I now it’s kinda complex to explain what I am trying to do here and I don’t know if this is the right forum to ask, so please tell me if I should ask for help anywhere else 😉

    https://ww.wp.xz.cn/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi Jimmy,

    I believe what you have here answers your own question. the only thing missing is the part where to play on the code to hide the button.

    You have two options actually. You can copy the add-to-cart.php template file from the WooCommerce template, and add it up to your own theme and change it there.

    The other solution would be to use the woocommerce_loop_add_to_cart_link filter, which is applied on that template file.

    Hope this helps!

    Thread Starter Jimmy

    (@jayem82)

    That’s my issue, I want to make this as a plugin rather than have it depend on template files.

    • I’m trying to create a function that automatically adds a class to the product post when my option is checked.
    • I also want to create a function that only adds the ‘text’ if my option is checked.

    But I’m pretty novice at php so I was looking for help 😉

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

The topic ‘Building a plugin for custom atalog mode, ideas?’ is closed to new replies.