|
| 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\Catalog\Api\ProductRepositoryInterface; |
| 9 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 10 | +use Magento\Quote\Model\QuoteFactory; |
| 11 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | + |
| 14 | +require __DIR__.'/../../../ConfigurableProduct/_files/product_configurable.php'; |
| 15 | + |
| 16 | +/** @var ProductRepositoryInterface $productRepository */ |
| 17 | +$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); |
| 18 | +/** @var QuoteFactory $quoteFactory */ |
| 19 | +$quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class); |
| 20 | +/** @var QuoteResource $quoteResource */ |
| 21 | +$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class); |
| 22 | +/** @var CartRepositoryInterface $cartRepository */ |
| 23 | +$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class); |
| 24 | + |
| 25 | +/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ |
| 26 | + |
| 27 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 28 | +$productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); |
| 29 | +$product = $productRepository->get('configurable'); |
| 30 | +/* Create simple products per each option */ |
| 31 | +/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */ |
| 32 | +$options = Bootstrap::getObjectManager()->create( |
| 33 | + \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class |
| 34 | +); |
| 35 | +$option = $options->setAttributeFilter($attribute->getId())->getFirstItem(); |
| 36 | + |
| 37 | +$requestInfo = new \Magento\Framework\DataObject( |
| 38 | + [ |
| 39 | + 'product' => 1, |
| 40 | + 'selected_configurable_option' => 1, |
| 41 | + 'qty' => 1, |
| 42 | + 'super_attribute' => [ |
| 43 | + $attribute->getId() => $option->getId() |
| 44 | + ] |
| 45 | + ] |
| 46 | +); |
| 47 | + |
| 48 | + |
| 49 | +$quote = $quoteFactory->create(); |
| 50 | +$quoteResource->load($quote, 'test_quote', 'reserved_order_id'); |
| 51 | +$quote->addProduct($product, $requestInfo); |
| 52 | +$cartRepository->save($quote); |
0 commit comments