0

Have gone through several posts but was somehow not able to find a fix for this issue

When I save, its says "Source saved" but my custom field is not beign saved/fetched

What I have done so far is as below: enter image description here

extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\InventoryApi\Api\Data\SourceInterface">
        <attribute code="warehouse_timezone" type="string" />
    </extension_attributes>
</config>

inventory_source_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="address">
        <field name="warehouse_timezone" formElement="select" sortOrder="70">
            <settings>
                <dataType>text</dataType>
                <dataScope>extension_attributes.warehouse_timezone</dataScope>
                <label translate="true">Warehouse timezone</label>
                <notice translate="true">Warehouse timezone will be used in fetching Fedex shipping rates accodring to warehouse current date-time.</notice>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="vendor\module\Model\WarehouseTimezone"/>
                        <!-- <options class="Magento\Config\Model\Config\Source\Locale\Timezone"/> -->
                    </settings>
                </select>
            </formElements>
        </field>
    </fieldset>
</form>

Model/WarehouseTimezon.php (for fetching option array)

<?php namespace vendor\module\Model;
class WarehouseTimezone implements \Magento\Framework\Option\ArrayInterface
{
    const US_EASTERN='US/Eastern';
    const US_CENTRAL='US/Central';
    const US_ARIZONA='US/Arizona';

    public function getOptionArray()
    {
        $data_array=array(); 
        $data_array[self::US_EASTERN]=self::US_EASTERN;
        $data_array[self::US_CENTRAL]=self::US_CENTRAL;
        $data_array[self::US_ARIZONA]=self::US_ARIZONA;
        return($data_array);
    }

    public function getValueArray()
    {
        $data_array=array();
        foreach($this->getOptionArray() as $key=>$value){ $data_array[]=array('label'=>$key, 'value'=>$value); }
        return($data_array);
    }

    /**
     * @return array
     */
    public function toOptionArray(){ return $this->getValueArray(); }
}

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">
    <type name="Magento\InventoryApi\Api\SourceRepositoryInterface">
        <plugin name="plugin_source_save" type="vendor\module\Plugin\SourceSave" sortOrder="1" />
    </type>
</config>

Plugin/SourceSave.php

<?php namespace vendor\module\Plugin;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface;
use Magento\InventoryApi\Api\Data\SourceExtensionFactory;
use Magento\InventoryApi\Api\Data\SourceExtensionInterfaceFactory;
use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
class SourceSave
{
    const FIELD_NAME='warehouse_timezone';
    protected $extensionFactory;
    protected $sourceFactory;

    public function __construct
    (
        SourceExtensionFactory $extensionFactory
    )
    {
        $this->extensionFactory=$extensionFactory;
    }


    public function afterGet(SourceRepositoryInterface $subject, SourceInterface $source)
    {
        $sourceComment=$source->getData(self::FIELD_NAME);
        $extensionAttributes=$source->getExtensionAttributes();
        $extensionAttributes=$extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setWarehouseTimezone($sourceComment);
        $source->setExtensionAttributes($extensionAttributes);
        return $source;
    }

    public function afterGetList(SourceRepositoryInterface $subject, SourceSearchResultsInterface $result)
    {
        $products=[];
        $sources=$result->getItems();
        foreach ($sources as $source)
        {
            $sourceComment=$source->getData(self::FIELD_NAME);
            // echo $sourceComment;
            $extensionAttributes=$source->getExtensionAttributes();
            $extensionAttributes=$extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setWarehouseTimezone($sourceComment);
            $source->setExtensionAttributes($extensionAttributes);
            $products[]=$source;
        }
        $result->setItems($products);
        return $result;
    }

    public function beforeSave(SourceRepositoryInterface $subject, SourceInterface $source)
    {
        $extensionAttributes=$source->getExtensionAttributes() ?: $this->extensionFactory->create();
        if($extensionAttributes !== null && $extensionAttributes->getWarehouseTimezone() !== null)
        {
            $source->setWarehouseTimezone($extensionAttributes->getWarehouseTimezone());
        }
        // var_dump($source->getData());die; // getting here value
        return [$source];
    }
}

events.xml Observer For saving its value

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_inventory_populate_source_with_data">
        <observer name="update_warehouse_timezone" instance="vendor\module\Observer\UpdateWarehouseTimezone"/>
    </event>
</config>

Observer/UpdateWarehouseTimezone.php

<?php namespace vendor\module\Observer;
use Magento\Framework\Event\ObserverInterface;
/**
 * Class UpdateWarehouseTimezone
 *
 * @package vendor\module\Observer
 */
class UpdateWarehouseTimezone implements ObserverInterface
{
    public function __construct
    (
        \Psr\Log\LoggerInterface $logger,
        \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository
    )
    {
        $this->logger=$logger;
        $this->sourceRepository=$sourceRepository;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $request=$observer->getEvent()->getRequest();
        $requestData=$request->getParam('general', []);
        $type=\vendor\module\Model\WarehouseTimezone::US_EASTERN;
        if(isset($requestData['extension_attributes']) && isset($requestData['extension_attributes']['warehouse_timezone']))
        { $type=$requestData['extension_attributes']['warehouse_timezone']; }
        $sourceCode=$requestData['source_code'];
        //Magento\InventoryApi\Api\SourceRepositoryInterface 
        $source=$this->sourceRepository->get($sourceCode);
        $source->setWarehouseTimezone($type);
        $source->save();
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.