Switching the Product Price for ‘Sold’ Label: A Comprehensive Guide
In Magento 2, managing product inventory is a critical aspect of e-commerce operations. One common requirement is to hide the price and display a ‘Sold Out’ label when a product goes out of stock. This guide will walk you through the process of achieving this.
Understanding the Price Display in Magento 2
Before diving into the implementation, it’s essential to understand where the price is displayed and how it can be customized.
The price display is managed primarily by two components:
-
XML Layout File: The layout file is responsible for defining which templates should be used to render specific elements on a product page. In this case, the final price is defined in the
catalog_product_view.xmlfile located under your theme directory (usually app/design/frontend/vendor/theme/Magento_Catalog/layout).<referenceBlock name="product.price.final" before="product.price.tier"> <arguments> <argument name="js_layout" xsi:type="string">price-final</argument> </arguments> </referenceBlock> -
Price Template File: The actual template file that renders the final price is
vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml. This file contains the PHP code responsible for displaying the product’s price.
Implementing the ‘Sold Out’ Condition
To switch the price for a ‘Sold’ label when a product is out of stock, you need to add a condition in the final_price.phtml template file. Here’s how to do it:
-
Locate the Template File: Navigate to your theme directory and find the
final_price.phtmlfile underMagento_Catalog/templates/product/price. Make a copy of this file for backup purposes. -
Add the ‘Sold Out’ Condition: Open the copied
final_price.phtmlfile in your favorite text editor or IDE. Look for the following line, which is responsible for displaying the final price:echo $block->getProduct()->getFinalPrice(); -
Implement the Condition: Add a condition to check if the product’s stock status is ‘out of stock’ (QTY > 0). If it is, display the ‘Sold Out’ label instead of the price. Here’s how you can do this:
$product = $block->getProduct(); if ($product->getStockItem()->getQty() <= 0) { echo '<span class="sold-out-label">Sold Out</span>'; } else { echo $block->getProduct()->getFinalPrice(); }
This code checks if the product’s stock quantity is less than or equal to zero. If it is, a ‘Sold Out’ label will be displayed; otherwise, the final price will be shown.
Conclusion
In this guide, we explored how to switch the product price for a ‘Sold Out’ label in Magento 2 when a product goes out of stock. By understanding the layout and template files responsible for displaying prices, you can implement this feature without touching any core Magento files.
Remember that customizing templates is an advanced task that requires careful testing to ensure the integrity of your store’s functionality. Always backup your theme directory before making changes and thoroughly test the implementation in a development environment before deploying it to a production site.
For more information on Magento 2 development best practices, check out our comprehensive guide at LaravelCompany.
Happy coding!