I have couple of shipping methods DHL & FedEx, if selected products exits in cart show only selected shipping method, how to archive using shopping cart pricing rule or any other way to do this?

Dani AI

Generated

Short answer for : shopping cart price rules (promotions/free shipping) won’t hide or whitelist carriers — they only change pricing or enable free shipping. For carrier-level control use a shipping-restrictions extension (as suggested) or a small checkout customization that filters available methods based on products in the quote.

A practical, robust pattern:

  • Add a product attribute (example name allowed_shipping) containing carrier codes (e.g. dhl, fedex) or leave empty for “no restriction.”
  • Decide the business rule: take the intersection of allowed carriers across all cart items (recommended — only show carriers that work for every item) or show the union (rare, can confuse checkout).
  • Filter the checkout shipping-method list server-side so real-time carrier rates are still requested but only permitted carriers are returned.

Magento 2 example (plugin idea): wrap \Magento\Quote\Model\ShippingMethodManagement::getList, collect quote items, compute the intersection of allowed_shipping values, then return only methods whose getCarrierCode() is in that set.

// simplified around plugin sketch
public function aroundGetList($subject, $proceed, \Magento\Quote\Api\Data\AddressInterface $address) {
    $methods = $proceed($address);
    $quote = $this->checkoutSession->getQuote();
    $items = $quote->getAllVisibleItems();
    $allowed = null;
    foreach ($items as $i) {
        $val = $i->getProduct()->getData('allowed_shipping'); // "dhl,fedex"
        if ($val) { $codes = array_map('trim', explode(',', $val));
            $allowed = $allowed === null ? $codes : array_intersect($allowed, $codes);
        }
    }
    if (!$allowed) { return $methods; }
    return array_filter($methods, function($m) use ($allowed) { return in_array($m->getCarrierCode(), $allowed); });
}

Notes / testing tips: confirm actual carrier codes by logging getCarrierCode(), apply the attribute to the correct SKU (simple vs parent), flush cache/reindex after deployment, test guest and customer flows, and consider storing allowed codes on the quote item at add-to-cart to avoid repeated product loads. If configuration is preferred over code, a reputable shipping-restrictions extension is the fastest route.

Here is a must-try tool on Magento 2 store: Shipping Restrictions. Let see how it brings your shipping system the best performance.
This module is super recommended for online stores who provide multiple shipping methods and they have to deal with the enormous amount of customers. In addition, the add-on is built exclusively for stores selling mass and various products quantity daily. Since it assists store owners to control their shipping system, it also contributes to business efficiency and profitability.

Elsner Magento Extensions can bring magic to your ecommerce store.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.