如何解决Magento2:如何以编程方式添加具有多个属性的可配置产品?
在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
)
)
)
)
任何人都可以帮忙!
解决方法
您可以通过创建自定义扩展名或使用Magento 2 API来实现。
,阅读本文。这是您需要做的非常详细的描述。
https://meetanshi.com/blog/programmatically-create-configurable-product-in-magento-2/
祝你好运!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。