0

I'm looking for a way to have a custom text attribute in place of the display label of the 'special price' in Magento 2. At the moment, our final_price file has the following

<span class="special-price">
    <?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(), [
        'display_label'     => __('Sale Price'),
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
</span>
<span class="old-price">
    <?= /* @noEscape */ $block->renderAmount($priceModel->getAmount(), [
        'display_label'     => __('WAS'),
        'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
        'price_type'        => 'oldPrice',
        'include_container' => true,
        'skip_adjustments'  => true
    ]); ?>
</span>

Which shows the products with "Was £xxx Sale Price £xxx" as follows

enter image description here

However, some of our products are not in the sale but are clearance pieces and we would like these pieces to show "Was £xxx Clearance Price £xxx", while still showing the "Sale Price £xxx" text on the other 99% of the products in our catalog. When we are not 'in sale', some of our products will need to say "Was £xxx NOW £xxx", some will need to say "Was £xxx Extra 20% off £xxx" and some will need to say "Was £xxx Clearance Price £xxx".

I came up with the idea that probably the easiest way to handle this is to have a custom attribute 'price_text' set up, with options such as "Sale Price", "Clearance Price", "Extra 20% off" etc set up, and I can bulk-apply these to the products that require them.

Is anyone able to give me any pointers on how to include this custom attribute in place of "Sale Price" in the below section please? Thank you so much!

<span class="special-price">
    <?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(), [
        'display_label'     => __('Sale Price'),
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
</span>
1

2 Answers 2

0
$product = $block->getSaleableItem();
$customAttributeValue = $product->getCustomAttribute();

Make sure custom attribute is visible in catalog pages.

0

Hi piyush systematix sorry if I'm being dim but would the below be correct? Thank you!

<span class="special-price">
    <?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(), [
    'display_label'     => $product = $block->getSaleableItem(); $customAttributeValue = $product->getCustomAttribute();,
    'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
    'price_type'        => 'finalPrice',
    'include_container' => true,
    'schema' => $schema
]); ?>
</span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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