-2

I am new to Magento2.

I have created a new shipment method programmatically and name it "Same Day Delivery". Also, I have created a product attribute programmatically and name it "Same Day Delivery Available: (Yes/No).

I would like to add the restriction on the Checkout page - If all items in cart have (Same Day Delivery Available as Yes). Then display the new shipment option or hide them in the shipment selection section.

Here is my source code: https://github.com/balajimrv/app

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 2
    You need to have your code etc. needed to answer your question here, no links to 3rd party sites please. – James Z May 13 '22 at 04:36

1 Answers1

0

In the method Balaji\CustomShipping\Model\Carrier\Customshipping::collectRates add after the enabled verification

if (!$this->getConfigFlag('active')) {
    return false;
}

this code that checks if same day delivery is available

foreach ($request->getAllItems() as $item) {
    if (!$item->getProduct()->getData('same_day_delivery_available')) { //use here the real attribute code you have on the product
        return false;
    }
}

you also need to tell magento to load your attribute when loading partial product data in checkout. For this create the file etc/catalog_attributes.xml with this content

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">    
    <group name="quote_item">
        <attribute name="same_day_delivery_available"/><!-- Use here the real attribute code -->
    </group>
    
</config>
Marius
  • 15,148
  • 9
  • 56
  • 76