How to Add Company Column in Abandoned Carts Report in Magento 2
The default Magento 2 offers an abandoned carts report that lists all registered customers who have abandoned carts which is yet to be expired.
The abandoned carts report grid lists the below details:
- Customer Name
- Email Address
- Number of products in the cart
- Cart Subtotal
- Applied Coupon
- Date created
- Date of the last update
- IP address
These details can help the store owner track the reason for the cart abandonment and strive for customer retention. Cart abandonment can cost the business all the efforts, time, and money put to bring the valuable traffic to the site.
In March 2020, 88.05% of online shopping orders were abandoned – Statista
To reduce this number, the store owners employ various tactics using the customer data that is collected by configuring customer accounts in Magento 2 store.
It may happen, depending on the nature of the business, that some useful customer data is not included in the abandoned carts report in Magento 2 default grid.
For example, the customer’s company name that is collected during registration can be helpful to decide their purchase power and including it in the abandoned carts report can make the task easier.
To add company column in abandoned carts report in Magento 2 can be easy with the method given in this post. However, do not forget to first enable company name in customer account in Magento 2 from the admin panel.
The default Abandoned carts report in Magento 2 looks something like this:
Stepwise method to add company column in abandoned carts report in Magento 2:
- Create registration.php file at app\code\Vendor\Module directory
1234<?phpuse \Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__); - Create module.xml file at app\code\Vendor\Module\etc directory
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"/></config> - Create di.xml file at app\code\Vendor\Module\etc\adminhtml directory
1234<?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\Reports\Block\Adminhtml\Shopcart\Abandoned\Grid" type="Vendor\Module\Block\Adminhtml\Shopcart\Abandoned\Grid" /></config> - Create Grid.php at app\code\Vendor\Module\Block\Adminhtml\Shopcart\Abandoned directory
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218<?phpnamespace Vendor\Module\Block\Adminhtml\Shopcart\Abandoned;class Grid extends \Magento\Reports\Block\Adminhtml\Grid\Shopcart{protected $_quotesFactory;public function __construct(\Magento\Backend\Block\Template\Context $context,\Magento\Backend\Helper\Data $backendHelper,\Magento\Reports\Model\ResourceModel\Quote\CollectionFactory $quotesFactory,array $data = []) {$this->_quotesFactory = $quotesFactory;parent::__construct($context, $backendHelper, $data);}protected function _construct(){parent::_construct();$this->setId('gridAbandoned');}protected function _prepareCollection(){$collection = $this->_quotesFactory->create();$filter = $this->getParam($this->getVarNameFilter(), []);if ($filter) {$filter = base64_decode($filter);parse_str(urldecode($filter), $data);}if (!empty($data)) {$collection->prepareForAbandonedReport($this->_storeIds, $data);} else {$collection->prepareForAbandonedReport($this->_storeIds);}$this->setCollection($collection);parent::_prepareCollection();if ($this->_isExport) {$collection->setPageSize(null);}$this->getCollection()->resolveCustomerNames();return $this;}protected function _addColumnFilterToCollection($column){$field = $column->getFilterIndex() ? $column->getFilterIndex() : $column->getIndex();$skip = ['subtotal', 'customer_name', 'email'];if (in_array($field, $skip)) {return $this;}parent::_addColumnFilterToCollection($column);return $this;}protected function _prepareColumns(){$this->addColumn('customer_name',['header' => __('Customer'),'index' => 'customer_name','sortable' => false,'header_css_class' => 'col-name','column_css_class' => 'col-name']);$this->addColumn('email',['header' => __('Email'),'index' => 'email','sortable' => false,'header_css_class' => 'col-email','column_css_class' => 'col-email']);//add Company field$this->addColumn('company',['header' => __('Company'),'index' => 'company','sortable' => false,'renderer' => \Meetanshi\Register\Block\Adminhtml\Grid\Column\Renderer\Company::class,'header_css_class' => 'col-company','column_css_class' => 'col-company']);$this->addColumn('items_count',['header' => __('Products'),'index' => 'items_count','sortable' => false,'type' => 'number','header_css_class' => 'col-number','column_css_class' => 'col-number']);$this->addColumn('items_qty',['header' => __('Quantity'),'index' => 'items_qty','sortable' => false,'type' => 'number','header_css_class' => 'col-qty','column_css_class' => 'col-qty']);if ($this->getRequest()->getParam('website')) {$storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();} elseif ($this->getRequest()->getParam('group')) {$storeIds = $this->_storeManager->getGroup($this->getRequest()->getParam('group'))->getStoreIds();} elseif ($this->getRequest()->getParam('store')) {$storeIds = [(int)$this->getRequest()->getParam('store')];} else {$storeIds = [];}$this->setStoreIds($storeIds);$currencyCode = $this->getCurrentCurrencyCode();$this->addColumn('subtotal',['header' => __('Subtotal'),'type' => 'currency','currency_code' => $currencyCode,'index' => 'subtotal','sortable' => false,'renderer' => \Magento\Reports\Block\Adminhtml\Grid\Column\Renderer\Currency::class,'rate' => $this->getRate($currencyCode),'header_css_class' => 'col-subtotal','column_css_class' => 'col-subtotal']);$this->addColumn('coupon_code',['header' => __('Applied Coupon'),'index' => 'coupon_code','sortable' => false,'header_css_class' => 'col-coupon','column_css_class' => 'col-coupon']);$this->addColumn('created_at',['header' => __('Created'),'type' => 'datetime','index' => 'created_at','filter_index' => 'main_table.created_at','sortable' => false,'header_css_class' => 'col-created','column_css_class' => 'col-created']);$this->addColumn('updated_at',['header' => __('Updated'),'type' => 'datetime','index' => 'updated_at','filter_index' => 'main_table.updated_at','sortable' => false,'header_css_class' => 'col-updated','column_css_class' => 'col-updated']);$this->addColumn('remote_ip',['header' => __('IP Address'),'index' => 'remote_ip','sortable' => false,'header_css_class' => 'col-ip','column_css_class' => 'col-ip']);$this->addExportType('*/*/exportAbandonedCsv', __('CSV'));$this->addExportType('*/*/exportAbandonedExcel', __('Excel XML'));return parent::_prepareColumns();}/*** Get rows url** @param \Magento\Framework\DataObject $row** @return string*/public function getRowUrl($row){return $this->getUrl('customer/index/edit', ['id' => $row->getCustomerId(), 'active_tab' => 'cart']);}} - Create Company.php at app\code\Vendor\Module\Block\Adminhtml\Grid\Column\Renderer and add below code
123456789101112131415161718192021222324252627282930313233<?phpnamespace Vendor\Module\Block\Adminhtml\Grid\Column\Renderer;use Magento\Backend\Block\Context;use Magento\Customer\Model\CustomerFactory;use Magento\Framework\DataObject;class Company extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer{protected $customerFactory;public function __construct(Context $context, array $data = array(),CustomerFactory $customerFactory) {$this->customerFactory = $customerFactory;parent::__construct($context, $data);}public function render(DataObject $row){$customerFactory = $this->customerFactory->create();$customerId = $row->getcustomer_id();$customer = $customerFactory->load($customerId);$Addresses = $customer->getAddresses();foreach ($Addresses as $address){return $address->getData('company');}}}
That’s it.
Now, if you’ll check, the abandoned carts report in Magento 2 will have the company column added:
Any doubts about the solution can be mentioned in the Comments section below. I’d be happy to help you out.
Also, do share the solution with the Magento community via social media.
Thanks.
Related Posts:
- How to Add a Custom Column in Magento 2 Products Grid
- How to Add Custom Column in Order Grid in Magento 2
- How to Add Category Column to Product Grid in Magento 2
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Hello!
I would to Add “product name” Column in Abandoned Carts Report in Magento 2, I tried with this example, but I not have a success.
Could you help me in this case?
Hello Raphael,
To get the product name, you first have to get quote_id from the current raw
and then based on quote_id, you can get the product name from quote_item table.
Thank You.