|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend'); |
| 7 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 8 | +/** Create simple and bundle products for quote*/ |
| 9 | +$simpleProducts[] = $objectManager->create('Magento\Catalog\Model\Product') |
| 10 | +->setTypeId( |
| 11 | + \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE |
| 12 | +)->setId( |
| 13 | + 1 |
| 14 | +)->setAttributeSetId( |
| 15 | + 4 |
| 16 | +)->setWebsiteIds( |
| 17 | + [1] |
| 18 | +)->setName( |
| 19 | + 'Simple Product 1' |
| 20 | +)->setSku( |
| 21 | + 'simple-1' |
| 22 | +)->setPrice( |
| 23 | + 10 |
| 24 | +)->setDescription( |
| 25 | + 'Description with <b>html tag</b>' |
| 26 | +)->setVisibility( |
| 27 | + \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH |
| 28 | +)->setStatus( |
| 29 | + \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED |
| 30 | +)->setCategoryIds( |
| 31 | + [2] |
| 32 | +)->setStockData( |
| 33 | + ['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1] |
| 34 | +)->save(); |
| 35 | + |
| 36 | +$simpleProducts[] = $objectManager->create('Magento\Catalog\Model\Product') |
| 37 | +->setTypeId( |
| 38 | + \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE |
| 39 | +)->setId( |
| 40 | + 2 |
| 41 | +)->setAttributeSetId( |
| 42 | + 4 |
| 43 | +)->setWebsiteIds( |
| 44 | + [1] |
| 45 | +)->setName( |
| 46 | + 'Simple Product 2' |
| 47 | +)->setSku( |
| 48 | + 'simple-2' |
| 49 | +)->setPrice( |
| 50 | + 10 |
| 51 | +)->setDescription( |
| 52 | + 'Description with <b>html tag</b>' |
| 53 | +)->setVisibility( |
| 54 | + \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH |
| 55 | +)->setStatus( |
| 56 | + \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED |
| 57 | +)->setCategoryIds( |
| 58 | + [2] |
| 59 | +)->setStockData( |
| 60 | + ['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1] |
| 61 | +)->save(); |
| 62 | + |
| 63 | +$product = $objectManager->create('Magento\Catalog\Model\Product'); |
| 64 | +$product->setTypeId( |
| 65 | + \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE |
| 66 | +)->setId( |
| 67 | + 3 |
| 68 | +)->setAttributeSetId( |
| 69 | + 4 |
| 70 | +)->setWebsiteIds( |
| 71 | + [1] |
| 72 | +)->setName( |
| 73 | + 'Bundle Product' |
| 74 | +)->setSku( |
| 75 | + 'bundle-product' |
| 76 | +)->setDescription( |
| 77 | + 'Description with <b>html tag</b>' |
| 78 | +)->setShortDescription( |
| 79 | + 'Bundle' |
| 80 | +)->setVisibility( |
| 81 | + \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH |
| 82 | +)->setStatus( |
| 83 | + \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED |
| 84 | +)->setStockData( |
| 85 | + [ |
| 86 | + 'use_config_manage_stock' => 0, |
| 87 | + 'manage_stock' => 0, |
| 88 | + 'use_config_enable_qty_increments' => 1, |
| 89 | + 'use_config_qty_increments' => 1, |
| 90 | + 'is_in_stock' => 0, |
| 91 | + ] |
| 92 | +)->setBundleOptionsData( |
| 93 | + [ |
| 94 | + [ |
| 95 | + 'title' => 'Bundle Product Items', |
| 96 | + 'default_title' => 'Bundle Product Items', |
| 97 | + 'type' => 'select', |
| 98 | + 'required' => 1, |
| 99 | + 'delete' => '', |
| 100 | + 'position' => 0, |
| 101 | + 'option_id' => '', |
| 102 | + ], |
| 103 | + ] |
| 104 | +)->setBundleSelectionsData( |
| 105 | + [ |
| 106 | + [ |
| 107 | + [ |
| 108 | + 'product_id' => $simpleProducts[0]->getId(), |
| 109 | + 'selection_qty' => 1, |
| 110 | + 'selection_can_change_qty' => 1, |
| 111 | + 'delete' => '', |
| 112 | + 'position' => 0, |
| 113 | + 'selection_price_type' => 0, |
| 114 | + 'selection_price_value' => 0.0, |
| 115 | + 'option_id' => '', |
| 116 | + 'selection_id' => '', |
| 117 | + 'is_default' => 1, |
| 118 | + ], |
| 119 | + [ |
| 120 | + 'product_id' => $simpleProducts[1]->getId(), |
| 121 | + 'selection_qty' => 1, |
| 122 | + 'selection_can_change_qty' => 1, |
| 123 | + 'delete' => '', |
| 124 | + 'position' => 0, |
| 125 | + 'selection_price_type' => 0, |
| 126 | + 'selection_price_value' => 0.0, |
| 127 | + 'option_id' => '', |
| 128 | + 'selection_id' => '', |
| 129 | + 'is_default' => 1, |
| 130 | + ] |
| 131 | + ], |
| 132 | + ] |
| 133 | +)->setCanSaveBundleSelections( |
| 134 | + true |
| 135 | +)->setAffectBundleProductSelections( |
| 136 | + true |
| 137 | +)->save(); |
| 138 | + |
| 139 | +//Load options |
| 140 | +$typeInstance = $product->getTypeInstance(); |
| 141 | +$typeInstance->setStoreFilter($product->getStoreId(), $product); |
| 142 | +$optionCollection = $typeInstance->getOptionsCollection($product); |
| 143 | +$selectionCollection = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product); |
| 144 | + |
| 145 | +$bundleOptions = []; |
| 146 | +$bundleOptionsQty = []; |
| 147 | +/** @var $option \Magento\Bundle\Model\Option */ |
| 148 | +foreach ($optionCollection as $option) { |
| 149 | + /** @var $selection \Magento\Bundle\Model\Selection */ |
| 150 | + foreach ($selectionCollection as $selection) { |
| 151 | + $bundleOptions[$option->getId()][] = $selection->getSelectionId(); |
| 152 | + $bundleOptionsQty[$option->getId()][] = 1; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +$buyRequest = new \Magento\Framework\Object( |
| 157 | + ['qty' => 1, 'bundle_option' => $bundleOptions, 'bundle_option_qty' => $bundleOptionsQty] |
| 158 | +); |
| 159 | +$product->setSkipCheckRequiredOption(true); |
| 160 | + |
| 161 | + |
| 162 | +$addressData = include __DIR__ . '/address_data.php'; |
| 163 | +$billingAddress = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 164 | + 'Magento\Quote\Model\Quote\Address', |
| 165 | + ['data' => $addressData] |
| 166 | +); |
| 167 | +$billingAddress->setAddressType('billing'); |
| 168 | + |
| 169 | +/** @var $rate \Magento\Quote\Model\Quote\Address\Rate */ |
| 170 | +$rate = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Quote\Model\Quote\Address\Rate'); |
| 171 | +$rate->setCode('freeshipping_freeshipping'); |
| 172 | +$rate->getPrice(1); |
| 173 | + |
| 174 | +/** @var Magento\Quote\Model\Quote\Address $shippingAddress */ |
| 175 | +$shippingAddress = clone $billingAddress; |
| 176 | +$shippingAddress->setId(null)->setAddressType('shipping'); |
| 177 | +$shippingAddress->setShippingMethod('freeshipping_freeshipping'); |
| 178 | +$shippingAddress->addShippingRate($rate); |
| 179 | + |
| 180 | +/** @var \Magento\Quote\Model\Quote $quote */ |
| 181 | +$quote = $objectManager->create('Magento\Quote\Model\Quote'); |
| 182 | +$quote->setCustomerIsGuest( |
| 183 | + true |
| 184 | +)->setStoreId( |
| 185 | + \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
| 186 | + 'Magento\Store\Model\StoreManagerInterface' |
| 187 | + )->getStore()->getId() |
| 188 | +)->setReservedOrderId( |
| 189 | + 'test01' |
| 190 | +)->setBillingAddress( |
| 191 | + $billingAddress |
| 192 | +)->setShippingAddress( |
| 193 | + $shippingAddress |
| 194 | +)->setCustomerEmail( |
| 195 | + 'test@test.magento.com' |
| 196 | +)->addProduct( |
| 197 | + $product, $buyRequest |
| 198 | +); |
| 199 | + |
| 200 | +$quote->getPayment()->setMethod('checkmo'); |
| 201 | +$quote->collectTotals(); |
| 202 | +$quote->save(); |
| 203 | + |
| 204 | +/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */ |
| 205 | +$quoteIdMask = $objectManager->create('Magento\Quote\Model\QuoteIdMaskFactory')->create(); |
| 206 | +$quoteIdMask->setQuoteId($quote->getId()); |
| 207 | +$quoteIdMask->setDataChanges(true); |
| 208 | +$quoteIdMask->save(); |
0 commit comments