ConfigurableProductManagement.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\ConfigurableProduct\Model;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Model\Product\Attribute\Source\Status;
  10. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  11. use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
  12. class ConfigurableProductManagement implements \Magento\ConfigurableProduct\Api\ConfigurableProductManagementInterface
  13. {
  14. /**
  15. * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
  16. */
  17. private $attributeRepository;
  18. /**
  19. * @var ProductVariationsBuilder
  20. */
  21. private $productVariationBuilder;
  22. /**
  23. * @var CollectionFactory
  24. */
  25. protected $productsFactory;
  26. /**
  27. * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
  28. * @param ProductVariationsBuilder $productVariationBuilder
  29. * @param CollectionFactory $productsFactory
  30. */
  31. public function __construct(
  32. \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
  33. ProductVariationsBuilder $productVariationBuilder,
  34. CollectionFactory $productsFactory
  35. ) {
  36. $this->attributeRepository = $attributeRepository;
  37. $this->productVariationBuilder = $productVariationBuilder;
  38. $this->productsFactory = $productsFactory;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function generateVariation(ProductInterface $product, $options)
  44. {
  45. $attributes = $this->getAttributesForMatrix($options);
  46. $products = $this->productVariationBuilder->create($product, $attributes);
  47. return $products;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getCount($status = null)
  53. {
  54. $products = $this->productsFactory->create();
  55. // @codingStandardsIgnoreStart
  56. /** @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection $products */
  57. // @codingStandardsIgnoreEnd
  58. switch ($status) {
  59. case Status::STATUS_ENABLED:
  60. $products->addAttributeToFilter('status', Status::STATUS_ENABLED);
  61. break;
  62. case Status::STATUS_DISABLED:
  63. $products->addAttributeToFilter('status', Status::STATUS_DISABLED);
  64. break;
  65. }
  66. return $products->getSize();
  67. }
  68. /**
  69. * Prepare attribute info for variation matrix generation
  70. *
  71. * @param \Magento\ConfigurableProduct\Api\Data\OptionInterface[] $options
  72. * @return array
  73. */
  74. private function getAttributesForMatrix($options)
  75. {
  76. $attributes = [];
  77. /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $option */
  78. foreach ($options as $option) {
  79. $configurable = $this->objectToArray($option);
  80. /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
  81. $attribute = $this->attributeRepository->get($option->getAttributeId());
  82. $attributeOptions = $attribute->getOptions() !== null ? $attribute->getOptions() : [];
  83. foreach ($attributeOptions as $attributeOption) {
  84. $configurable['options'][] = $this->objectToArray($attributeOption);
  85. }
  86. $configurable['attribute_code'] = $attribute->getAttributeCode();
  87. $attributes[$option->getAttributeId()] = $configurable;
  88. }
  89. return $attributes;
  90. }
  91. /**
  92. * Return Data Object data in array format.
  93. *
  94. * @param \Magento\Framework\DataObject $object
  95. * @return array
  96. */
  97. private function objectToArray(\Magento\Framework\DataObject $object)
  98. {
  99. $data = $object->getData();
  100. foreach ($data as $key => $value) {
  101. if (is_object($value)) {
  102. $data[$key] = $this->objectToArray($value);
  103. } elseif (is_array($value)) {
  104. foreach ($value as $nestedKey => $nestedValue) {
  105. if (is_object($nestedValue)) {
  106. $value[$nestedKey] = $this->objectToArray($nestedValue);
  107. }
  108. }
  109. $data[$key] = $value;
  110. }
  111. }
  112. return $data;
  113. }
  114. }