3

I am working on a custom module. I need order data in my own controller. I want users to use the checkboxes to select orders so i can use the order id's/data in my controller. How do I do this?

What I have done so far:

  1. Make module
  2. Add massaction with custom url to the sales order grid
  3. Setup routing for my custom url
  4. Created controller so my custom url works

I don't know how I can get the selected order id's in my controller. I asked this: Magento 2: How to get Post data in backend/adminhtml? But the POST Data doesn't contain the order id's.

How can I proceed? What is the next step? And how do I achieve that?

Sourav
1,2988 silver badges16 bronze badges
asked Jan 10, 2016 at 19:33
1
  • Hello , Get ids using Custom Mass Action. Commented May 11, 2018 at 2:50

2 Answers 2

3

\Magento\Ui\Component\MassAction\Filter::getCollection is responsible for adding filter criteria to your collection and you can iterate through it to do some mass action. It will obtain \Magento\Ui\Component\MassAction\Filter::(SELECTED|EXCLUDED)_PARAM param from request and use it as filter for collection. See \Magento\Catalog\Controller\Adminhtml\Product\MassDelete for example.

answered Jan 10, 2016 at 20:17
1
  • Thanks, how can I implement this? I am new to Magento 2. A tutorial or guide with explanaition would be awesome. I couldn't find any sepcific for this task. Commented Jan 11, 2016 at 13:20
1

To complete @KAndy answer, as of magento 2.2 (not tested on previous version), you can't get ids from request. You must use \Magento\Ui\Component\MassAction\Filter::getCollection to getAllIds.

Here a full example :

In your ui_component : (ex : routefrontendname_index_listing.xml)

<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" component="Magento_Ui/js/grid/tree-massactions">
 <action name="delete">
 <settings>
 <confirm>
 <message translate="true">Are you sure you want to delete selected items?</message>
 <title translate="true">Delete items</title>
 </confirm>
 <url path="routeFrontendName/index/massDelete"/>
 <type>delete</type>
 <label translate="true">Delete</label>
 </settings>
 </action>
 </massaction>
...
 </listingToolbar>
...
</listing>

Controller MassDelete :

 <?php
namespace Vendor\ModuleName\Controller\Adminhtml\Index;
use \Magento\Backend\App\Action;
use \Magento\Ui\Component\MassAction\Filter;
use \Vendor\ModuleName\Model\ResourceModel\YourModel\CollectionFactory;
use \Vendor\ModuleName\Model\YourModelRepository;
class MassDelete extends \Magento\Backend\App\Action
{
 /**
 * @var Filter
 */
 protected $filter;
 /**
 * @var CollectionFactory
 */
 protected $collectionFactory;
 /**
 * @var YourModelRepository
 */
 protected $yourModelRepository;
 public function __construct(
 Action\Context $context,
 Filter $filter,
 CollectionFactory $collectionFactory,
 YourModelRepository $yourModelRepository
 ) {
 parent::__construct($context);
 $this->filter = $filter;
 $this->collectionFactory = $collectionFactory;
 $this->yourModelRepository = $yourModelRepository;
 }
 public function execute()
 {
 $collection = $this->filter->getCollection($this->collectionFactory->create());
 try {
 $entityDeleted = 0;
 foreach ($collection->getAllIds() as $id) {
 $this->yourModelRepository->deleteById($id);
 $entityDeleted++;
 }
 $this->messageManager->addSuccess(__('A total of %1 record(s) were deleted.', $entityDeleted));
 } catch (\Exception $e) {
 $this->messageManager->addError(__($e->getMessage()));
 }
 $resultRedirect = $this->resultRedirectFactory->create();
 return $resultRedirect->setPath('*/*/index', array('_current' => true));
 }
}
answered May 11, 2018 at 2:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.