4

all can you help me how to override a product's special price? I want to unset Special Price if that product does not meet my condition.

I was able to override Price to my custom value, but if there is any special price exists for that product, then the special price is showing so I can't set my custom price for that product.

  • 1
    I would save a special price on product save after event. – Adarsh Khatri Sep 17 at 4:32
  • I can't update price value to table for that product, my condition will work daily basis. – Sanjun Dev Sep 17 at 4:34
  • Then have cron job to save price for the products matching your condition. – Adarsh Khatri Sep 17 at 6:38
1

First Create ‘Module.xml’ file at below location using following code. It will help to override our custom layout file. Path: app\code\OSCP\ModuleName\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="OSCP_ModuleName" setup_version="1.0.0" active="true">
        <sequence>
            <module name="Magento_Catalog"/>
            <module name="Magento_CatalogWidget"/>
            <module name="Magento_Widget"/>
        </sequence>
    </module>
</config>

Now it’s time to create our own custom layout at following path using this code. Path: app\code\OSCP\ModuleName\view\base\layout\catalog_product_prices.xml

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
                <item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item>
                <item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item>
                <item name="default_amount_render_template" xsi:type="string">OSCP_ModuleName::catalog/product/price/amount/default.phtml</item>
                <item name="prices" xsi:type="array">
                    <item name="special_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </block>
</layout>

Lastly, we have to create one more file ‘default.phtml’ with our logic of overriding default price. Path: app\code\OSCP\ModuleName\view\base\templates\catalog\product\price\amount\default.phtml

<?php
$objectManagerIn = \Magento\Framework\App\ObjectManager::getInstance();
$priceArr = explode('-', $block->getPriceId());
foreach ($priceArr as $item)
{
    if(is_numeric($item)){
        $pid=$item;
        break;
    }
}
$product = $objectManagerIn->get('Magento\Catalog\Model\ProductFactory')->create()->load($pid);
$priceHelper = $objectManagerIn->create('Magento\Framework\Pricing\Helper\Data'); ?>
<span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
    <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
<?php if ($block->getDisplayLabel()): ?>
    <span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
<?php endif; ?>
    <?php $ finalprice =$product->getFinalPrice();
        $finalprice+=(($finalprice*15)/100); ?>
    <span class="price-label">
<h4>    <b><?php echo $priceHelper->currency($finalprice, true, false);  ?></b>
</h4>
</span>
</span>

You can change $finalprice as per your requirement of price. Also, you can manipulate with these codes according to your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.