cancel
Showing results for 
Search instead for 
Did you mean: 

orders custom attribute

SOLVED

orders custom attribute

hello,

I need to add a custom attribute to orders. I just need to make get and set to it from custom scripts, so no need to have it in admin or frontend.

Below is my InstallData:

 

<?php

namespace Hoop\Util\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 =       ['exported'];
        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', 'exported', $options);

    }
}

then I did magento setup:upgrade, but nothing happened.

What am I missing?

 

thanks

 

3 REPLIES

Re: orders custom attribute

Hi @agiorgini,

 

Can you check if your module appears on setup_module table on your database?

Also, if the module was installed before you need to use the upgrade script.

 

 

--
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"

Re: orders custom attribute

Hey Damian,

 

thanks for your answer. Yes my module is in setup table, so I did set version of my module to 1.0.1 (before it was 1.0.0), this is my new InstallData:

<?php

namespace Hoop\Util\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 =  ['exported'];
        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', 'exported', $options);

    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if ($context->getVersion() && version_compare($context->getVersion(), '1.0.1') < 0) {

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


            /**
             * Remove previous attributes
             */
            $attributes =  ['exported'];
            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', 'exported', $options);
        }

        $setup->endSetup();
    }
}

ran upgrade, re-compiled, cleaned cache, in the setup_module table I have 1.0.1 in both schema_version and data_version, so looks like everything was fine, but my new attribute is not there yet.

Any hint?

Thanks

Re: orders custom attribute

@agiorgini 
Once your module entry are generated in module_setup table in magento2,
You must need to Use UpgradeData.php file to add new entry in any table.

InstallData.php file is only valid for first time use when module is initialized.

Just keep below code and one new entry are generated in your sales_order table.

Create one UpgradeData.php file,

now in your case setup version is 1.0.2 so i have keep version 1.0.2
app/code/Hoop/Util/Setup/UpgradeData.php

 

<?php
namespace Hoop\Util\Setup;

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

class UpgradeData implements UpgradeDataInterface
{
    /**
     * Sales setup factory
     *
     * @var SalesSetupFactory
     */
    private $salesSetupFactory;

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

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
        
        if ($context->getVersion()
            && version_compare($context->getVersion(), '1.0.2') < 0
        ){
            $attributes = [
               'exported' =>['type' =>'varchar','visible' => false, 'required' => false],
            ];

            foreach ($attributes as $attributeCode => $attributeParams) {
                $salesSetup->addAttribute('order', $attributeCode, $attributeParams);
            }
        }
    }
}


SalesSetup Class doesn't support RemoveAttribute method so you cant delete already exist attribute using removeAttribute() method.

 

please let me know if you need help.

 

If issue solved, click Kudos and Accept as Solution.

If Issue Solved, Click Kudos/Accept As solutions.