强制将过滤器应用于销售订单网格

时间:2019-02-20 20:38:04

标签: magento magento2

我正试图强制按状态将文件管理器应用于特定用户角色的订单网格,因此他只能看到已处理的订单。 the same of image, 我在以下代码中获得了角色名称:

class HelperFunctions:

    def comma_split_to_strings(string:str) -> list:
        # Returns a list of strings
        return string.split(',')

    def split_to_floats(string:str) -> list:
        # Returns a 1D list of floats
        return list(map(float,string.split()))


keywords = {
    '_root': str,
    'units': HelperFunctions.comma_split_to_strings,
    'datum': HelperFunctions.split_to_floats
    }

card = {}
key = 'units'
value = 'Pa,kg,km'

if key in keywords:
    cast = keywords[key]

    card[key] = cast(value)

我想为此角色应用过滤器。

1 个答案:

答案 0 :(得分:0)

如果未创建,请创建一个自定义模块。

  

应用程序/代码/ [供应商名称] / [模块名称] /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">[VendorName]\[ModuleName]\Model\ResourceModel\Order\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>
  

应用程序/代码/ [供应商名称] / [模块名称] /模型/资源模型/订单/网格/Collection.php

<?php

namespace [VendorName]\[ModuleName]\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 Psr\Log\LoggerInterface as Logger;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;

/**
 * Order grid extended collection
 */
class Collection extends OriginalCollection
{
    /**
     * @var \Magento\Backend\Model\Auth\Session
     */
    protected $_adminSession;

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        \Magento\Backend\Model\Auth\Session $adminSession,
        $mainTable = 'sales_order_grid',
        $resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
    ) {
        $this->_adminSession = $adminSession;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }
   protected function _renderFiltersBefore()
   {
        $roleData = $this->_adminSession->getUser()->getRole()->getData();
        if ($roleData['role_name'] == '[Name of your role here]') {
            $this->getSelect()->where('status = "processing"');
        }
        parent::_renderFiltersBefore();
   }
}