2

I made a custom mass action for the product grid. This is the ui component part:

<?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="barcode">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">barcode</item>
                        <item name="label" xsi:type="string" translate="true">Create Barcode(s)</item>
                        <item name="url" xsi:type="url" path="modulename_product_barcode/product/massCreateBarcode"/>
                    </item>
                </argument>
            </action>
        </massaction>
    </listingToolbar>
</listing>

In my controller I put this code:

    public function execute()
    {
        var_dump($_POST); exit();
...
    }

The result of the var_dump is:

array (size=5)
  'selected' => 
    array (size=1)
      0 => string '1' (length=1)
  'filters' => 
    array (size=1)
      'placeholder' => string 'true' (length=4)
  'search' => string '' (length=0)
  'namespace' => string 'product_listing' (length=15)
  'form_key' => string 'fgqSDhnd44Eao74Y' (length=16)

As you can see the selected which contains the product entity ids is present! If I do a search in the product grid after the sku, the result of the var_dump is:

array (size=5)
  'excluded' => string 'false' (length=5)
  'filters' => 
    array (size=1)
      'placeholder' => string 'true' (length=4)
  'search' => string 'SP' (length=2)
  'namespace' => string 'product_listing' (length=15)
  'form_key' => string 'fgqSDhnd44Eao74Y' (length=16)

Here the selected is missing. Not sure why ? So how can i get the product entity ids after searching or filtering the product grid for a mass action ? Thank you

2

I had a look how the mass delete is done in the core and i saw that i missed some parts.

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Ui\Component\MassAction\Filter;
......

/**
* @var Filter
*/
private $filter;
/**
* @var CollectionFactory
*/
private $collectionFactory;
......

public function execute()
{
......
        $productIds = $this->getRequest()->getParam('selected');
        if (!$productIds) {   //for searching and filters, when the `selected` key is not present
            $collection = $this->filter->getCollection($this->collectionFactory->create());
            $productIds = $collection->getColumnValues('entity_id');

        }
......

}

I hope my answer will help someone :) Cheers!

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.