1

I have working a website with others extensions between them the "Ultimo - Fluid Responsive" theme that overwrite much part of website, as well as my own extension.

My question friends:

  1. I how can get all products(prices) before to be renderized? I need get this collection for send it to my webservice and make some calculate.
  2. I thinked in get the products of each block/widget. Is optimal? How can overwrite all product's prices of any block/widget?

Here part of my code

Multicalculate Class: (truncated)

namespace MyExtension\Checkout\Plugin\Product;

class MultiCalculatePrice
{
    ...
    const MULTI_CALCULATE_PRICE_RESULT_RESPONSE = 'multi_calculate_result';
    ...

    public function __construct(
        ...
        \MyExtension\Checkout\Block\Product\AfterPrice $productBlock,
        \Psr\Log\LoggerInterface $logger,
        ...
    ) {
        $this->productBlock = $productBlock;
    }

    public function beforeGetLoadedProductCollection($subject)
    {
        ...
    }

    public function afterGetLoadedProductCollection($subject, $collection)
    {
        foreach ($collection as $item) {
            $dataProduct = $this->productBlock->getDataProduct($item);

            $product = new MyExtension\Checkout\Sdk\Entities\Product();
            $product->setId($dataProduct['id']);
            $product->setSku($dataProduct['sku']);
            $product->setName($dataProduct['title']);
            $product->setPrice($dataProduct['price']);
            $product->setImg($dataProduct['image']);
            ...
            $products[] = $product;
        }

        $wsclient = $this->dataHelper->getClient();
        $resultResponse = $wsclient->getCatalog()->multiCalculate($products);

        $this->coreRegistry->register(self::MULTI_CALCULATE_PRICE_RESULT_RESPONSE, $resultResponse);

        return $collection;
    }
}

AfterPrice Class: (truncated)

namespace MyExtension\Checkout\Plugin\Product;

use \Magento\Catalog\Pricing\Price\ConfiguredPrice;
class AfterPrice
{
    private $priceCode;

    public function beforeRender(
        \Magento\Framework\Pricing\Render $subject,
        $priceCode,
        \Magento\Catalog\Model\Product $saleableItem,
        array $arguments = []
    ) {
        $this->coreRegistry->unregister(self::PRODUCT_REGISTRY_KEY);
        $this->coreRegistry->register(self::PRODUCT_REGISTRY_KEY, $saleableItem);
        $this->priceCode = $priceCode;
        ...
    }

    public function afterRender(\Magento\Framework\Pricing\Render $subject, $renderHtml) {
        $product = $this->coreRegistry->registry(self::PRODUCT_REGISTRY_KEY);
        if ($this->priceCode === \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE) {
            return '<div data-product-id="' . $product->getId() . '">' . $this->priceCode . '</div>'; 
        } else {
            return $this->priceCode;
        }
    }
}

Here my di.xml

<config>
    ...
    <type name="Magento\Framework\Pricing\Render">
        <plugin name="myextension_product_priceRender" type="MyExtension\Checkout\Plugin\Product\AfterPrice" />
    </type>
    ...
    <type name="Magento\CatalogWidget\Block\Product\ProductsList">
        <plugin name="myextension_widget_productslist" type="MyExtension\Checkout\Plugin\Product\MultiCalculatePrice" />
    </type>
    ...
</config>
  • With "MultiCalculatePrice" class I try get all product collection for send it to my webservice. But it work getting products from my first block only, no for others.
  • With "AfterPrice" class I try update the price before of renderized of each product for any block. Work renderized any products of any
    block.

Here an screenshot of HomePage with my three blocks:

HomePage Blocks

The Block's definitiion within Home Page: enter image description here

2
  • you mean to whatever rails are getting load on home before that you want to make some call and want change price of respective product. right? Dec 28 '17 at 2:56
  • Correct, i need capture the products collection of each block (or of all blocks, preferable) and after render the new product-price executing the AfterPrice class. My main problem is that I can't capture the products collection of each block. Thanks Pramod! Dec 28 '17 at 5:19

Your Answer

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

Browse other questions tagged or ask your own question.