在Magento 2.4.1中,我创建了一个php脚本(不是模块)来添加可配置的产品。
我想添加属性为color
和size
的可配置产品。我已经有示例产品设置,并且已经获得了其产品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
)
)
)
)
任何人都可以帮忙!
答案 0 :(得分:0)
您可以通过创建自定义扩展名或使用Magento 2 API来实现。
答案 1 :(得分:-1)
阅读本文。这是您需要做的非常详细的描述。
https://meetanshi.com/blog/programmatically-create-configurable-product-in-magento-2/
祝你好运!