Skip to content
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

Closed
sivajik34 opened this issue May 1, 2015 · 7 comments
Closed

URL key for specified store already exists. #1225

sivajik34 opened this issue May 1, 2015 · 7 comments

Comments

@sivajik34
Copy link
Contributor

I have created command for product delete by given sku.its working fine except url rewrite.
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?

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 . ''); } } }
@vrann vrann added the PS label May 1, 2015
@mazhalai mazhalai added MX and removed PS labels May 4, 2015
@kokoc kokoc self-assigned this May 5, 2015
@kokoc
Copy link
Member

kokoc commented May 5, 2015

@sivajik34 Product management is possible only in admin application area. Currently, you can switch an execution context using --bootstrap="MAGE_RUN_CODE=admin" command line option. However, the work on CLI is in progress, so we may change the defaults for this entry point.

Please let us know the results.

@sivajik34
Copy link
Contributor Author

sivaji- bin # php magento kensium:delete_product yellow --bootstrap="MAGE_RUN_CODE=admin"

[RuntimeException]
The "--bootstrap" option does not exist.

@sivajik34
Copy link
Contributor Author

can you explain little bit info about how to set --bootstrap option?

@kokoc
Copy link
Member

kokoc commented May 6, 2015

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.

@sivajik34
Copy link
Contributor Author

thank you for your patience to explain.now its working fine.
feedback:
I want my all custom commands into one namespace.for that i have to follow:for example setName(kensium:delete_product).but its too lengthy for user to enter.is there any possiblity for user can enter command only "delete_product".

@kokoc
Copy link
Member

kokoc commented May 6, 2015

Thank you for feedback. We will do our best to address it.

Can we close the issue?

@sivajik34
Copy link
Contributor Author

sure.thank you once again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants