Magento 2 Cara membuat atribut orde baru secara terprogram

12

Saya telah mencari di web tentang cara membuat atribut pesanan (jika itu yang disebut), pada dasarnya saya hanya ingin kolom database baru muncul di database sales_order, jelas saya bisa membuatnya secara manual tetapi apakah ada cara yang bisa saya buat melalui skrip pemutakhiran / terprogram?

André Ferraz
sumber

Jawaban:

25

Secara praktis, ada dua cara utama untuk menambahkan atribut pesanan (kolom baru) untuk memesan melalui skrip pemutakhiran.

- Menggunakan $ setup-> getConnection () -> addColumn ()

app / code / Vendor / SalesOrder / Setup / UpgradeSchema.php

<?php

namespace Vendor\SalesOrder\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * Upgrades DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $quote = 'quote';
        $orderTable = 'sales_order';

        $setup->getConnection()
            ->addColumn(
                $setup->getTable($quote),
                'custom_attribute',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'Custom Attribute'
                ]
            );
        //Order table
        $setup->getConnection()
            ->addColumn(
                $setup->getTable($orderTable),
                'custom_attribute',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'Custom Attribute'
                ]
            );

        $setup->endSetup();
    }
}

- Menggunakan Kutipan dan Penjualan Pengaturan Pabrik

app / code / Vendor / SalesOrder / Setup / UpgradeData.php

<?php

namespace Vendor\SalesOrder\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var QuoteSetupFactory
     */
    protected $quoteSetupFactory;

    /**
     * @var SalesSetupFactory
     */
    protected $salesSetupFactory;

    /**
     * @param QuoteSetupFactory $quoteSetupFactory
     * @param SalesSetupFactory $salesSetupFactory
     */
    public function __construct(
        QuoteSetupFactory $quoteSetupFactory,
        SalesSetupFactory $salesSetupFactory
    ) {
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;
    }
    /**
     * Upgrades DB for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var \Magento\Quote\Setup\QuoteSetup $quoteInstaller */
        $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);

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

        $setup->startSetup();

        //Add multiple attributes to quote 
        $entityAttributesCodes = [
            'custom_attribute' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'custom_attribute1' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT

        ];

        foreach ($entityAttributesCodes as $code => $type) {

            $quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
             $salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true,]);
            $salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
        }

        $setup->endSetup();
    }
}
Khoa TruongDinh
sumber
Terima kasih banyak, saya baru saja akan menambahkan solusi.
André Ferraz
4
Ini hanya untuk membuat kolom tambahan kosong di tabel pesanan, kan? Bagaimana kita bisa menyalin data dari atribut khusus ke dalam kolom tambahan ini dalam tabel pesanan?
Magento Learner
1
Apa yang lebih baik dari dua cara itu?
Alex
Apakah atribut ini lulus dalam perintah pihak ketiga mana pun mengelola api?
Dhaduk Mitesh