Magento2 Özel Sipariş Özelliği Ekleme


9

Magento'daki bir Siparişe nasıl özel bir özellik eklerim? Ürün ve Müşteri için açık gibi görünüyor, ancak siparişlere ekstra özellikler eklemek için herhangi bir bilgi bulamıyorum.


Çözdün mü? Aynı problemim var
— Phoenix128_RiccardoT

Yanıtlar:


4

Kendi modülünüzü oluşturun ve Kurulum /InstallData.php

<?php



namespace Own\Module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Encryption\Encryptor;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Category setup factory
     *
     * @var CategorySetupFactory
     */
    protected $categorySetupFactory;

    /**
     * Quote setup factory
     *
     * @var QuoteSetupFactory
     */
    protected $quoteSetupFactory;

    /**
     * Sales setup factory
     *
     * @var SalesSetupFactory
     */
    protected $salesSetupFactory;


    /**
     * Init
     *
     * @param CategorySetupFactory $categorySetupFactory
     * @param SalesSetupFactory $salesSetupFactory
     */
    public function __construct(
        SalesSetupFactory $salesSetupFactory
    ) {
        $this->salesSetupFactory = $salesSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);


        /**
         * Remove previous attributes
         */
        $attributes =       ['NEW_ATTRIBUTE'];
        foreach ($attributes as $attr_to_remove){
            $salesSetup->removeAttribute(\Magento\Sales\Model\Order::ENTITY,$attr_to_remove);

        }



        /**
         * Add 'NEW_ATTRIBUTE' attributes for order
         */
        $options = ['type' => 'varchar', 'visible' => false, 'required' => false];
        $salesSetup->addAttribute('order', 'NEW_ATTRIBUTE', $options);

    }
}

Ayrıca, öğelere yeni özellik atamak için aynı yapıyı kullanabilirsiniz. @RiccardoT sizin için de

Düzenleme: Nasıl yeni öznitelik değerleri eklemek (Bunu bir cron işi benim Model / Orders.php kullanın):

use Magento\Directory\Model\Currency;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\ResourceModel\Order\Address\Collection;
use Magento\Sales\Model\ResourceModel\Order\Creditmemo\Collection as CreditmemoCollection;
use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection as InvoiceCollection;
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ImportCollection;
use Magento\Sales\Model\ResourceModel\Order\Payment\Collection as PaymentCollection;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;

class Orders
{

    /**
     * @param \Magento\Sales\Model\Order $order
     */

    public function __construct(
        \Magento\Sales\Model\Order $order,
        \Psr\Log\LoggerInterface $loggerInterface,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepositoryInterface
    ) {

        $this->order = $order;
        $this->logger = $loggerInterface;
        $this->orderRepository = $orderRepositoryInterface;
    }

    public function execute(){

       $order = $this->order->loadByIncrementId($incrementId);
       $order->setNewAttribute('NEW VALUE');
       $order->save();
   }
}

Bu genellikle bir siparişe nasıl erişileceği ve özelliklerin değerlerinin nasıl ayarlanacağıdır.


Bu özellikler eklemek, ancak daha sonra yeni özellikler hakkında bilgi kaydetmek için herhangi bir yol bulamıyorum, onları filtrelemek gibi görünüyor.
— Phoenix128_RiccardoT

1
@RiccardoT Kodu düzenledim, kodumun yeni özelliklerime değerler eklemek için kullandığım bir bölümünü görebiliyorsunuz.
— ntzz
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.