Wondering how to configure the Free Shipping in Magento 2 store and encountered a problem?
Don’t worry, you’re not the only one.
Been there, done that!
Let me share a recent experience of one of our developers where he had to add a Magento configured free shipping rule for one of our clients.
As he was a new client for us, his Magento store was developed by some other agency and it was our responsibilityonsi to alter the core code and add the Free Shipping Rule to it.
Long story short, the previous developer had mistakenly named Cities’ Grid module calling it a TEST.
As one can suspect, this was not a proper name for the module and does not follow Magento’s Naming Convention or procedures.
Hence, we had to rename the module and make slight changes to it according to the set guidelines by the Magento’s Naming Convention.
First, we had to make a GRID for Cities from the ground up.
So, then we can add the cities according to our preferences and according to our choices.
Also previously, the grid had issues when we tried to edit or remove a row or city.
The code restricted us to delete any row or any city according to the requirement.
So, our next task was to fix this issue.
We thoroughly checked everything and read all the code files.
Debugged the files.
We noticed that the files to delete a row or a city was missing in the core code.
So, fixing that became our top priority.
Following is the code we added to the core code to resolve this issue:
<?php
namespace RLTSquare\CitiesGrid\Controller\Adminhtml\City;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use RLTSquare\CitiesGrid\Model\ResourceModel\City\CollectionFactory;
<code>
/**
* Class MassDelete
* @package RLTSquare\CitiesGrid\Controller\Adminhtml\City
*/
class MassDelete extends \Magento\Backend\App\Action
{
/**
* @var Filter
*/
protected $_filter;
/**
* @var CollectionFactory
*/
protected $_collectionFactory;
/**
* MassDelete constructor.
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory
) {
parent::__construct($context);
$this->_filter = $filter;
$this->_collectionFactory = $collectionFactory;
}
/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute()
{
$collection = $this->_filter->getCollection(
$this->_collectionFactory->create()
);
$recordDeleted = 0;
foreach ($collection->getItems() as $record) {
$record->setId($record->getId());
$record->delete();
$recordDeleted++;
}
$this->messageManager->addSuccessMessage(__(‘A total of %1 record(s) have been deleted.’, $recordDeleted));
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath(‘*/*/index’);
}
/**
* @return bool
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed(‘RLTSquare_CitiesGrid::row_data_delete’);
}
}
</code>
This code enabled us to delete a row completely.
Next, there was another task at hand where we had to build an EDIT action or button in the core code of the store.
Hence according to the requirements of the client, we added some missing cities in the list of cities and deleted some unnecessary ones.
Hence, coming back to the task which led us to do all of this – adding a Free Shipping Rule called as Cart Price Rule.
To do this, we were supposed to add an option named as Free Shipping City in the Drop-Down Menu.
For this to work, we needed another file named: RLTSquare\FreeShippingCities\Model\Rule\Condition\Address.php
We overwrote this file and added the above mentioned Magento 2 Cart Price Rule and Free Shipping.
Then used the collection class from the above grid in the address path:
RLTSquare\FreeShippingCities\Model\Rule\Condition\Address.php
The collection of the cities made earlier in the grid were supposed to be displayed here in the Free Shipping Drop-Down Menu.
Also, we added an option through which the admin of the store can select multiple cities in their drop-down menu for applying Free Shipping Rule.
If you’re facing a similar problem then you might want to try this solution.
As it worked for us, it should work for you.