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:
- 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.
- 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: