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