Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableProductManagement.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
13 
15 {
19  private $attributeRepository;
20 
24  private $productVariationBuilder;
25 
29  protected $productsFactory;
30 
36  public function __construct(
37  \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
38  ProductVariationsBuilder $productVariationBuilder,
39  CollectionFactory $productsFactory
40  ) {
41  $this->attributeRepository = $attributeRepository;
42  $this->productVariationBuilder = $productVariationBuilder;
43  $this->productsFactory = $productsFactory;
44  }
45 
50  {
51  $attributes = $this->getAttributesForMatrix($options);
52  $products = $this->productVariationBuilder->create($product, $attributes);
53  return $products;
54  }
55 
59  public function getCount($status = null)
60  {
61  $products = $this->productsFactory->create();
62  // @codingStandardsIgnoreStart
64  // @codingStandardsIgnoreEnd
65  switch ($status) {
67  $products->addAttributeToFilter('status', Status::STATUS_ENABLED);
68  break;
70  $products->addAttributeToFilter('status', Status::STATUS_DISABLED);
71  break;
72  }
73  return $products->getSize();
74  }
75 
82  private function getAttributesForMatrix($options)
83  {
84  $attributes = [];
86  foreach ($options as $option) {
87  $configurable = $this->objectToArray($option);
89  $attribute = $this->attributeRepository->get($option->getAttributeId());
90  $attributeOptions = $attribute->getOptions() !== null ? $attribute->getOptions() : [];
91 
92  foreach ($attributeOptions as $attributeOption) {
93  $configurable['options'][] = $this->objectToArray($attributeOption);
94  }
95  $configurable['attribute_code'] = $attribute->getAttributeCode();
96  $attributes[$option->getAttributeId()] = $configurable;
97  }
98  return $attributes;
99  }
100 
107  private function objectToArray(\Magento\Framework\DataObject $object)
108  {
109  $data = $object->getData();
110  foreach ($data as $key => $value) {
111  if (is_object($value)) {
112  $data[$key] = $this->objectToArray($value);
113  } elseif (is_array($value)) {
114  foreach ($value as $nestedKey => $nestedValue) {
115  if (is_object($nestedValue)) {
116  $value[$nestedKey] = $this->objectToArray($nestedValue);
117  }
118  }
119  $data[$key] = $value;
120  }
121  }
122  return $data;
123  }
124 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
__construct(\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository, ProductVariationsBuilder $productVariationBuilder, CollectionFactory $productsFactory)
$status
Definition: order_status.php:8
$attributes
Definition: matrix.phtml:13