1

In magento 2, I've created a recently viewed items using Magento Reports block. Now I need to show prices for only registered users.

I have 2 problems,

  1. price is rendered by following php code,

    echo $block->getProductPriceHtml(
        $_item,
        \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
        \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
        [
            'price_id_suffix' => '-' . $type
        ]
    );
    

When it renders, chrome inspector shows multiple div classes and spans, which makes my css obsolete. I need to just get the price value given from above block and put inside my own div block. How can I do that? From where this price comes from?

please help me

1

If your block extend "AbstractProduct"

(namespace Magento\Catalog\Block\Product)

Then you can use different Price Render or create your own, example found in the core code :

echo $block->getProductPriceHtml(
    $_product,
    \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
    \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW,
    [
        'price_id_suffix' => '_clone'
    ]
)


echo $block->getProductPriceHtml(
                            $_product,
                            \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
                            \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
                            [
                                'price_id_suffix' => '-widget-viewed-' . $suffix
                            ]
  • Hi, I dont know much about magento. I just need to get price and put inside my div tag. can you help me? Im a beginner. How should I put this into my code? – Joey Sep 22 '17 at 9:45
  • If you simply want the price, I recommand this post that will helps you getting it from the $product Model : magento.stackexchange.com/questions/149076/… – Neymir Sep 22 '17 at 9:59
  • in that question, they are talking about $this->_productFactory->create()->load($idProd); . I don't have it. Im using Magento Reports module from vendor and I just need the price – Joey Sep 22 '17 at 10:32
  • 1
    You already have your product in $item variable I assume. You don't need the factory. Call this : $item->getPriceInfo()->getPrice('final_price')->getValue(); – Neymir Sep 22 '17 at 10:58
  • yes. I get what I want from that code, but the thing is I need to apply some styes to it. from this php line, it renders with few divs and spans which I cant edit. so I need to just get the price to put inside my own div tag. for now I wrote some css targeting that auto generated div tags. but thats not what I need to do here – Joey Sep 22 '17 at 11:05

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.