سؤال

أريد إنشاء أمر لعملية الحذف لمنتج بسيط بواسطة sku.أحصل على الخطأ التالي. كيفية تعيين منطقة الإدارة؟

[ماجنتو\إطار العمل\الاستثناء\الاستثناء المحلي]
عملية الحذف محظورة للمنطقة الحالية

<?php
namespace Sivakumar\Sample\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

class DeleteSimpleProduct extends Command
{
    protected $_product;
    public function __construct(\Magento\Catalog\Model\Product $_product)
    {
        $this->_product =$_product;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('delete_simple_product')
            ->setDescription('Delete Simple Product')
            ->setDefinition($this->getOptionsList());

        parent::configure();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $errors = $this->validate($input);
        if ($errors) {
            throw new \InvalidArgumentException(implode("\n", $errors));
        }

    $product_id = $this->_product->getIdBySku($input->getOption('sku'));
    $product=$this->_product->load($product_id);
        $product->delete();
        $output->writeln('<info>product deleted ' . $input->getOption('sku') . '</info>');
    }

    public function getOptionsList()
    {
        return [
            new InputOption('sku', null, InputOption::VALUE_REQUIRED, 'SKU'),
        ];
    }

    public function validate(InputInterface $input)
    {
        $errors = [];
        $required =['sku',]; 

        foreach ($required as $key) {
            if (!$input->getOption($key)) {
                $errors[] = 'Missing option ' . $key;
            }
        }
        return $errors;
    }
}

di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
    <arguments>
        <argument name="commands" xsi:type="array">
            <item name="delete_simple_product" xsi:type="object">Sivakumar\Sample\Console\DeleteSimpleProduct</item>
        </argument>
    </arguments>
</type>
</config>
هل كانت مفيدة؟

المحلول

اتفق مع ماكس على أنه يجب عليك استخدام ProductRepositoryInterface::deleteById($sku), ، ولكنك ستحتاج أيضًا إلى إجراء تغيير إضافي للحصول على أذونات الحذف.

لاحظ أن منطقة الإدارة تتعامل مع هذا عن طريق إنشاء التكوين التالي في app/code/Magento/Backend/etc/adminhtml/di.xml

    <preference for="Magento\Framework\Model\ActionValidator\RemoveAction" type="Magento\Framework\Model\ActionValidator\RemoveAction\Allowed" />

ال Magento\Framework\Model\ActionValidator\RemoveAction\Allowed يمنع الفصل التحقق من الإذن بمجرد العودة true في ال isAllowed طريقة.

بدون التغيير أعلاه إلى di.xml Magento\Framework\Model\ActionValidator\RemoveAction سيتم استخدام الفئة، الأمر الذي سيؤدي إلى فشل طلب الحذف الخاص بك ما لم $this->registry->registry('isSecureArea') تم ضبطه على صحيح.

يبدو أنك تحاول إنشاء بعض أوامر وحدة التحكم ولست على دراية بها جيدًا بعد، لذا قد يكون أفضل رهان لك الآن هو تعيين السجل للسماح بعملية الحذف وإعادة البناء لاحقًا إذا تم العثور على حل أنظف.

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

نصائح أخرى

لقد واجهت هذه المشكلة مؤخرًا أثناء كتابة أمر وحدة التحكم لحذف الفئات الفارغة.

كما قيل في إجابة أخرى تحتاج إلى التسجيل 'isSecureArea' إلى صحيح.

للقيام بذلك في أمر وحدة التحكم، تحتاج إلى تمرير فئة Magento\Framework egistry إلى مُنشئك.

في حالتي فعلت بهذه الطريقة:

public function __construct(CategoryManagementInterface $categoryManagementInterface, CategoryRepositoryInterface $categoryRepositoryInterface, Registry $registry)
{
    $this->_categoryRepository = $categoryRepositoryInterface;
    $this->_categoryManagement = $categoryManagementInterface;
    $registry->register('isSecureArea', true);


    parent::__construct();
}

ومن ثم في execute الطريقة التي استخدمت بها المستودع لإجراء الحذف الفعلي:

$this->_categoryRepository->deleteByIdentifier($category->getId());

إذا كنت تستخدم البرنامج النصي، فيرجى إنشاء كائن التسجيل كما هو موضح أدناه.

giveacodicetagpre.

يرجى النقر هنا للحصول على شرح مفصل. http://www.pearlbells.co.uk/mass-Delete-magento-2-الفئات برمجيا /

إذا كان البرنامج النصي مرة واحدة، يمكنك استخدام OM

توسيع إجابة كريس O'tool.أحتاج أيضا إلى حذف الفئات من أمر، في الواقع من أوامر متعددة.في البداية مجرد وجود

giveacodicetagpre.

في أمر واحد يعمل بشكل جيد، ولكن عندما أضع ذلك في أوامر متعددة (في المنشئ) حصلت على هذا الخطأ أثناء تجميع

مفتاح التسجيل "ISSECUREAEA" موجود بالفعل

التحقق أولا من وجود مفتاح التسجيل حلها

giveacodicetagpre.

لست متأكدا مما إذا كان الأمر سيئا لوضع ذلك في المنشئ، لكنه افترض أن هذا هو السبب في أن الخطأ واجه الخطأ.بدلا من ذلك، يجب أن تكون قادرا على الابتعاد عن تشغيل المقتطف الأول من أساليب GodeAdicetagCode.مرة أخرى، لست متأكدا مما يعتبر أفضل ممارسة ...

للعمليات مع المنتج، يجب عليك استخدام مستودع المخزون.

giveacodicetagpre.

بدلاً من تعيين isSecureArea، يمكنك أيضًا السماح بإزالة نوع واحد من الكائنات عن طريق تجاوز النوع RemoveAction الحجج في الخاص بك di.xml مثله:

<type name="Magento\Framework\Model\ActionValidator\RemoveAction">
    <arguments>
        <argument name="protectedModels" xsi:type="array">
            <item name="salesOrder" xsi:type="null" /> <!--allow orders to be removed from front area-->
        </argument>
    </arguments>
</type>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top