In a recent Genesis customization project I completed, the requirement was to be able to mark specific products as “Not (yet) available for sale” when adding/editing the product.
Additionally, the standard “Add to Cart” buttons on the product archives like the main Shop page should show a custom “Call to Order” text and the single product pages should show custom message along the lines of “To order this product, give us a call at so and so number”.
In this tutorial, I share the details of how the above can be set up in WooCommerce.
But first, screenshots.
Product being edited:
WooCommerce archive pages like Shop:
Single product page:
All the code below goes in child theme’s functions.php
Step 1
Add Not Ready to Sell
field (checkbox) in the Product data’s General tab.
add_action( 'woocommerce_product_options_general_product_data', 'custom_general_product_data_custom_fields' );
/**
* Add `Not Ready to Sell` field in the Product data's General tab.
*/
function custom_general_product_data_custom_fields() {
// Checkbox.
woocommerce_wp_checkbox(
array(
'id' => '_not_ready_to_sell',
'wrapper_class' => 'show_if_simple',
'label' => __( 'Not Ready to Sell', 'woocommerce' ),
'description' => __( '', 'woocommerce' )
)
);
}
Step 2
Save the state of the checkbox by updating the value of _not_ready_to_sell
meta key (custom field) when the product is saved/updated.
add_action( 'woocommerce_process_product_meta', 'custom_save_general_proddata_custom_fields' );
/**
* Save the data values from the custom fields.
* @param int $post_id ID of the current product.
*/
function custom_save_general_proddata_custom_fields( $post_id ) {
// Checkbox.
$woocommerce_checkbox = isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_not_ready_to_sell', $woocommerce_checkbox );
}
Step 3
Mark the “Not ready to sell” products as not purchasable.
add_filter( 'woocommerce_is_purchasable', 'custom_woocommerce_set_purchasable' );
/**
* Mark "Not ready to sell" products as not purchasable.
*/
function custom_woocommerce_set_purchasable() {
$not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell', true );
return ( 'yes' === $not_ready_to_sell ? false : true );
}
Step 4
This will change the text of non-purchasable products’ buttons’ (on WooCommerce archives) to Read more
.
To change the button text to Call to Order
, add
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_product_add_to_cart_text' );
/**
* Change "Read More" button text for non-purchasable products.
*/
function custom_product_add_to_cart_text() {
$not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell', true );
if ( 'yes' === $not_ready_to_sell ) {
return __( 'Call to Order', 'woocommerce' );
} else {
return __( 'Add to cart', 'woocommerce' );
}
}
Step 5
Add instructions for non-purchasable products on the single product pages.
add_action( 'woocommerce_single_product_summary', 'custom_woocommerce_call_to_order_text', 30 );
/**
* Add calling instructions for non-purchasable products.
*/
function custom_woocommerce_call_to_order_text() {
$not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell', true );
if ( 'yes' === $not_ready_to_sell ) {
echo '<p class="content-box-blue">Call to order this product. <strong>123-456-7890</strong>.</p>';
}
}
References:
https://www.speakinginbytes.com/2016/05/add-call-order-button-woocommerce/
http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
https://docs.woocommerce.com/document/change-add-to-cart-button-text/
Cool, will this register the click as an order in WooCommerce > Orders?
Or just some other stats in Woo, salesstats etc.?
Thank you for this solution, this is what i need!
Would it be possible to add this functionality to product variations so that if specific variations were selected on the product page the call to order information would show instead of the add to cart button?
I would love it too if this could be added to variable products. Any plans to expand this to include variable products?
Been scratching my head for a solution and this was perfect! Thanks
This is brilliant – added exactly the functionality I needed to my online shop
You are a life saver. Thank you!
Fantastic method for those of us who aren’t afraid of a little code. Thanks!
Any option to do this for categorys?
This worked perfectly. Thanks so much!
Hello, I tried it and it works! thanks!
But now that I translated the website with WPML, the translated product is still for sale (the original one is fine). How can I make the translated product “non-purchasable”?
Thanks, for your help.