I have following model want to override,
vendorAheaddReviewsModelProductResolver
I did create the di.xml file and execute following commands.
bin/magento setup:di:compile
bin/magento cache:flush
bin/magento cache:clean
rm -rf generated/code
rm -rf pub/static/*
rm -rf var/*
Here is the code I override
<?php
namespace ABCAReviewsModelProduct;
use MagentoCatalogApiDataProductInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkRegistry;
use MagentoFrameworkAppRequestInterface;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCatalogModelProduct;
use MagentoCatalogHelperOutput;
use MagentoFrameworkExceptionLocalizedException;
/**
* Class Resolver
*
* @package ABCAReviewsModelProduct
*/
class Resolver
{
/**
* @var RequestInterface
*/
private $request;
/**
* @var Registry
*/
private $registry;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var Output
*/
private $outputHelper;
/**
* @param Registry $registry
* @param RequestInterface $request
* @param ProductRepositoryInterface $productRepository
* @param Output $outputHelper
*/
public function __construct(
Registry $registry,
RequestInterface $request,
ProductRepositoryInterface $productRepository,
Output $outputHelper
) {
$this->registry = $registry;
$this->request = $request;
$this->productRepository = $productRepository;
$this->outputHelper = $outputHelper;
}
/**
* Extract current product id
*
* @return int|null
*/
public function getCurrentProductId()
{
$currentProductId = null;
$currentProduct = $this->registry->registry('product');
if (empty($currentProduct) || !($currentProduct instanceof ProductInterface)) {
$requestId = $this->request->getParam('id');
try {
/** @var Product|ProductInterface $product */
$product = $this->productRepository->getById($requestId);
$currentProductId = $product->getId();
} catch (Exception $e) {
$currentProductId = null;
}
} else {
$currentProductId = $currentProduct->getId();
}
return $currentProductId;
}
/**
* Retrieve prepared product name by product object
*
* @param Product $product
* @return string
*/
public function getPreparedProductNameByObject($product)
{
try {
$preparedProductName=$this->outputHelper->productAttribute(
$product,
$product->getName(),
'name'
);
return $preparedProductName;
} catch (LocalizedException $ex) {
return '';
}
}
/**
* Retrieve prepared product name by its id
*
* @param int $productId
* @return string
*/
public function getPreparedProductName($productId)
{
try {
/** @var Product|ProductInterface $product */
$product=$this->productRepository->getById($productId);
$preparedProductName=$this->getPreparedProductNameByObject($product);
return $preparedProductName;
} catch (NoSuchEntityException $ex) {
return '';
}
}
/**
* Retrieve product review url by product object
*
* @param Product $product
* @return string
*/
public function getProductReviewUrlByObject($product)
{
return $product->getUrlModel()->getUrl(
$product,
[
'_fragment' => 'product_aw_reviews_tab',
'_secure' => true
]
);
}
/**
* Retrieve product review url
*
* @param int $productId
* @return string
*/
public function getProductReviewUrl($productId)
{
try {
/** @var Product|ProductInterface $product */
$product=$this->productRepository->getById($productId);
return $this->getProductReviewUrlByObject($product);
} catch (NoSuchEntityException $ex) {
return '';
}
}
}
Below Error
Error filtering template: Type Error occurred when creating object: AheadReviewsBlockEmailReviewRequestForm, Argument 5 passed to AheadReviewsBlockEmailReviewRequestForm::__construct() must be an instance of AheadReviewsModelProductResolver, instance of ABCAReviewModelProductResolver given, called in /var/www/html/Magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121