문제

To get children of Configurable Product, I'm using following API

/V1/configurable-products/{{SKU}}/children

I'm getting following output:

[
    {
        "id": 3234,
        "sku": "ABC1006",
        "name": "ABC Config Product",
        "attribute_set_id": 4,
        "price": 2400,
        "status": 1,
        "type_id": "simple",
        "created_at": "2018-12-17 13:27:58",
        "updated_at": "2018-12-18 11:06:28",
        "weight": 1,
        "product_links": [],
        "tier_prices": [],
        "custom_attributes": [
            {
                "attribute_code": "tax_class_id",
                "value": "2"
            },
            {
                "attribute_code": "required_options",
                "value": "0"
            },
            {
                "attribute_code": "category_ids",
                "value": [
                    "173"
                ]
            },
            {
                "attribute_code": "has_options",
                "value": "0"
            },
            {
                "attribute_code": "brand",
                "value": "399"
            },
            {
                "attribute_code": "short_description",
                "value": "<ul></ul>"
            },
            {
                "attribute_code": "image",
                "value": "/h/a/hamster-ball-32cm_7.png"
            },
            {
                "attribute_code": "small_image",
                "value": "/h/a/hamster-ball-32cm_7.png"
            },
            {
                "attribute_code": "thumbnail",
                "value": "/h/a/hamster-ball-32cm_7.png"
            },
            {
                "attribute_code": "swatch_image",
                "value": "/h/a/hamster-ball-32cm_7.png"
            },
            {
                "attribute_code": "url_key",
                "value": "savic-hamster-ball-extra-large"
            },
            {
                "attribute_code": "msrp_display_actual_price_type",
                "value": "0"
            },
            {
                "attribute_code": "size_swatch",
                "value": "213"
            }
        ]
    }
]

I want to add one more key pair (type=string) in the above json output. Where should I make changes to get the results.

도움이 되었습니까?

해결책

You may add an attribute with its value using extension attribute model as follows.

Add attribute code to extension attribute set by creating following file

Vendor\YourModule\etc\extension_attributes.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="custom_attr_code" type="string" />
    </extension_attributes>
</config>

Now set your custom value by overriding

Magento\ConfigurableProduct\Model\LinkManagement

using dependency injection

Vendor\YourModule\etc\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\ConfigurableProduct\Model\LinkManagement" type="Vendor\YourModule\Model\LinkManagement" />
</config>

Rewriting GetChildren method with your custom value

Vendor\YourModule\Model\LinkManagement.php

    public function getChildren($sku)
        {
            /** @var \Magento\Catalog\Model\Product $product */
            $product = $this->productRepository->get($sku);
            if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
                return [];
            }

            /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
            $productTypeInstance = $product->getTypeInstance();
            $productTypeInstance->setStoreFilter($product->getStoreId(), $product);

            $childrenList = [];
            /** @var \Magento\Catalog\Model\Product $child */
            foreach ($productTypeInstance->getUsedProducts($product) as $child) {
                $attributes = [];
                foreach ($child->getAttributes() as $attribute) {
                    $attrCode = $attribute->getAttributeCode();
                    $value = $child->getDataUsingMethod($attrCode) ?: $child->getData($attrCode);
                    if (null !== $value) {
                        $attributes[$attrCode] = $value;
                    }
                }
                $attributes['store_id'] = $child->getStoreId();
                /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
                $productDataObject = $this->productFactory->create();
                $this->dataObjectHelper->populateWithArray(
                    $productDataObject,
                    $attributes,
                    \Magento\Catalog\Api\Data\ProductInterface::class
                );


//Start of custom code
                $extensionAttributes = $productDataObject->getExtensionAttributes();
                $extensionAttributes->setCustomAttrCode('customValue');
                $productDataObject->setExtensionAttributes($extensionAttributes);
//End of custom code


                $childrenList[] = $productDataObject;
            }
            return $childrenList;
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top