Enabling Store Pickup for Specific Magento Products
As an experienced e-commerce developer, I frequently help merchants optimize their checkout experiences using Magento’s advanced features. One common request is to enable or disable shipping methods based on specific product attributes in the cart.
In this post, we’ll explore how to achieve this functionality by manipulating the estimate-shipping-methods API endpoint and extending core Magento modules. Let me walk you through a practical approach that leverages Magento 2’s event observers and API responses.
Understanding the Magento Checkout Process
Before diving into the code, it’s essential to grasp how the checkout process works in Magento 2:
- The customer adds products to their cart.
- The
estimate-shipping-methodsAPI endpoint (POST /rest/en/V1/guest-carts/SESSION_ID/estimate-shipping-methods) is called to fetch available shipping methods based on the current cart items.
Extending Core Magento Modules
To customize the shipping method estimation, we need to extend the module-checkout and module-quote modules. First, create a custom module using the following structure:
app/code/Vendor/DisableShippingMethod/etc/
âââ di.xml
In your di.xml file, declare the observers for the required events:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\GuestCartManagement">
<plugin name="guestcart_estimate_shipping_after" type="Vendor\DisableShippingMethod\Plugin\QuoteManagement" sortOrder="10"/>
</type>
</config>
Next, create the plugin responsible for filtering shipping methods:
namespace Vendor\DisableShippingMethod\Plugin;
class QuoteManagement
{
public function afterEstimateShipping(\Magento\Quote\Model\GuestCartManagement $subject, \Magento\Quote\Api\Data\ShippingMethodInterface[] $result)
{
foreach ($result as &$method) {
$carrierCode = $method->getCarrierCode();
// Check if the carrier code matches your desired condition (e.g., 'flatrate_fixed')
if ($carrierCode === 'flatrate_fixed') {
continue; // Skip this shipping method
}
}
return $result;
}
}
Implementing the Attribute-based Filter
To enable store pickup only for certain products, you’ll need to use a product attribute such as pickup_enabled. Here’s an example of how to filter shipping methods based on this attribute:
foreach ($result as &$method) {
$carrierCode = $method->getCarrierCode();
if ($carrierCode === 'flatrate_fixed') {
$productIds = array_column($subject->getQuote()->getItems(), 'product_id');
foreach ($productIds as $productId) {
$product = $this->_productRepository->getById($productId);
$pickupEnabled = $product->getData('pickup_enabled');
if ($pickupEnabled === 1) {
continue; // Skip this shipping method for pickup-enabled products
}
}
}
}
Conclusion
In this blog post, we explored how to disable specific shipping methods based on product attributes during the Magento 2 checkout process. By extending core modules and leveraging event observers, we can create a seamless customer experience tailored to your business needs.
Remember, always test your customizations thoroughly to ensure they don’t introduce any performance or compatibility issues with your existing extensions. If you require further assistance in implementing this feature or have other Magento development questions, feel free to reach out to the expert team at Laravel Company.
Happy coding!