คุณสามารถเพิ่มไฟล์สร้าง saperate price สำหรับสินค้าและรหัสที่สามารถกำหนดค่าได้
catalog_product_prices.xml
เพิ่มรหัส
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="prices" xsi:type="array">
                    <item name="final_price" xsi:type="array">
                        <item name="render_class" xsi:type="string">Vendor\Module\Pricing\Render\FinalPriceBox</item>
                        <item name="render_template" xsi:type="string">Vendor_Module::product/price/final_price.phtml</item>
                    </item>
                </item>
            </argument>
            <argument name="configurable" xsi:type="array">
                <item name="prices" xsi:type="array">
                    <item name="final_price" xsi:type="array">
                        <item name="render_class" xsi:type="string">Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox</item>
                        <item name="render_template" xsi:type="string">Vendor_Module::product/price/final_price_configurable.phtml</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</layout>
มันเป็นสินค้าที่กำหนดไม่ได้ที่จะตรวจสอบด้วยและgetFinalPrice()getSpecialPrice()
เพิ่มรหัสด้านล่างสำหรับผลิตภัณฑ์ที่กำหนดค่าได้
$priceModel = $block->getPriceType('regular_price');
$finalPriceModel = $block->getPriceType('final_price');
<?php if($finalPriceModel->getAmount() < $priceModel->getAmount()) : ?>
        <span class="old-price sly-old-price no-display config-old" style="text-decoration: line-through;">
            <?= $block->renderAmount($priceModel->getAmount(), [
                'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
                'price_type'        => 'oldPrice',
                'include_container' => true,
                'skip_adjustments'  => true
            ]); ?>
        </span>
        <?php 
            $array = (array)$priceModel->getAmount();
            $prefix = chr(0).'*'.chr(0);
            $price = $array[$prefix.'amount'];
            $array = (array)$finalPriceModel->getAmount();
            $prefix = chr(0).'*'.chr(0);
            $finalPrice = $array[$prefix.'amount'];
            $percentage = 100 - round(($finalPrice / $price)*100);
            echo "<span class='percent-amt'>- ".$percentage."%</span>";
        ?>
    <?php endif; ?>
หมายเหตุ:คุณสามารถรับสิ่งนี้ได้โดยตรงจากไฟล์การเปลี่ยนแปลงapp\design\frontend\Vendor\theme\Magento_Catalog\templates\product\price\final_price.phtmlคุณเพียงแค่ใส่เงื่อนไขสำหรับผลิตภัณฑ์ที่สามารถกำหนดค่าได้
เปอร์เซ็นต์จะแสดงในหน้ารายการ 