I've set up a custom sales order attribute so I can mark orders as 'sent'
when they've been exported via a custom module.
My issue is it's not saving the attribute on the $_order->save()
function. I've tried setSentToSage(1)
and setData('sent_to_sage',1)
but neither work
I've setup up the following:
Setup/InstallData.php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Setup\SalesSetupFactory;
class InstallData implements InstallDataInterface
{
private $salesSetupFactory;
/**
* Constructor
*
* @param \Magento\Sales\Setup\SalesSetupFactory $salesSetupFactory
*/
public function __construct(SalesSetupFactory $salesSetupFactory)
{
$this->salesSetupFactory = $salesSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
$salesSetup->addAttribute('order', 'sent_to_sage',
[
'type' => 'varchar',
'length' => 255,
'visible' => true,
'required' => false,
'grid' => true,
'user_defined' => true
]
);
}
}
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
<arguments>
<argument name="columns" xsi:type="array">
<item name="sent_to_sage" xsi:type="string">sales_order.sent_to_sage</item>
</argument>
</arguments>
</virtualType>
</config>
Model/Orders.php
namespace Vendor\Module\Model;
use Magento\Sales\Model\Order;
class Orders
{
private $_order;
public function __construct(
Order $order,
){
$this->_order = $order;
}
public function exportOrders()
{
/*Perform export functions...*/
/* Mark exported orders as sent */
$this->markOrdersAsSent($order_ids);
}
public function markOrdersAsSent($order_ids)
{
if ($order_ids) {
foreach ($order_ids as $id) {
$_order = $this->_order->load($id);
$_order->setSentToSage(1);
$_order->save();
}
}
return;
}
}
Any help is appreciated. I feel I am missing something.