Hi Guys
We have some products with out of stock, but the old price is still there. How can we hide the price and write " Call for price " instead?
Try following way for changing product detail page.
app/code/SR/MagentoStackExchange/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="product.price.final"> <arguments> <argument name="price_render" xsi:type="string">product.price.render.default</argument> <argument name="price_type_code" xsi:type="string">final_price</argument> <argument name="zone" xsi:type="string">item_view</argument> <argument name="product_view" xsi:type="boolean">true</argument> </arguments> </referenceBlock> </body> </page>
app/code/SR/MagentoStackExchange/Pricing/Render.php
<?php namespace SR\MagentoStackExchange\Pricing; use Magento\Framework\Registry; use Magento\Framework\View\Element\Template; class Render extends \Magento\Catalog\Pricing\Render { private $productView; /** * Construct * * @param Template\Context $context * @param Registry $registry * @param array $data */ public function __construct( Template\Context $context, Registry $registry, array $data = [] ) { $this->productView = isset($data['product_view']) ?: false; parent::__construct($context, $registry, $data); } /** * Produce and return block's html output * * @return string */ protected function _toHtml() { if ($this->productView) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->registry->registry('current_product'); if (!$product->isAvailable()) { return __('Call for price'); } } return parent::_toHtml(); } }
app/code/SR/MagentoStackExchange/etc/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Pricing\Render" type="SR\MagentoStackExchange\Pricing\Render"/> </config>
For listing, overwrite vendor/magento/module-catalog/view/frontend/templates/product/list.phtml and modify
<?php if ($_product->isAvailable()):?> <?= /* @escapeNotVerified */ $block->getProductPrice($_product) ?> <?php else: ?> <?= __('Call for price')?> <?php endif ?>
Hi again
Thanks, but what is App/code/SR? I dont have any folder called SR
SR/MagentoStackExchange this is a new module under app/code directory. So creating a new module you need to add registration.php and module.xml as well.
app/code/SR/MagentoStackExchange/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="SR_MagentoStackExchange" setup_version="2.0.0"/> </config>
app/code/SR/MagentoStackExchange/registration.php
<?php use \Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'SR_MagentoStackExchange', __DIR__);