Question

I've extended \Magento\Catalog\Pricing\Render\FinalPriceBox and overwritten the function wrapResult, to get the styling I want for my product prices.

protected function wrapResult($html)
{
    if($this->getRequest()->getControllerName()=='category'){
        return '<div class="category-price-box ' . $this->getData('css_classes') . '" ' .
            'data-role="priceBox" ' .
            'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
            'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
            '>' . $html . '</div>';
    }else{
        return '<div class="product-price-box' . $this->getData('css_classes') . '" ' .
            'data-role="priceBox" ' .
            'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
            'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
            '>' . $html . '</div>';
    }
}

It works well on the category page. On my Product page though, I have 2 types of prices that I show. I have the price for the product, but I also have a collection of similar products that also have prices. What I want is that the collection of products should have the same styling as prices on the category pages. But the main product price should remain as it is. How can I add a parameter so I know in FinalPriceBox.php which class I should add?

The way I render the product price is:

use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Framework\Pricing\Render;

class ListProduct
{
    protected function getPriceRender()
    {
        return $this->getLayout()->getBlock('product.price.render.default')
            ->setData('is_product_list', true);
    }

    public function getProductPrice($p)
    {
        $priceRender = $this->getPriceRender();

        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                FinalPrice::PRICE_CODE,
                $p,
                [
                    'include_container' => true,
                    'display_minimal_price' => true,
                    'zone' => Render::ZONE_ITEM_LIST,
                    'list_category_page' => true
                ]
            );
        }

        return $price;
    }
}

Could I add a value after 'list_category_page', and if so how can I get it in FinalPriceBox.php ?

Était-ce utile?

La solution

I found the solution, by using setData in getPriceRender:

protected function getPriceRender()
{
    return $this->getLayout()->getBlock('product.price.render.default')
        ->setData('is_product_list', true)
        ->setData('is_similar_product', true);
}

And then checking it in wrapResult with getData:

protected function wrapResult($html)
{
    if(($this->getRequest()->getControllerName()=='category') || ($this->getData('is_more_from_category'))){
        return '<div class="category-price-box' . $this->getData('css_classes') . '" ' .
            'data-role="priceBox" ' .
            'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
            'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
            '>' . $html . '</div>';
    }else{
        return '<div class="product-price-box' . $this->getData('css_classes') . '" ' .
            'data-role="priceBox" ' .
            'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
            'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
            '>' . $html . '</div>';
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top