gpt4 book ai didi

php - 使用表前缀在 magento 中以编程方式创建产品

转载 作者:行者123 更新时间:2023-11-29 00:18:46 24 4
gpt4 key购买 nike

我正在尝试以编程方式在 magento 中创建产品。此脚本适用于默认安装。但是,当我尝试在我提供表前缀的那些 magento 安装中创建产品时,它会显示以下错误。您能告诉我需要进行哪些更改吗?

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (databasename_magento.catalog_category_product_index, CONSTRAINT FK_CAT_CTGR_PRD_IDX_CTGR_ID_CAT_CTGR_ENTT_ENTT_ID FOREIGN KEY (category_id) REFERENCES catalog_category_entity (`entity_)

$product = new Mage_Catalog_Model_Product();
$product->setSku($sku);
$product->setAttributeSetId(9);
$product->setTypeId('simple');
$product->setName('55');
$product->setCategoryIds(array(7)); # some cat id's, my is 7
$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription('Gift card Full description here');
$product->setShortDescription('Gift Card Short description here');
$product->setPrice('200'); # Set some price
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
$product->setStockData(array( 'is_in_stock' => 1, 'qty' => 9999 ));

$product->save();

最佳答案

错误消息清楚地通知 category_id 字段的外键完整性遭到破坏。

$product->setCategoryIds(array(7)); # some cat id's, my is 7

上述语句一定会导致错误,因为您设置的值不存在于引用的父列 catalog_category_entity (entity_id) 中。

将输入更改为父表中的现有值并运行。它应该可以正常工作。

关于php - 使用表前缀在 magento 中以编程方式创建产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com