2

I want to delete products by sctip.

They way I am doing it for test :

if(!empty($products))
        {
            foreach($products as $_product)
            {
            $sku = $_product->getSku();

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $productRepository = $objectManager->create('\Magento\Catalog\Model\ProductRepository');
            $registry = $objectManager->create('\Magento\Framework\Registry');
            $registry->register('isSecureArea', true);
            $productRepository->deleteById($sku);
        }
    }

I am getting Error:

 [Magento\Framework\Exception\StateException]
  Unable to remove product 1000180701

EDIT:

Full Code, i am using a Product Collection:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
        $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $productCollection->addFieldToSelect('*');
        $productCollection->addFieldToFilter('sku',array('nin'=> $aProductIds));
        /** Apply filters here */
        $products = $productCollection->load();

        if(!empty($products))
        {
            foreach($products as $_product)
            {
                $sku = $_product->getSku();


                $registry = $objectManager->create('\Magento\Framework\Registry');
                $registry->register('isSecureArea', true);
                $_product->delete();
            }
        }

Throwing:

[Magento\Framework\Exception\LocalizedException]
  Delete operation is forbidden for current area
2

If you change this:

$registry = $objectManager->create('\Magento\Framework\Registry');

To:

$registry = $objectManager->get('\Magento\Framework\Registry');

It should work I believe.

1

If $products is a collection of products you don't need to do all that.

You can simply do:

foreach ($products as $product) {
    $product->delete();
}
  • 2
    Outputs: [Magento\Framework\Exception\LocalizedException] Delete operation is forbidden for current area - I also added the register Part but didnt change anything. BTW it's a productCollection. – Me7z Oct 11 '16 at 9:10
  • 1
    @Ecoplan what is the the class of $products variable ? – Raphael at Digital Pianism Oct 11 '16 at 9:11
  • 1
    $products is a loaded collection – Me7z Oct 11 '16 at 9:12
  • 1
    @Ecoplan you still getting the Delete operation is forbidden for current area error even after adding the register part ? – Raphael at Digital Pianism Oct 11 '16 at 9:15
  • 1
    watch my edited post – Me7z Oct 11 '16 at 9:18
1

I have to fetch the same issue so create a test file root directory and add below code...

<?php
use Magento\Framework\AppInterface;
try {
    require_once __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try{
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');

    $productID = 11;
    $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productID);
    $product->delete();
    echo ' successfully remove..';
}
catch(\Exception $e){
    print_r($e->getMessage());
}

may be your issue resolve...

0

You can also delete product via the Magento 2 Import process.

Just create a CSV file with the SKU list and then select Delete behavior from the product import in Magento 2 BO.

Moreover, I think you cannot delete products from the FO. You can only delete, edit product from Admin store.

You need to have

$this->registry->registry('isSecureArea')  == true

If you check the logic code of the regitry, you cannot overwride a regitry key

public function register($key, $value, $graceful = false)
{
    if (isset($this->_registry[$key])) {
        if ($graceful) {
            return;
        }
        throw new \RuntimeException('Registry key "' . $key . '" already exists');
    }
    $this->_registry[$key] = $value;
}

Try this :

$this->registry->unregister('isSecureArea')
$this->registry->register('isSecureArea', true)

Complete method based on Magento 2 test code :

/**
 * Enable secure/admin area
 *
 * @param bool $flag
 * @return void
 */
protected static function _enableSecureArea($flag = true)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $objectManager->get('Magento\Framework\Registry')->unregister('isSecureArea');
    if ($flag) {
        $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', $flag);
    }
}

And try to use Dependancy injection whereas objectmanager.

0

Your registry key : $objectManager->create('\Magento\Framework\Registry')

This should be Magento\Framework\Registry instead of \Magento\Framework\Registry.

The full snippet :

$registry = $objectManager->create('Magento\Framework\Registry');
$registry->register('isSecureArea', true);
$_product->delete();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.