I’m trying to to switch the price for a ‘Sold’ label on the product page of products that are out of stock.
If a product is sold out, the price should be hidden and in its stead should be a ‘Sold’ label.
I figured out that the price is placed in catalog_product_view.xml and it calls upon the vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml
file, but I could not figure out where and how to bring in a condition to check whether product is sold out or not.
Can someone help here?
Thanks in advance.
Yuan
2
Answers
Hiding price for out of stock products in Magento 1.
RWD Theme
Change
To:
Default Theme:
Change
To:
OR
Hiding price for out of stock products in Magento 2.
On admin page, Click on Stores, then under the Setting section, choose Configuration.
In this page, you find the Inventory section under Catalog. Expand the Stock Options section and you can start to set the custom status of the product.
Before coming to the Out of stock product section, you need to enter this field.
Set Items’ Status to be in Stock When Order in Canceled: You choose YES when you want to return items to your stock if an order is canceled.
Decrease Stock When Order is Placed: if you want to adjust the quantity on hand when the order is placed, you choose Yes
Then we can come to the part that allows you to display or disable the product out of stock in Magento 2.
You want to display the product out of stock, you set the Display Out of Stock Products section is Yes. In contrast, set No if you want to disable it.
As I understand you have two parts to this issue
1) Hide price on Product-detail page if product is Out-of-stock
You can override the method using DI to below
$product->isAvailable() is the new condition that we have added
2) Show Sold label
outofstock label is shown by default, but if you still want to show create your block & template in vendor/magento/modul-catalog/view/frontend/layout/catalog_product_view.xml
Hope this helps