I am fairly new to Magento and following a tutorial on Udemy to learn how to develop on Magento 2.
I am attempting to overwrite the ProductRepositoryInterface, however whenever I add the preference tag in my di.xml file I am getting a blank page.
When I remove the preference tag my di.xml works fine.
I’ve attached my code below.
I have tried cache:clean and setup:upgrade with no luck.
If somebody is able to stear me in the right direction I’d be very grateful.
Thanks, Pete!
Controller/Page/HelloWorld.php
<?php
namespace PHPeteFirstModuleControllerPage;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
use PHPeteFirstModuleNotMagentoPencilInterface;
use MagentoCatalogApiProductRepositoryInterface;
class HelloWorld extends MagentoFrameworkAppActionAction {
protected $pencilInterface;
protected $productRepository;
public function __construct(Context $context, PencilInterface $pencilInterface, ProductRepositoryInterface $productRepository){
$this->pencilInterface = $pencilInterface;
$this->productRepository = $productRepository;
parent::__construct($context);
}
public function execute(){
echo 'Hello World<br>';
echo get_class($this->productRepository);
}
}
Model/CustomImplementation.php
namespace PHPeteFirstModuleModel;
use MagentoCatalogApiProductRepositoryInterface;
class CustomImplementation implements ProductRepositoryInterface
{
public function save(MagentoCatalogApiDataProductInterface $product, $saveOptions = false);
public function get($sku, $editMode = false, $storeId = null, $forceReload = false);
public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);
public function delete(MagentoCatalogApiDataProductInterface $product);
public function deleteById($sku);
public function getList(MagentoFrameworkApiSearchCriteriaInterface $searchCriteria);
}
etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="PHPeteFirstModuleNotMagentoPencilInterface" type="PHPeteFirstModuleNotMagentoBigPencils" />
<preference for="MagentoCatalogApiProductRepositoryInterface" type="PHPeteFirstModuleModelCustomImplementation" />
</config>