cancel
Showing results for 
Search instead for 
Did you mean: 

Add Shipping Method to Shipping Address in Quote programmatically

Add Shipping Method to Shipping Address in Quote programmatically

Hello, guys.

 

I need to create script that adds orders to Magento. I create quote then submit this quote using \Magento\Quote\Model\QuoteManagement. There are a lot of interfaces and its implementations in Magento 2. In my case these are \Magento\Quote\Api\Data\AddressInterface and \Magento\Quote\Model\Quote\Address. I suppose I should use interface rather than its implementation to operate with objects:

 

$addr = ObjectManager::getInstance()->create(\Magento\Quote\Api\Data\AddressInterface::class);

I see that this interface contains only 46 public methods (and 4 methods in the parent interface), but the implementation contains 73 annotated methods (@method in docblock) and 88 public methods in the class body (public function). In total we have 50 (46+4) methods in interface and >150 methods in implementation. It's OK, not all implementation's methods are important on the application level. But should be the way to set important information into the object and this way should be declared in the interfaces.

 

In my case I want to set shipping method to the quote. I see validation code in \Magento\Quote\Model\QuoteValidator::validateBeforeSubmit:

 

$method = $quote->getShippingAddress()->getShippingMethod();
$rate = $quote->getShippingAddress()->getShippingRateByCode($method);

 and I suppose should be the setter in the quote address interface or in quote interface (\Magento\Quote\Api\Data\CartInterface)

 

public function setShippingMethod($method);

or should be the method in the Quote(Cart)Manager interface (\Magento\Quote\Api\CartManagementInterface):

public function setShippingMethod($cartId, $method)

or should be declared interface for other manager (ShippingAddressManager | ShippingMethodManager | ...) that gives availability to set shipping method to shipping address in quote. But I found nothing.

 

Is any interface in Magento 2 that declares method to setup shipping method into the quote shipping address or into the quote itself?