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.
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>';
}
}