Enabling Free Shipping for Certain Magento Products Only
If you’re using a table rate shipping system in your Magento store and want to apply free shipping only to certain products based on specific attributes, you might encounter issues when both regular and free-shipping eligible items are present in the cart. In this post, we’ll explore how to ensure that free shipping is calculated correctly for mixed baskets.
Understanding the Current Behavior
By default, Magento’s table rate system calculates shipping costs based on the total order amount, including all products in the basket. If you have a free shipping product with the attribute “free_shipping” set to “yes”, and another regular item in the cart, the free shipping will apply to the combined order total.
This leads to incorrect shipping calculations when both types of products coexist in the customer’s shopping cart. To resolve this issue and ensure that free shipping is applied only to eligible items, we need to modify the way Magento processes the rules for mixed baskets.
Implementing a Custom Solution
To achieve the desired behavior, you’ll need to create an observer that intercepts the calculation of the table rates before they are processed by the default Magento functionality. This observer will filter out any non-eligible products from the total order amount used for shipping calculations.
Here are the general steps involved in implementing this solution:
-
Create a custom module: First, you need to create a new module in your Magento installation. Name it something like “FreeShippingForEligibleProducts”.
-
Create an observer: In the
etcfolder of your newly created module, add an XML file namedevents.xml. Inside this file, define a new event observer that listens for thesales_quote_collect_totals_beforeevent.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/events.xsd">
<event name="sales_quote_collect_totals_before">
<observer name="free_shipping_for_eligible_products" instance="Magento\FreeShippingForEligibleProducts\Observer\ApplyFreeShippingToEligibleItemsOnly"/>
</event>
</config>
- Create the observer class: In your module, create a file named
Observer.phpin theObserverdirectory. This is where you’ll implement the logic to filter out non-eligible products from the order total.
namespace Magento\FreeShippingForEligibleProducts\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote\Item;
use Magento\SalesRule\Model\Rules\Applicability\ConditionManager;
class ApplyFreeShippingToEligibleItemsOnly implements ObserverInterface
{
protected $conditionManager;
public function __construct(ConditionManager $conditionManager)
{
$this->conditionManager = $conditionManager;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$quote = $observer->getQuote();
foreach ($quote->getAllVisibleItems() as $item) {
if (!$this->isItemEligibleForFreeShipping($item)) {
$item->setRowTotal(0);
$item->setTaxAmount(0);
}
}
return $this;
}
protected function isItemEligibleForFreeShipping(Item $item)
{
// Implement your logic here to check if the item is eligible for free shipping
// You can use the attribute "free_shipping" or any other criteria specific to your store
return (bool) $item->getProduct()->getData('free_shipping');
}
}
In this observer class, we loop through all visible items in the cart and check if each item is eligible for free shipping. If an item is not eligible, we set its row total and tax amount to zero, effectively removing it from the order total used for shipping calculations.
- Enable your module: Finally, make sure to enable your custom module by adding it to the
app/etc/modman/manifest.jsonfile of your Magento installation.
Conclusion
To ensure that free shipping is applied correctly in Magento when both eligible and non-eligible products are present in a customer’s shopping cart, you need to create a custom observer that filters out non-eligible items from the order total used for shipping calculations. By implementing this solution as described above, you can maintain accurate and fair shipping rates for your customers, regardless of the product mix in their baskets.
Remember to test this implementation thoroughly to ensure it meets your specific requirements and works seamlessly with your existing Magento setup.
For more information on customizing Magento’s shipping functionality, consider exploring our comprehensive guides at LaravelCompany.