【问题标题】:Magento2: How to add configurable products with multiple attribute programatically?Magento2:如何以编程方式添加具有多个属性的可配置产品?
【发布时间】:2020-11-05 12:22:19
【问题描述】:

在 Magento 2.4.1 中,我创建了一个 php 脚本(不是模块)来添加可配置产品。

我想添加属性为colorsize 的可配置产品。我已经有示例产品设置,并且我有他们的产品 ID 的信息,其中一个进入哪个配置产品。我以为我只需要将简单的产品 ID 分配为关联产品,但没有成功。

我读过几篇文章,他们说我需要先添加选项数据。所以,我做了下面的代码,但这也不起作用:

$productId = $productObjectManager->getIdBySku($sku);
$product = null;
if ($product) {
    $product = $productObjectManager->load($productId);
} else {
    $product = $productObjectManager;
    $product->setTypeId(Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE);
    $product->setAttributeSetId($productObjectManager->getDefaultAttributeSetId());
    $product->setSku($sku);
}

$product->setStoreId(0);
$product->setWebsiteIds($websiteIds);
$product->setVisibility(Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$product->setStatus(Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$product->setName($name);
$product->setPrice($price);
$product->setWeight($weight);
$product->setTaxClassId(0); // None
$product->setStockData([
    'use_config_manage_stock' => 1, 
    'is_in_stock' => 1
]);

$configurableAttributesData = [];
$position = 0;
foreach ($configurableAttributes as $configurableAttributeId => $configurableAttribute) {
    $configurableAttributesData[] = [
        'attribute_id' => $configurableAttribute->getId(),
        'code' => $configurableAttribute->getAttributeCode(),
        'label' => $configurableAttribute->getStoreLabel(),
        'position' => $position,
        'values' => $configurableAttributeValues[$configurableAttribute->getAttributeCode()],
    ];
    $position++;
}

//echo '<pre>';print_r($configurableAttributesData);die;

$optionsFactory = $objectManager->create(Magento\ConfigurableProduct\Helper\Product\Options\Factory::class);
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$extensionConfigurableAttributes = $product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($configurableProducts);
$product->setExtensionAttributes($extensionConfigurableAttributes);
$product->save();
echo 'Added';

我正在传递$configurableAttributesData 数据如下:

Array
(
    [0] => Array
        (
            [attribute_id] => 93
            [code] => color
            [label] => Colour
            [position] => 0
            [values] => Array
                (
                    [0] => Array
                        (
                            [label] => Colour
                            [attribute_id] => 93
                            [value_index] => Black
                        )

                )

        )

    [1] => Array
        (
            [attribute_id] => 140
            [code] => size
            [label] => Size
            [position] => 1
            [values] => Array
                (
                    [0] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 3
                        )

                    [1] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 4
                        )

                    [2] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 5
                        )

                    [3] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 6
                        )

                    [4] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 7
                        )

                    [5] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 8
                        )

                    [6] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 9
                        )

                    [7] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 10
                        )

                    [8] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 11
                        )

                    [9] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 12
                        )

                    [10] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 13
                        )

                    [11] => Array
                        (
                            [label] => Size
                            [attribute_id] => 140
                            [value_index] => 14
                        )

                )

        )

)

谁能帮忙!

【问题讨论】:

    标签: php magento2 configurable-product


    【解决方案1】:

    您可以通过创建自定义扩展或使用 Magento 2 API 来实现。

    【讨论】:

      【解决方案2】:

      阅读这篇文章。这是对您需要做什么的非常详细的描述。

      https://meetanshi.com/blog/programmatically-create-configurable-product-in-magento-2/

      祝你好运!

      【讨论】:

      • 它只是添加了一个新的配置类型产品,我的问题不同,请阅读它,如果您需要更多详细信息来了解,请告诉我:)
      猜你喜欢
      • 2018-09-11
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多