Just re-write the file vendor/magento/module-configurable-product/Model/ConfigurableAttributeData.php
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 | <?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\ConfigurableProduct\Model; use Magento\Catalog\Model\Product; use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute; /** * Class ConfigurableAttributeData * @api * @since 100.0.2 */ class ConfigurableAttributeData { /** * Get product attributes * * @param Product $product * @param array $options * @return array */ public function getAttributesData(Product $product , array $options = []) { $defaultValues = []; $attributes = []; foreach ( $product ->getTypeInstance()->getConfigurableAttributes( $product ) as $attribute ) { $attributeOptionsData = $this ->getAttributeOptionsData( $attribute , $options ); if ( $attributeOptionsData ) { $productAttribute = $attribute ->getProductAttribute(); $attributeId = $productAttribute ->getId(); $attributes [ $attributeId ] = [ 'id' => $attributeId , 'code' => $productAttribute ->getAttributeCode(), 'label' => $productAttribute ->getStoreLabel( $product ->getStoreId()), 'options' => $attributeOptionsData , 'position' => $attribute ->getPosition(), ]; $defaultValues [ $attributeId ] = $this ->getAttributeConfigValue( $attributeId , $product ); } } return [ 'attributes' => $attributes , 'defaultValues' => $defaultValues , ]; } public function labelsort( $a , $b ){ return strcmp ( $a [ 'label' ], $b [ 'label' ]);} /** * @param Attribute $attribute * @param array $config * @return array */ protected function getAttributeOptionsData( $attribute , $config ) { $attributeOptionsData = []; foreach ( $attribute ->getOptions() as $attributeOption ) { $optionId = $attributeOption [ 'value_index' ]; $attributeOptionsData [] = [ 'id' => $optionId , 'label' => $attributeOption [ 'label' ], 'products' => isset( $config [ $attribute ->getAttributeId()][ $optionId ]) ? $config [ $attribute ->getAttributeId()][ $optionId ] : [], ]; } if ( count ( $attributeOptionsData ) > 1){usort( $attributeOptionsData , array ( $this , 'labelsort' ));} return $attributeOptionsData ; } /** * @param int $attributeId * @param Product $product * @return mixed|null */ protected function getAttributeConfigValue( $attributeId , $product ) { return $product ->hasPreconfiguredValues() ? $product ->getPreconfiguredValues()->getData( 'super_attribute/' . $attributeId ) : null; } } |
Leave a Reply