-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URL key for specified store already exists. #1225
Comments
@sivajik34 Product management is possible only in admin application area. Currently, you can switch an execution context using Please let us know the results. |
sivaji- bin # php magento kensium:delete_product yellow --bootstrap="MAGE_RUN_CODE=admin" [RuntimeException] |
can you explain little bit info about how to set --bootstrap option? |
Bootstrap option allows to transfer initialization params such as run_code, maintanence, etc (\Magento\Framework\Console\Cli::getApplicationCommands:44-47. (Unfortunatelly, not enough to switch the context) Here is the complete listing, that allows to execute your code in admin context: use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\App\State;
use Magento\Framework\App\ObjectManager\ConfigLoader;
class TestDeleteCommand extends Command
{
protected $_productRepository;
protected $registry;
protected $state;
protected $configLoader;
const SKU = "sku";
protected $objectManager;
public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Catalog\Api\ProductRepositoryInterface $_productRepository,
State $state,
ConfigLoader $loader,
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->registry = $registry;
$this->state = $state;
$this->objectManager = $objectManager;
$this->_productRepository = $_productRepository;
$this->configLoader = $loader;
parent::__construct();
}
protected function configure()
{
$this->setName('delete_product')
->setDescription('Delete the product by given sku')
->setDefinition($this->getOptionsList());
}
public function getOptionsList()
{
return [
new InputArgument(self::SKU, InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'SKU')
];
}
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument(self::SKU)) {
throw new \InvalidArgumentException('Missing Argument ' . self::SKU);
}
$this->state->setAreaCode('adminhtml');
$this->objectManager->configure($this->configLoader->load('adminhtml'));
$this->registry->register('isSecureArea', true);
$skus = $input->getArgument(self::SKU);
foreach ($skus as $sku) {
$this->_productRepository->deleteById($sku);
$output->writeln('product deleted, sku: ' . $sku . '');
}
} Please note: CLI entry point might be changed. It would be very helpful if you could share your feedback about command creation. |
thank you for your patience to explain.now its working fine. |
Thank you for feedback. We will do our best to address it. Can we close the issue? |
sure.thank you once again. |
1223: remove uneeded system.xml
I have created command for product delete by given sku.its working fine except url rewrite.
registry = $registry; $this->_productRepository =$_productRepository; parent::__construct(); } /** - {@inheritdoc} */ protected function configure() { $this->setName('delete_product') ->setDescription('Delete the product by given sku')->setDefinition($this->getOptionsList()); parent::configure(); } public function getOptionsList() { return [ new InputArgument(self::SKU, InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'SKU') ]; } /** - {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { if (!$input->getArgument(self::SKU)) { throw new \InvalidArgumentException('Missing Argument ' . self::SKU); } $this->registry->register('isSecureArea', true); $skus=$input->getArgument(self::SKU); foreach($skus as $sku) { $this->_productRepository->deleteById($sku); $output->writeln('product deleted, sku: ' . $sku . ''); } } }for example i added product named "shirt123" in back end.and url key also shirt123.html
case 1:if i delete same product using my custom command in terminal..its deleted product.but in table url_rewrite still associated rows are existed.
if i try to add with same product name and url key i'm getting following error
" URL key for specified store already exists."
case 2:if i delete same product from back end ...all is well.
So problem is my code or its a magento bug?
The text was updated successfully, but these errors were encountered: