Company custom fields's value not saving in company admin form(Magento b2b) - magento

I have created some custom fields in company table. After creating the fields in company form, i have used one event to save the custom value in database. So, first time the value is saving in database but it is not saving in UI component form rather it is showing as a blank.
customer_form.xml
<fieldset name="proofofbusiness" sortOrder="100">
<settings>
<collapsible>true</collapsible>
<label> Proof of Business </label>
</settings>
<field name="gst_number" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">gst_number</item>
</item>
</argument>
<settings>
<label translate="true">GST Number</label>
<dataType>text</dataType>
<dataScope>gst_number</dataScope>
</settings>
</field>
</fieldset>
2)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">
<preference for="Magento\Company\Model\Company\DataProvider" type="Arijit\CustomerShop\Model\CustomerShop\DataProvider" />
<type name="Magento\Company\Model\Company\DataProvider">
<plugin name="after_save_company_additional_field"
type="Arijit\CustomerShop\Plugin\AdditionalDataProvider"/>
</type>
</config>
3)db_schema.xml
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="company" resource="default" engine="innodb">
<column xsi:type="varchar" name="gst_number" nullable="true"/>
</table>
</schema>
Dataprovider.php(Added custom field)
public function getProofOfBusiness(CompanyInterface $company)
{
return [
'gst_number'=> $company->getGstNumber(),
];
}

I can see that the fieldset name is not matching the function name that returns its data. The fieldset name is "proofofbusiness" so your function in DataProvider.php should be named getProofofbusinessData().

Related

How to show field in custom sales rule form based on value of another field

I want to display my custom field in sales rule form only if field simple_data (label Apply) is set to value by_percent. I added this xml in my module:
<?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="actions" sortOrder="30">
<field name="simple_action" formElement="select">
<settings>
<switcherConfig>
<rules>
<rule name="0">
<value>by_percent</value>
<actions>
<action name="0">
<target>sales_rule_form.sales_rule_form.actions.is_base_price_discount</target>
<callback>show</callback>
</action>
</actions>
</rule>
</rules>
</switcherConfig>
</settings>
</field>
<field name="is_base_price_discount" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">sales_rule</item>
<item name="default" xsi:type="number">0</item>
<item name="sortOrder" xsi:type="number">20</item>
</item>
</argument>
<settings>
<dataType>boolean</dataType>
<label translate="true">Discount from base price</label>
<visible>true</visible>
<dataScope>is_base_price_discount</dataScope>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="number">0</map>
<map name="true" xsi:type="number">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
</fieldset>
</form>
But it still appears with any option selected. How to set it correctly?
enter image description here

Properly add new columns to sales order grid in Magento 2

I created a module to add 2 columns in sales grid, shipping_information and tracking number.
The columns appears but when I tried to filter by Order ID in admin order page, example with 973, I got this error
main.CRITICAL: Item (Magento\Framework\View\Element\UiComponent\DataProvider\Document\Interceptor) with the same ID "6245" already exists. {"exception":"[object] (Exception(code: 0): Item (Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\Document\\Interceptor) with the same ID \"6245\" already exists. at /home/xxxx/public_html/vendor/magento/framework/Data/Collection.php:404)"}
Also this error
Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous
But when I tried with another ID, the error disappear
In Vendor/Module/Model/ResourceModel/Order/Grid/Collection.php
namespace Wetag\AdditionalOrderFields\Model\ResourceModel\Order\Grid;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
use Psr\Log\LoggerInterface as Logger;
/**
* Order grid extended collection
*/
class Collection extends OriginalCollection
{
protected $helper;
public function __construct(
EntityFactory $entityFactory,
Logger $logger,
FetchStrategy $fetchStrategy,
EventManager $eventManager,
$mainTable = 'sales_order_grid',
$resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
)
{
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
}
protected function _renderFiltersBefore()
{
$joinTable = $this->getTable('sales_order');
$joinTable_track = $this->getTable('sales_shipment_track');
$this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order.entity_id', ['shipping_information'])->distinct();
$this->getSelect()->joinLeft($joinTable_track, 'sales_order.entity_id = sales_shipment_track.order_id', ['track_number'])->distinct();
parent::_renderFiltersBefore();
}
protected function _initSelect() {
$this->addFilterToMap('increment_id', 'main_table.increment_id');
$this->addFilterToMap('status', 'main_table.status');
parent::_initSelect();
return $this;
}
}
In Vendor/Module/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">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="sales_order_grid_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="Vendor\Module\Model\ResourceModel\Order\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">sales_order_grid</argument>
<argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
</arguments>
</type>
</config>
In etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_sales" />
</sequence>
</module>
</config>
In Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="shipping_information">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Shipping method</item>
</item>
</argument>
</column>
<column name="track_number">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Tracking number</item>
</item>
</argument>
</column>
</columns>
</listing>
I like to use the field shipping_information in order_sales_grid table, I didn’t get it without making a left join, I like to select this field without join
I solved the issue by grouping by entity_id ->group('main_table.entity_id') to remove duplicaded rows and delete LeftJoin to Sales_order table since shipping_information already exists in sales_order_grid table
protected function _renderFiltersBefore()
{
$joinTable_track = $this->getTable('sales_shipment_track');
$this->getSelect()->joinLeft($joinTable_track, 'main_table.entity_id = sales_shipment_track.order_id', ['track_number'])->group('main_table.entity_id');
parent::_renderFiltersBefore();
}

Magento 2 dynamicRows as new tab for admin customer edit, how do you get data loaded?

How can I load the data from customer DataProvider plugin into my ui_component dynamicRows?
I have created a new tab in the admin customer edit form, and added my ui_component, which seems to work well. But I can't populate the data already stored in my database.
I have tried to add xml for DataSource, but I think that has conflict with original customer_form.xml DataSource "customer_form.customer_form_data_source" because when I add DataSource, the page no longer loads.
dataSource XML:
<dataSource name="mycustomrows">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">MyVendor\MyModule\Model\Customer\DataProvider</argument>
<argument name="primaryFieldName" xsi:type="string">id</argument>
<argument name="requestFieldName" xsi:type="string">id</argument>
<argument name="meta" xsi:type="array">
<item name="myshipping" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">My Custom Rows</item>
</item>
</item>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
</item>
</argument>
</dataSource>
dataProvider for dataSource:
<?php
namespace MyVendor\MyModule\Model\Customer;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
protected $loadedData;
protected $collectionFactory;
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
\MyVendor\MyModule\Model\ResourceModel\Row\Collection $collection,
\MyVendor\MyModule\Model\ResourceModel\Row\CollectionFactory $collectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $collection;
$this->collectionFactory = $collectionFactory;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$collection = $this->collectionFactory->create();
// add Filter for Customer ID
//->setOrder('position', 'ASC');
$items = $collection->getItems();
foreach ($items as $item) {
$this->loadedData[$item->getId()] = $item->getData();
}
return $this->loadedData;
}
}
I believe the data has to be loaded with Plugin for "Magento\Customer\Model\Customer\DataProvider" as I am doing below, but I can't seem to get populate rows on load.
view/adminhtml/ui_component/customer_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="mycustomrows">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">My Custom Rows</item>
<item name="sortOrder" xsi:type="number">10</item>
</item>
</argument>
<dynamicRows name="mycustomrows">
<settings>
<addButtonLabel translate="true">Add Row</addButtonLabel>
<additionalClasses>
<class name="admin__field-wide">true</class>
</additionalClasses>
<componentType>dynamicRows</componentType>
</settings>
<container name="record" component="Magento_Ui/js/dynamic-rows/record">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="isTemplate" xsi:type="boolean">true</item>
<item name="is_collection" xsi:type="boolean">true</item>
<item name="componentType" xsi:type="string">container</item>
<item name="positionProvider" xsi:type="string">position</item>
</item>
</argument>
<field name="mycustomrows_select1" formElement="select" sortOrder="10">
<settings>
<dataType>text</dataType>
<label translate="true">Select Field 1</label>
<disabled>false</disabled>
<dataScope>mycustomrows_select1</dataScope>
</settings>
<formElements>
<select>
<settings>
<options class="MyVendor\MyModule\Model\Source\SelectFieldOne"/>
</settings>
</select>
</formElements>
</field>
<field name="mycustomrows_input1" sortOrder="20" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">customer</item>
</item>
</argument>
<settings>
<label translate="true">Text Field 1</label>
<dataType>text</dataType>
<dataScope>mycustomrows_text1</dataScope>
</settings>
</field>
<actionDelete sortOrder="30">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="componentType" xsi:type="string">actionDelete</item>
<item name="dataType" xsi:type="string">text</item>
<item name="fit" xsi:type="boolean">false</item>
<item name="label" xsi:type="string">Actions</item>
<item name="sortOrder" xsi:type="string">500</item>
<item name="additionalClasses" xsi:type="string">data-grid-actions-cell</item>
<item name="template" xsi:type="string">Magento_Backend/dynamic-rows/cells/action-delete</item>
</item>
</argument>
</actionDelete>
</container>
</dynamicRows>
</fieldset>
</form>
etc/adminhtml/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\Customer\Model\Customer\DataProvider">
<plugin name="customer_get_mycustomrows" type="MyVendor\MyModule\Model\Customer\DataProvider" sortOrder="5" />
</type>
</config>
Model/Customer/DataProvider.php
<?php
namespace MyVendor\MyModule\Model\Customer;
class DataProvider
{
protected $collection;
protected $collectionFactory;
protected $loadedData;
public function __construct(
\MyVendor\MyModule\Model\ResourceModel\Row\Collection $collection,
\MyVendor\MyModule\Model\ResourceModel\Row\CollectionFactory $collectionFactory,
array $data = []
) {
$this->collection = $collection;
$this->collectionFactory = $collectionFactory;
}
public function afterGetData(\Magento\Customer\Model\Customer\DataProvider $subject, $result)
{
if($result){
$customer_id = key($result);
$customerData = $result[$customer_id]['customer'];
if(is_null($this->loadedData)) {
$this->loadedData = array();
$collection = $this->collectionFactory->create(); //->setOrder('position', 'ASC');
$items = $collection->getItems();
foreach ($items as $item) {
$this->loadedData[] = $item->getData();
}
}
$result[$customer_id]['customer']["mycustomrows"] = $this->loadedData;
}
return $result;
}
}
I can't seem to get any records populated when I load a customer edit page.

magento 2.0 and 2.1 massaction compatibility issue admin back-end

We have a custom written Magento 2 module that adds an action to the order overview dropdown.
Now have we ran into an issue where it would work on 2.0.x but not 2.1.x and up.
After a bit of research it is caused by Magento changing an xml node from container to listingToolbar.
Now my question is, how do we make our module compatible with both 2.0.x and 2.1.x?
Adding both; container and listingToolbar will break the massaction dropdown.
magento 2.0.x version:
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<container name="listing_top">
<massaction name="listing_massaction">
<action name="new_action">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">action_name</item>
<item name="label" xsi:type="string" translate="true">Action Name Text Here</item>
<item name="url" xsi:type="url" path="vendor_module/action"/>
</item>
</argument>
</action>
</massaction>
</container>
magento 2.1.x version:
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
<massaction name="listing_massaction">
<action name="new_action">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">action_name</item>
<item name="label" xsi:type="string" translate="true">Action Name Text Here</item>
<item name="url" xsi:type="url" path="vendor_module/action"/>
</item>
</argument>
</action>
</massaction>
</listingToolbar>

Magento2 extension: add link to product grid

I'm developing extension for magento 2.1.3 ce. I want to add a link to each product in the products grid in admin-panel:
I want to have a link in this column, how to change my extension to have a link instead of plain text?
My Magento extension code (app\code\MyCompany\ExampleAdminNewPage\view\adminhtml\ui_component\product_listing.xml):
<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
<column name="sku">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Custom Field2</item>
<item name="sortOrder" xsi:type="number">75</item>
</item>
</argument>
</column>
</columns>
</listing>
in pseudo code what i want is:
<column name="{http://mysite/}"+"sku">
If the URL is internal, try this:
Step 1: config the column like this:
<column name="custom_field2" class="MyCompany\ExampleAdminNewPage\Ui\Component\Listing\Column\HTMLLink">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<!-- other configurations -->
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
</item>
</argument>
</column>
Step 2: create file <Magento_ROOT>/app/code/MyCompany\ExampleAdminNewPage/Ui/Component/Listing/Column/HtmlLink.php
use Magento\Framework\Escaper;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
class HTMLLink extends Column{
protected $escaper;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
Escaper $escaper,
array $components = [],
array $data = []
) {
$this->escaper = $escaper;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource){
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as & $item) {
$html = 'product';
$item[$fieldName] = $this->escaper->escapeHtml($html, ['a']);
}
}
return $dataSource;
}
}

Resources