skip to Main Content

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


  1. Hiding price for out of stock products in Magento 1.

    RWD Theme

    app/design/frontend/rwd/template/catalog/product/view.phtml
    

    Change

    <div class="price-info">
        <?php echo $this->getPriceHtml($_product); ?>
        <?php echo $this->getChildHtml('bundle_prices') ?>
        <?php echo $this->getTierPriceHtml() ?>
    </div>
    

    To:

    <?php if($_product->isSaleable()): ?>
        <div class="price-info">
            <?php echo $this->getPriceHtml($_product); ?>
            <?php echo $this->getChildHtml('bundle_prices') ?>
            <?php echo $this->getTierPriceHtml() ?>
        </div>
    <?php endif; ?>
    

    Default Theme:

    appdesignfrontendbasedefaulttemplatecatalogproductviewtypedefault.phtml
    

    Change

    <?php echo $this->getPriceHtml($_product) ?>
    

    To:

    <?php if($_product->isSaleable()): ?>
        <?php echo $this->getPriceHtml($_product) ?>
    <?php endif; ?>
    

    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.

    enter image description here

    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.

    Login or Signup to reply.
  2. As I understand you have two parts to this issue

    1) Hide price on Product-detail page if product is Out-of-stock

    • Prices are defined in vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml
    • called at class: MagentoCatalogPricingRender and method: _toHtml()
    • You can override the method using DI to below

      
          protected function _toHtml()
          {
              /** @var PricingRender $priceRender */
              $priceRender = $this->getLayout()->getBlock($this->getPriceRender());
              if ($priceRender instanceof PricingRender) {
                  $product = $this->getProduct();
                  if ($product instanceof SaleableInterface && $product->isAvailable()) {
                      $arguments = $this->getData();
                      $arguments['render_block'] = $this;
                      return $priceRender->render($this->getPriceTypeCode(), $product, $arguments);
                  }
              }
              return parent::_toHtml();
          }
      
    • $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

      • and $product->isAvailable() function to check product’s availability

    Hope this helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search