31 private $queryTemplates;
38 private $resourceConnections;
43 private $storeManager;
48 private $productRepository;
53 private $optionRepository;
58 private $productCollectionFactory;
63 private $linkManagement;
73 private $productStubData;
83 private $fixtureModel;
106 $this->productCollectionFactory = $productCollectionFactory;
108 $this->optionRepository = $optionRepository;
112 $this->fixtureModel = $fixtureModel;
121 public function generateQuotes()
123 $maxItemsPerOrder = $this->config->getSimpleCountTo()
124 + ($this->config->getConfigurableCountTo() + $this->config->getBigConfigurableCountTo()) * 2;
126 $maxItemId = $this->getMaxEntityId(
132 $itemIdSequence = $this->getItemIdSequence(
134 $this->config->getRequiredQuoteQuantity(),
137 $this->productStubData = $this->prepareProductsForQuote();
138 $this->prepareQueryTemplates();
141 $quoteQty = $this->config->getExistsQuoteQuantity();
143 while ($quoteQty < $this->config->getRequiredQuoteQuantity()) {
149 $this->saveQuoteWithQuoteItems($entityId, $itemIdSequence);
151 foreach ($this->resourceConnections as
$connection) {
156 throw $lastException;
159 if ($batchNumber >= self::BATCH_SIZE) {
160 $this->commitBatch();
165 foreach ($this->resourceConnections as
$connection) {
179 private function saveQuoteWithQuoteItems($entityId, \Generator $itemIdSequence)
183 $this->config->getSimpleCountFrom(),
184 $this->config->getSimpleCountTo()
187 $this->config->getConfigurableCountFrom(),
188 $this->config->getConfigurableCountTo()
191 $this->config->getBigConfigurableCountFrom(),
192 $this->config->getBigConfigurableCountTo()
196 '%itemsPerOrder%' => array_sum($productCount),
197 '%orderNumber%' => 100000000 * $this->getStubProductStoreId($entityId) + $entityId,
198 '%email%' =>
"quote_{$entityId}@example.com",
199 '%time%' => date(\
Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
200 '%productStoreId%' => $this->getStubProductStoreId($entityId),
201 '%productStoreName%' => $this->getStubProductStoreName($entityId),
202 '%entityId%' => $entityId,
204 $shippingAddress = [
'%orderAddressId%' => $entityId * 2 - 1,
'%addressType%' =>
'shipping'];
205 $billingAddress = [
'%orderAddressId%' => $entityId * 2,
'%addressType%' =>
'billing'];
206 $address = $this->getAddressDataFixture();
213 $this->saveItemSimpleData($entityId,
$i, $itemIdSequence->current(),
$quote);
214 $itemIdSequence->next();
220 $parentItemId = $itemIdSequence->current();
221 $this->saveParentItemConfigurableData($entityId,
$i, $parentItemId,
$type,
$quote);
222 $itemIdSequence->next();
225 $itemId = $itemIdSequence->current();
226 $this->saveChildItemConfigurable($entityId,
$i, $itemId, $parentItemId,
$type,
$quote);
227 $itemIdSequence->next();
241 private function saveItemSimpleData($entityId,
$index, $itemId, array
$quote)
247 '%itemId%' => $itemId,
250 '%parentItemId%' =>
'null',
252 $this->
query(
'quote_item', $quote, $itemData);
253 $this->
query(
'quote_item_option', $quote, $itemData, [
254 '%code%' =>
'info_buyRequest',
255 '%value%' => $this->serializer->serialize([
258 'uenc' =>
'aHR0cDovL21hZ2UyLmNvbS9jYXRlZ29yeS0xLmh0bWw' 273 private function saveParentItemConfigurableData($entityId,
$index, $parentItemId, $productType, array
$quote)
276 '%productId%' => $this->getStubProductId($entityId,
$index, $productType),
277 '%sku%' => $this->getStubProductSku($entityId,
$index, $productType),
278 '%name%' => $this->getStubProductName($entityId,
$index, $productType),
279 '%productOptions%' => $this->getStubProductBuyRequest($entityId,
$index, $productType)[
'order'],
280 '%itemId%' => $parentItemId,
281 '%parentItemId%' =>
'null',
284 $this->
query(
'quote_item', $quote, $itemData);
285 $this->
query(
'quote_item_option', $quote, $itemData, [
286 '%code%' =>
'info_buyRequest',
287 '%value%' => $this->getStubProductBuyRequest($entityId,
$index, $productType)[
'quote']
289 $this->
query(
'quote_item_option', $quote, $itemData, [
290 '%code%' =>
'attributes',
291 '%value%' => $this->getStubProductBuyRequest($entityId,
$index, $productType)[
'super_attribute']
293 $itemData[
'%productId%'] = $this->getStubProductChildId($entityId,
$index, $productType);
294 $this->
query(
'quote_item_option', $itemData, [
295 '%code%' =>
"product_qty_" . $this->getStubProductChildId($entityId,
$index, $productType),
298 $this->
query(
'quote_item_option', $itemData, [
299 '%code%' =>
"simple_product",
300 '%value%' => $this->getStubProductChildId($entityId,
$index, $productType)
315 private function saveChildItemConfigurable($entityId,
$index, $itemId, $parentItemId, $productType, array
$quote)
318 '%productId%' => $this->getStubProductChildId($entityId,
$index, $productType),
319 '%sku%' => $this->getStubProductSku($entityId,
$index, $productType),
320 '%name%' => $this->getStubProductName($entityId,
$index, $productType),
321 '%productOptions%' => $this->getStubProductChildBuyRequest($entityId,
$index, $productType)[
'order'],
322 '%itemId%' => $itemId,
323 '%parentItemId%' => $parentItemId,
327 $this->
query(
'quote_item', $quote, $itemData);
328 $this->
query(
'quote_item_option', $itemData, [
329 '%code%' =>
"info_buyRequest",
330 '%value%' => $this->getStubProductChildBuyRequest($entityId,
$index, $productType)[
'quote']
332 $this->
query(
'quote_item_option', $itemData, [
333 '%code%' =>
"parent_product_id",
334 '%value%' => $this->getStubProductId($entityId,
$index, $productType)
344 private function getStubProductStoreId($entityId)
346 return $this->productStubData[$this->getProductStubIndex($entityId)][0];
355 private function getStubProductStoreName($entityId)
357 return $this->productStubData[$this->getProductStubIndex($entityId)][1];
368 private function getStubProductId($entityId,
$index,
$type)
370 return $this->productStubData[$this->getProductStubIndex($entityId)][2][
$type][
$index][
'id'];
381 private function getStubProductSku($entityId,
$index,
$type)
383 return $this->productStubData[$this->getProductStubIndex($entityId)][2][
$type][
$index][
'sku'];
394 private function getStubProductName($entityId,
$index,
$type)
396 return $this->productStubData[$this->getProductStubIndex($entityId)][2][
$type][
$index][
'name'];
407 private function getStubProductBuyRequest($entityId,
$index,
$type)
409 return $this->productStubData[$this->getProductStubIndex($entityId)][2][
$type][
$index][
'buyRequest'];
420 private function getStubProductChildBuyRequest($entityId,
$index,
$type)
422 return $this->productStubData[$this->getProductStubIndex($entityId)][2][
$type][
$index][
'childBuyRequest'];
433 private function getStubProductChildId($entityId,
$index,
$type)
435 return $this->productStubData[$this->getProductStubIndex($entityId)][2][
$type][
$index][
'childId'];
444 private function getProductStubIndex($entityId)
446 $storeCount = count($this->productStubData);
447 $qty = intdiv($this->config->getRequiredQuoteQuantity(), $storeCount);
448 return intdiv($entityId, $qty) % $storeCount;
456 private function getAddressDataFixture()
459 '%firstName%' =>
'First Name',
460 '%lastName%' =>
'Last Name',
461 '%company%' =>
'Company',
462 '%address%' =>
'Address',
464 '%state%' =>
'Alabama',
476 private function prepareProductsForQuote()
480 foreach ($this->storeManager->getStores() as
$store) {
481 $productsResult = [];
482 $this->storeManager->setCurrentStore(
$store->getId());
484 if ($this->config->getSimpleCountTo() > 0) {
494 foreach ($configurables as
$type => $qty) {
496 $productsResult[
$type] = $this->prepareConfigurableProducts(
497 $this->getProductIds(
509 $this->storeManager->getWebsite(
$store->getWebsiteId())->getName(),
510 $this->storeManager->getGroup(
$store->getStoreGroupId())->getName(),
530 private function prepareQueryTemplates()
532 $fileName = $this->config->getFixtureDataFilename();
549 .
"\\Model\\ResourceModel\\" 557 $querySuffix =
$template[
'_query_suffix'];
569 $this->queryTemplates[
$table] =
"INSERT INTO `{$tableName}` ({$fields}) VALUES ({$values}){$querySuffix};";
587 foreach ($replacements as
$data) {
605 private function getMaxEntityId(
$tableName, $resourceName, $column =
'entity_id')
609 return (
int)
$connection->query(
"SELECT MAX(`{$column}`) FROM `{$tableName}`;")->fetchColumn(0);
647 private function prepareSimpleProducts(array
$productIds = [])
649 $productsResult = [];
655 $productsResult[$key][
'buyRequest'] = $this->serializer->serialize([
656 "info_buyRequest" => [
657 "uenc" =>
"aHR0cDovL21hZ2VudG8uZGV2L2NvbmZpZ3VyYWJsZS1wcm9kdWN0LTEuaHRtbA,,",
663 return $productsResult;
674 private function prepareConfigurableProducts(array
$productIds = [])
676 $productsResult = [];
681 $simpleSku = $configurableChild->getSku();
682 $simpleId = $this->productRepository->get($simpleSku)->getId();
684 $attributesInfo = [];
685 $superAttribute = [];
687 $attributesInfo[] = [
688 "label" =>
$option->getLabel(),
689 "value" =>
$option[
'options'][
'0'][
'label'],
690 "option_id" =>
$option->getAttributeId(),
691 "option_value" =>
$option->getValues()[0]->getValueIndex()
693 $superAttribute[
$option->getAttributeId()] =
$option->getValues()[0]->getValueIndex();
696 $configurableBuyRequest = [
697 "info_buyRequest" => [
698 "uenc" =>
"aHR0cDovL21hZ2UyLmNvbS9jYXRlZ29yeS0xLmh0bWw",
699 "product" => $configurableId,
700 "selected_configurable_option" =>
$simpleId,
701 "related_product" =>
"",
702 "super_attribute" => $superAttribute,
705 "attributes_info" => $attributesInfo,
706 "simple_name" => $configurableChild->getName(),
707 "simple_sku" => $configurableChild->getSku(),
709 $simpleBuyRequest = [
710 "info_buyRequest" => [
711 "uenc" =>
"aHR0cDovL21hZ2VudG8uZGV2L2NvbmZpZ3VyYWJsZS1wcm9kdWN0LTEuaHRtbA,,",
712 "product" => $configurableId,
713 "selected_configurable_option" =>
$simpleId,
714 "related_product" =>
"",
715 "super_attribute" => $superAttribute,
720 $quoteConfigurableBuyRequest = $configurableBuyRequest[
'info_buyRequest'];
721 $quoteSimpleBuyRequest = $simpleBuyRequest[
'info_buyRequest'];
722 unset($quoteConfigurableBuyRequest[
'selected_configurable_option']);
723 unset($quoteSimpleBuyRequest[
'selected_configurable_option']);
725 $productsResult[$key][
'id'] = $configurableId;
726 $productsResult[$key][
'sku'] = $simpleSku;
728 $productsResult[$key][
'childId'] =
$simpleId;
729 $productsResult[$key][
'buyRequest'] = [
730 'order' => $this->serializer->serialize($configurableBuyRequest),
731 'quote' => $this->serializer->serialize($quoteConfigurableBuyRequest),
732 'super_attribute' => $this->serializer->serialize($superAttribute)
734 $productsResult[$key][
'childBuyRequest'] = [
735 'order' => $this->serializer->serialize($simpleBuyRequest),
736 'quote' => $this->serializer->serialize($quoteSimpleBuyRequest),
739 return $productsResult;
749 private function commitBatch()
751 foreach ($this->resourceConnections as
$connection) {
767 private function getItemIdSequence($maxItemId, $requestedOrders, $maxItemsPerOrder)
769 $requestedItems = $maxItemId + ($requestedOrders + 1) * $maxItemsPerOrder;
770 for (
$i = $maxItemId + 1;
$i <= $requestedItems;
$i++) {
783 private function getTableName(
$tableName, $resourceName)
786 $resource = $this->fixtureModel->getObjectManager()->get($resourceName);
796 private function getConnection($resourceName)
798 $resource = $this->fixtureModel->getObjectManager()->get($resourceName);
query($table,... $replacements)
const BIG_CONFIGURABLE_TYPE
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository, \Magento\ConfigurableProduct\Api\LinkManagementInterface $linkManagement, \Magento\Framework\Serialize\SerializerInterface $serializer, QuoteConfiguration $config, \Magento\Setup\Fixtures\FixtureModel $fixtureModel)