|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +use Magento\Checkout\Model\Session; |
| 9 | +use Magento\Eav\Model\Config; |
| 10 | +use Magento\Framework\DataObject; |
| 11 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 12 | +use Magento\Quote\Api\Data\AddressInterface; |
| 13 | +use Magento\Quote\Api\Data\PaymentInterface; |
| 14 | +use Magento\Quote\Model\Quote; |
| 15 | +use Magento\Quote\Model\Quote\Address\Rate; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use Magento\TestFramework\ObjectManager; |
| 18 | + |
| 19 | +require __DIR__ . '/../../ConfigurableProduct/_files/configurable_products.php'; |
| 20 | + |
| 21 | +/** @var ObjectManager $objectManager */ |
| 22 | +$objectManager = Bootstrap::getObjectManager(); |
| 23 | + |
| 24 | +$product = $productRepository->getById(10); |
| 25 | +$product->setStockData(['use_config_manage_stock' => 1, 'qty' => 2, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 26 | +$productRepository->save($product); |
| 27 | + |
| 28 | +/** @var Quote $quote */ |
| 29 | +$quote = $objectManager->create(Quote::class); |
| 30 | +$request = $objectManager->create(DataObject::class); |
| 31 | + |
| 32 | +/** @var Config $eavConfig */ |
| 33 | +$eavConfig = $objectManager->get(Config::class); |
| 34 | +/** @var $attribute */ |
| 35 | +$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable'); |
| 36 | + |
| 37 | +$request->setData( |
| 38 | + [ |
| 39 | + 'product_id' => $productRepository->get('configurable')->getId(), |
| 40 | + 'selected_configurable_option' => '1', |
| 41 | + 'super_attribute' => [ |
| 42 | + $attribute->getAttributeId() => $attribute->getOptions()[1]->getValue() |
| 43 | + ], |
| 44 | + 'qty' => '2' |
| 45 | + ] |
| 46 | +); |
| 47 | + |
| 48 | +$quote->setStoreId(1) |
| 49 | + ->setIsActive( |
| 50 | + true |
| 51 | + )->setIsMultiShipping( |
| 52 | + 1 |
| 53 | + )->setReservedOrderId( |
| 54 | + 'test_order_with_configurable_product' |
| 55 | + )->setCustomerEmail( |
| 56 | + 'store@example.com' |
| 57 | + )->addProduct( |
| 58 | + $productRepository->get('configurable'), |
| 59 | + $request |
| 60 | + ); |
| 61 | + |
| 62 | +/** @var PaymentInterface $payment */ |
| 63 | +$payment = $objectManager->create(PaymentInterface::class); |
| 64 | +$payment->setMethod('checkmo'); |
| 65 | +$quote->setPayment($payment); |
| 66 | + |
| 67 | +$addressList = [ |
| 68 | + [ |
| 69 | + 'firstname' => 'Jonh', |
| 70 | + 'lastname' => 'Doe', |
| 71 | + 'telephone' => '0333-233-221', |
| 72 | + 'street' => ['Main Division 1'], |
| 73 | + 'city' => 'Culver City', |
| 74 | + 'region' => 'CA', |
| 75 | + 'postcode' => 90800, |
| 76 | + 'country_id' => 'US', |
| 77 | + 'email' => 'store@example.com', |
| 78 | + 'address_type' => 'shipping', |
| 79 | + ], |
| 80 | + [ |
| 81 | + 'firstname' => 'Antoni', |
| 82 | + 'lastname' => 'Holmes', |
| 83 | + 'telephone' => '0333-233-221', |
| 84 | + 'street' => ['Second Division 2'], |
| 85 | + 'city' => 'Denver', |
| 86 | + 'region' => 'CO', |
| 87 | + 'postcode' => 80203, |
| 88 | + 'country_id' => 'US', |
| 89 | + 'email' => 'customer002@shipping.test', |
| 90 | + 'address_type' => 'shipping' |
| 91 | + ], |
| 92 | +]; |
| 93 | + |
| 94 | +$methodCode = 'flatrate_flatrate'; |
| 95 | +foreach ($addressList as $data) { |
| 96 | + /** @var Rate $rate */ |
| 97 | + $rate = $objectManager->create(Rate::class); |
| 98 | + $rate->setCode($methodCode) |
| 99 | + ->setPrice(5.00); |
| 100 | + |
| 101 | + $address = $objectManager->create(AddressInterface::class, ['data' => $data]); |
| 102 | + $address->setShippingMethod($methodCode) |
| 103 | + ->addShippingRate($rate) |
| 104 | + ->setShippingAmount(5.00) |
| 105 | + ->setBaseShippingAmount(5.00); |
| 106 | + |
| 107 | + $quote->addAddress($address); |
| 108 | +} |
| 109 | + |
| 110 | +/** @var CartRepositoryInterface $quoteRepository */ |
| 111 | +$quoteRepository = $objectManager->get(CartRepositoryInterface::class); |
| 112 | +$quoteRepository->save($quote); |
| 113 | + |
| 114 | +$items = $quote->getAllItems(); |
| 115 | + |
| 116 | +foreach ($quote->getAllShippingAddresses() as $address) { |
| 117 | + foreach ($items as $item) { |
| 118 | + $item->setQty(1); |
| 119 | + $address->setTotalQty(1); |
| 120 | + $address->addItem($item); |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +$billingAddressData = [ |
| 125 | + 'firstname' => 'Jonh', |
| 126 | + 'lastname' => 'Doe', |
| 127 | + 'telephone' => '0333-233-221', |
| 128 | + 'street' => ['Third Division 1'], |
| 129 | + 'city' => 'New York', |
| 130 | + 'region' => 'NY', |
| 131 | + 'postcode' => 10029, |
| 132 | + 'country_id' => 'US', |
| 133 | + 'email' => 'store@example.com', |
| 134 | + 'address_type' => 'billing', |
| 135 | +]; |
| 136 | + |
| 137 | +/** @var AddressInterface $address */ |
| 138 | +$billingAddress = $objectManager->create(AddressInterface::class, ['data' => $billingAddressData]); |
| 139 | +$quote->setBillingAddress($billingAddress); |
| 140 | +$quote->collectTotals(); |
| 141 | +$quoteRepository->save($quote); |
| 142 | + |
| 143 | +/** @var Session $session */ |
| 144 | +$session = $objectManager->get(Session::class); |
| 145 | +$session->setQuoteId($quote->getId()); |
0 commit comments