Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableAttributeData.php
Go to the documentation of this file.
1 <?php
8 
11 
18 {
26  public function getAttributesData(Product $product, array $options = [])
27  {
28  $defaultValues = [];
29  $attributes = [];
30  foreach ($product->getTypeInstance()->getConfigurableAttributes($product) as $attribute) {
31  $attributeOptionsData = $this->getAttributeOptionsData($attribute, $options);
32  if ($attributeOptionsData) {
33  $productAttribute = $attribute->getProductAttribute();
34  $attributeId = $productAttribute->getId();
35  $attributes[$attributeId] = [
36  'id' => $attributeId,
37  'code' => $productAttribute->getAttributeCode(),
38  'label' => $productAttribute->getStoreLabel($product->getStoreId()),
39  'options' => $attributeOptionsData,
40  'position' => $attribute->getPosition(),
41  ];
42  $defaultValues[$attributeId] = $this->getAttributeConfigValue($attributeId, $product);
43  }
44  }
45  return [
46  'attributes' => $attributes,
47  'defaultValues' => $defaultValues,
48  ];
49  }
50 
57  {
58  $attributeOptionsData = [];
59  foreach ($attribute->getOptions() as $attributeOption) {
60  $optionId = $attributeOption['value_index'];
61  $attributeOptionsData[] = [
62  'id' => $optionId,
63  'label' => $attributeOption['label'],
64  'products' => isset($config[$attribute->getAttributeId()][$optionId])
65  ? $config[$attribute->getAttributeId()][$optionId]
66  : [],
67  ];
68  }
69  return $attributeOptionsData;
70  }
71 
77  protected function getAttributeConfigValue($attributeId, $product)
78  {
79  return $product->hasPreconfiguredValues()
80  ? $product->getPreconfiguredValues()->getData('super_attribute/' . $attributeId)
81  : null;
82  }
83 }
$config
Definition: fraud_order.php:17
$attributes
Definition: matrix.phtml:13