4

I created an custom attribute for orders:

UpgradeSchema.php

<?php



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),
                'mediabasebestellnummer',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'MediabaseNummer'
                ]
            );
        //Order table
        $setup->getConnection()
            ->addColumn(
                $setup->getTable($orderTable),
                'mediabasebestellnummer',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'MediabaseNummer'
                ]
            );

        $setup->endSetup();
    }
}

Right now my question is how get the custom attribute when I have an order. This is NOT working:

$custom = $order->getCustomAttribute('mediabasebestellnummer');

Thanks for any help!

  • Did you try $custom = $order->getMediabasebestellnummer(); – Sukumar Gorai Jul 17 '18 at 10:48
5

check if the column is created in sales_order table

Now getting the values

 $order->getMediabasebestellnummer();

Or

$order->getData('mediabasebestellnummer');

For adding the data

 $order->setMediabasebestellnummer('test value')->save();

or

 $order->setData('mediabasebestellnummer','test value')->save();

or

$data = array('mediabasebestellnummer'=>'test');
$order->setData($data)->save();`
  • is there also a possibility to Set? – k33n Jul 17 '18 at 10:53
  • @L.Klmn set is use to assign the value not for fetching. – Qaisar Satti Jul 17 '18 at 10:54
  • yes I also need that. :) – k33n Jul 17 '18 at 10:55
  • @L.Klmn added the possible solutions .. – Qaisar Satti Jul 17 '18 at 10:59
3

Make sure you have file fieldset.xml in your module to convert data from quote to order.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_quote">
            <field name="mediabasebestellnummer">
                <aspect name="to_order" />
            </field>
        </fieldset>
    </scope>
</config>
  • where should I place that? – k33n Jul 17 '18 at 11:18
  • @L.Klmn it is used when you have column in both quote and order table. – Qaisar Satti Jul 17 '18 at 11:24
  • @L.Klmn You could place that in etc folder, and yes, if you have column in both table, it will convert from quote table to sales_order table – Quan Le Jul 17 '18 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.