The codes:

public function __construct ( ProductInterfaceFactory $product_factory,
                              ProductRepositoryInterface $product_repository,
                              Registry $registry,
                              LoggerInterface $logger ) {
    $this->productFactory = $product_factory;
    $this->productRepository = $product_repository;
    $this->registry = $registry;
    $this->logger = $logger;

public function execute () {
    $product = $this->productRepository->get('Test Product A');

Also, tried deleting by calling delete() and deleteById(), both also does not work. And putting 'isSecureArea' inside registry gives me error "Registry key already exists"

And - deleteById expects Product's SKU? Not ID? Bug or just bad naming?

I got this error:

[2018-05-21 01:45:16] main.CRITICAL: Unable to remove product Test Product A {"exception":"[object] (Magento\\Framework\\Exception\\StateException(code: 0): Unable to remove product Test Product A at /var/www/magento2/vendor/magento/module-catalog/Model/ProductRepository.php:661)"} []

Help please.

Solution: I just needed to call unregister first before I register, I have no idea why.

  • Please share u full code – Amit Bera May 18 '18 at 10:23
  • @AmitBera I have put the whole code, I doubt it would make any differences. – Farid May 21 '18 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.