I don't think using layout you will easily add this block after price section.I guess that you are trying to add this block to every product
and, using Plugin
on the method getProductPrice()
and add a block after Price section on listing section.
So. create Around plugin on the function getProductPrice()
and your block output HTML to this function.
The di.xml
file in your module declares a plugin for a class object:
<?xml version="1.0"?>
<config>
<type name="Magento\Catalog\Block\Product\AbstractProduct">
<plugin name="add_custom_block" type="My\Module\Plugin\AddCustomBlock" sortOrder="1" disabled="false" />
</type>
</config>
Plugin Class:
<?php
namespace My\Module\Plugin;
class AddCustomBlock
{
public function afterGetProductPrice(
\Magento\Catalog\Block\Product\AbstractProduct $subject,
$output,
\Magento\Catalog\Model\Product $product
)
{
$customBlockHtml = '';
$name = 'banner_category_ptk_' . $product->getId();
$customBlock = $subject->getLayout()->getBlock($name);
if (!$subject->getLayout()->getBlock($name)) {
$customBlock = $subject->getLayout()->createBlock(
\Magento\Framework\View\Element\Template::class,
$name,
['product' => $product]
);
}
if ($customBlock) {
$customBlock->setTemplate('Vendor_Module::category/banner_category.phtml');
$customBlockHtml = $customBlock->toHtml();
}
return $output . $customBlockHtml;
}
}