This is How to Add a Custom Text When a Product is Sold Individually in WooCommerce
As an e-commerce business owner, you know the importance of providing a seamless and informative shopping experience for your customers. One aspect of this is managing how your products are displayed and sold on your WooCommerce store.
In some cases, you may want to sell certain products individually, rather than allowing customers to add multiple units to their cart. This is a common practice for products that are unique, limited in quantity, or require special handling.
When a product is set to be sold individually in WooCommerce, it's a good idea to let your customers know this upfront. This can help avoid any confusion or frustration during the checkout process.
Fortunately, WooCommerce provides a handy function that allows you to add a custom text message when a product is sold individually. In this article, we'll walk you through the step-by-step process of implementing this feature on your WooCommerce store.
Understanding the WooCommerce is_sold_individually()
Function
The WooCommerce is_sold_individually()
function is a built-in feature that allows you to determine whether a product can only be purchased one unit at a time. This function returns a boolean value (true
or false
) based on the product's settings.
By default, most products in WooCommerce are not set to be sold individually. However, you can enable this setting by navigating to the individual product's edit page in the WordPress admin area, and then checking the "Sold Individually" option under the "Inventory" section.
Once a product is set to be sold individually, you can use the is_sold_individually()
function to display a custom message on the single product page. This message can be used to inform customers about the product's unique selling conditions.
Step-by-Step Guide to Adding a Custom Text Message
To add a custom text message when a product is sold individually, you'll need to add the following code to your theme's functions.php
file:
add_action( 'woocommerce_single_product_summary', 'sold_individually_custom_text', 25 );
function sold_individually_custom_text() {
global $product;
if ( $product->is_sold_individually() ) {
echo '<p class="sold-individually">' . __("You can only buy one piece of this product", "woocommerce") . '</p>';
}
}
Let's break down the code:
-
add_action( 'woocommerce_single_product_summary', 'sold_individually_custom_text', 25 );
: This line hooks the sold_individually_custom_text()
function to the woocommerce_single_product_summary
action, which is triggered on the single product page. The 25
parameter sets the priority of the function, allowing you to control the order in which it's executed in relation to other actions on the same hook.
-
function sold_individually_custom_text() { ... }
: This is the function that will display the custom text message. It first retrieves the global $product
object, which represents the current product being displayed.
-
if ( $product->is_sold_individually() ) { ... }
: This conditional statement checks if the current product is set to be sold individually. If it is, the code inside the curly braces will be executed.
-
echo '<p class="sold-individually">' . __("You can only buy one piece of this product", "woocommerce") . '</p>';
: This line outputs the custom text message, which is wrapped in a <p>
tag with the class "sold-individually". The __()
function is used to make the text translatable.
By adding this code to your theme's functions.php
file, you'll see the custom text message displayed on the single product page for any products that are set to be sold individually.
Real-World Example and Customization
Let's say you're selling a unique, limited-edition art print on your WooCommerce store. You've set the product to be sold individually, as you only have a few copies available. In this case, you might want to display a message like "You can only purchase one copy of this limited-edition art print."
To customize the message, you can simply modify the text within the __()
function in the code above:
echo '<p class="sold-individually">' . __("You can only purchase one copy of this limited-edition art print", "woocommerce") . '</p>';
You can also adjust the CSS styles for the "sold-individually" class to match the design of your WooCommerce store. For example, you might want to change the font, size, or color of the text:
.sold-individually {
font-size: 14px;
color: #666;
font-style: italic;
}
By customizing the message and styling, you can ensure that the information displayed to your customers is both informative and visually appealing.
Conclusion
In this article, we've explored how to add a custom text message when a product is sold individually in WooCommerce. By using the built-in is_sold_individually()
function and hooking into the woocommerce_single_product_summary
action, you can easily display a message that informs customers about the unique selling conditions for certain products.
This feature can help improve the customer experience on your WooCommerce store, reducing confusion and frustration during the checkout process. Additionally, you can customize the message and styling to match the branding and design of your e-commerce website.
If you're looking to take your WooCommerce store to the next level and ensure a seamless shopping experience for your customers, be sure to consider implementing this custom text message feature. And remember, Flowpoint.ai can help you identify and fix any technical issues that may be impacting your conversion rates
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.