I need to overwrite the block MagentoCatalogBlockProductView to add the getStoreCode () function and be able to call it from a phtml file that uses this block.
I have followed a tutorial to create a module that does this, but I am getting the following error on the product page:
1 exception(s):
Exception #0 (MagentoFrameworkExceptionRuntimeException): Type Error occurred when creating object: VendorIconosPorIdiomasBlockRewriteProductViewInterceptor
I put all the code of my module below in case someone can tell me what I’m doing wrong.
VendorIconosPorIdiomasregistration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Vendor_IconosPorIdiomas',
__DIR__
);
VendorIconosPorIdiomasetcmodule.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_IconosPorIdiomas" setup_version="1.0.0">
</module>
</config>
VendorIconosPorIdiomasetcdi.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogBlockProductView" type="VendorIconosPorIdiomasBlockRewriteProductView" />
</config>
VendorIconosPorIdiomasBlockRewriteProductView I have just copied the View class and added the StoreManagerInterface class to the constructor and the getStoreCode () function
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorIconosPorIdiomasBlockRewriteProduct;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCatalogModelCategory;
use MagentoStoreModelStoreManagerInterface;
/**
* Product View block
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class View extends MagentoCatalogBlockProductView
{
/**
* Magento string lib
*
* @var MagentoFrameworkStdlibStringUtils
*/
protected $string;
/**
* @var MagentoFrameworkJsonEncoderInterface
*/
protected $_jsonEncoder;
/**
* @var MagentoFrameworkPricingPriceCurrencyInterface
* @deprecated 102.0.0
*/
protected $priceCurrency;
/**
* @var MagentoFrameworkUrlEncoderInterface
*/
protected $urlEncoder;
/**
* @var MagentoCatalogHelperProduct
*/
protected $_productHelper;
/**
* @var MagentoCatalogModelProductTypesConfigInterface
*/
protected $productTypeConfig;
/**
* @var MagentoFrameworkLocaleFormatInterface
*/
protected $_localeFormat;
/**
* @var MagentoCustomerModelSession
*/
protected $customerSession;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* @param Context $context
* @param MagentoFrameworkUrlEncoderInterface $urlEncoder
* @param MagentoFrameworkJsonEncoderInterface $jsonEncoder
* @param MagentoFrameworkStdlibStringUtils $string
* @param MagentoCatalogHelperProduct $productHelper
* @param MagentoCatalogModelProductTypesConfigInterface $productTypeConfig
* @param MagentoFrameworkLocaleFormatInterface $localeFormat
* @param MagentoCustomerModelSession $customerSession
* @param ProductRepositoryInterface|MagentoFrameworkPricingPriceCurrencyInterface $productRepository
* @param MagentoFrameworkPricingPriceCurrencyInterface $priceCurrency
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param array $data
* @codingStandardsIgnoreStart
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
MagentoFrameworkUrlEncoderInterface $urlEncoder,
MagentoFrameworkJsonEncoderInterface $jsonEncoder,
MagentoFrameworkStdlibStringUtils $string,
MagentoCatalogHelperProduct $productHelper,
MagentoCatalogModelProductTypesConfigInterface $productTypeConfig,
MagentoFrameworkLocaleFormatInterface $localeFormat,
MagentoCustomerModelSession $customerSession,
ProductRepositoryInterface $productRepository,
MagentoFrameworkPricingPriceCurrencyInterface $priceCurrency,
MagentoStoreModelStoreManagerInterface $storeManager,
array $data = ()
) {
$this->_productHelper = $productHelper;
$this->urlEncoder = $urlEncoder;
$this->_jsonEncoder = $jsonEncoder;
$this->productTypeConfig = $productTypeConfig;
$this->string = $string;
$this->_localeFormat = $localeFormat;
$this->customerSession = $customerSession;
$this->productRepository = $productRepository;
$this->priceCurrency = $priceCurrency;
$this->_storeManager = $storeManager;
parent::__construct(
$context,
$data
);
}
// @codingStandardsIgnoreEnd
/**
* Get Store code
*
* @return string
*/
public function getStoreCode()
{
return $this->_storeManager->getStore()->getCode();
}
/**
* Return wishlist widget options
*
* @return array
* @deprecated 101.0.1
*/
public function getWishlistOptions()
{
return ('productType' => $this->getProduct()->getTypeId());
}
/**
* Retrieve current product model
*
* @return MagentoCatalogModelProduct
*/
public function getProduct()
{
if (!$this->_coreRegistry->registry('product') && $this->getProductId()) {
$product = $this->productRepository->getById($this->getProductId());
$this->_coreRegistry->register('product', $product);
}
return $this->_coreRegistry->registry('product');
}
/**
* Check if product can be emailed to friend
*
* @return bool
*/
public function canEmailToFriend()
{
return false;
}
/**
* Retrieve url for direct adding product to cart
*
* @param MagentoCatalogModelProduct $product
* @param array $additional
* @return string
*/
public function getAddToCartUrl($product, $additional = ())
{
if ($this->hasCustomAddToCartUrl()) {
return $this->getCustomAddToCartUrl();
}
if ($this->getRequest()->getParam('wishlist_next')) {
$additional('wishlist_next') = 1;
}
$addUrlKey = MagentoFrameworkAppActionInterface::PARAM_NAME_URL_ENCODED;
$addUrlValue = $this->_urlBuilder->getUrl('*/*/*', ('_use_rewrite' => true, '_current' => true));
$additional($addUrlKey) = $this->urlEncoder->encode($addUrlValue);
return $this->_cartHelper->getAddUrl($product, $additional);
}
/**
* Get JSON encoded configuration which can be used for JS dynamic price calculation depending on product options
*
* @return string
*/
public function getJsonConfig()
{
/* @var $product MagentoCatalogModelProduct */
$product = $this->getProduct();
if (!$this->hasOptions()) {
$config = (
'productId' => $product->getId(),
'priceFormat' => $this->_localeFormat->getPriceFormat()
);
return $this->_jsonEncoder->encode($config);
}
$tierPrices = ();
$priceInfo = $product->getPriceInfo();
$tierPricesList = $priceInfo->getPrice('tier_price')->getTierPriceList();
foreach ($tierPricesList as $tierPrice) {
$tierPrices() = $tierPrice('price')->getValue() * 1;
}
$config = (
'productId' => (int)$product->getId(),
'priceFormat' => $this->_localeFormat->getPriceFormat(),
'prices' => (
'oldPrice' => (
'amount' => $priceInfo->getPrice('regular_price')->getAmount()->getValue() * 1,
'adjustments' => ()
),
'basePrice' => (
'amount' => $priceInfo->getPrice('final_price')->getAmount()->getBaseAmount() * 1,
'adjustments' => ()
),
'finalPrice' => (
'amount' => $priceInfo->getPrice('final_price')->getAmount()->getValue() * 1,
'adjustments' => ()
)
),
'idSuffix' => '_clone',
'tierPrices' => $tierPrices
);
$responseObject = new MagentoFrameworkDataObject();
$this->_eventManager->dispatch('catalog_product_view_config', ('response_object' => $responseObject));
if (is_array($responseObject->getAdditionalOptions())) {
foreach ($responseObject->getAdditionalOptions() as $option => $value) {
$config($option) = $value;
}
}
return $this->_jsonEncoder->encode($config);
}
/**
* Return true if product has options
*
* @return bool
*/
public function hasOptions()
{
if ($this->getProduct()->getTypeInstance()->hasOptions($this->getProduct())) {
return true;
}
return false;
}
/**
* Check if product has required options
*
* @return bool
*/
public function hasRequiredOptions()
{
return $this->getProduct()->getTypeInstance()->hasRequiredOptions($this->getProduct());
}
/**
* Define if setting of product options must be shown instantly.
* Used in case when options are usually hidden and shown only when user
* presses some button or link. In editing mode we better show these options
* instantly.
*
* @return bool
*/
public function isStartCustomization()
{
return $this->getProduct()->getConfigureMode() || $this->_request->getParam('startcustomization');
}
/**
* Get default qty - either as preconfigured, or as 1.
*
* Also restricts it by minimal qty.
*
* @param null|MagentoCatalogModelProduct $product
* @return int|float
*/
public function getProductDefaultQty($product = null)
{
if (!$product) {
$product = $this->getProduct();
}
$qty = $this->getMinimalQty($product);
$config = $product->getPreconfiguredValues();
$configQty = $config->getQty();
if ($configQty > $qty) {
$qty = $configQty;
}
return $qty;
}
/**
* Get container name, where product options should be displayed
*
* @return string
*/
public function getOptionsContainer()
{
return $this->getProduct()->getOptionsContainer() == 'container1' ? 'container1' : 'container2';
}
/**
* Check whether quantity field should be rendered
*
* @return bool
*/
public function shouldRenderQuantity()
{
return !$this->productTypeConfig->isProductSet($this->getProduct()->getTypeId());
}
/**
* Get Validation Rules for Quantity field
*
* @return array
*/
public function getQuantityValidators()
{
$validators = ();
$validators('required-number') = true;
return $validators;
}
/**
* Return identifiers for produced content
*
* @return array
*/
public function getIdentities()
{
$identities = $this->getProduct()->getIdentities();
return $identities;
}
/**
* Retrieve customer data object
*
* @return int
*/
protected function getCustomerId()
{
return $this->customerSession->getCustomerId();
}
}
VendorIconosPorIdiomasviewfrontendlayoutcatalog_product_view.xml
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.detail.info" remove="true"/>
<referenceContainer name="content">
<block class="MagentoCatalogBlockProductView" name="product.detail.info" as="product_detail_info" template="Vendor_IconosPorIdiomas::detail_layout.phtml" after="-" />
</referenceContainer>
</body>
</page>
And finally in my VendorIconosPorIdiomasviewfrontendtemplatesdetail_layout.phtml file I have added:
<?php echo $block->getStoreCode()?>
I appreciate any help.