首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento -以编程方式保存的产品在后端不可见

Magento -以编程方式保存的产品在后端不可见
EN

Stack Overflow用户
提问于 2012-04-26 22:13:58
回答 2查看 4.6K关注 0票数 3

我试着在Magento 1.5.1上用下面的代码创建一个产品。该产品已保存在数据库中,但未显示在后端产品网格中。如果我调用产品视图url,一些字段没有被填充,比如: Name,Description (参见:NOK)。

代码语言:javascript
复制
$product = Mage::getModel('catalog/product');

$product->setSku("ABC123") // OK
    ->setName("Type 7 Widget") // NOK
    ->setDescription("This widget will give you years of trouble-free widgeting.") // NOK
    ->setShortDescription("High-end widget.") // NOK
    ->setPrice(70.50) // OK
    ->setTypeId('simple') // OK
    ->setAttributeSetId('14') // need to look this up // OK
    ->setCategoryIds("3,7") // need to look these up // OK
    ->setWeight(1.0) // OK
    ->setTaxClassId(1) //taxable goods // ?
    ->setVisibility(4) // catalog, search // NOK
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); // enabled // NOK

$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

$product->save();

这是保存在数据库print_r($product->debug());中的产品

代码语言:javascript
复制
[type_id] => simple
[sku] => ABC123
[has_options] => 0
[required_options] => 0
[created_at] => 2012-04-26 12:57:22
[updated_at] => 2012-04-26 12:57:22
[weight] => 1
[url_path] => .html
[price] => 70.5000
[enable_googlecheckout] => 1
[name] => Type 7 Widget
[meta_title] => Type 7 Widget
[meta_description] => Type 7 Widget, Fotos
[url_key] => type-7-widget
[options_container] => container2
[delivery_time] => 2-3 Tage
[generate_meta] => 0
[status] => 1
[tax_class_id] => 1
[visibility] => 4
[description] => This widget will give you years of trouble-free widgeting.
[short_description] => High-end widget.
[meta_keyword] => Type 7 Widget, Fotos
[media_gallery] => Array
    (
        [images] => Array
            (
            )

        [values] => Array
            (
            )

    )

[tier_price] => Array
    (
    )

[tier_price_changed] => 0
[stock_item (Mage_CatalogInventory_Model_Stock_Item)] => Array
    (
        [product_id] => 158
        [product_name] => Type 7 Widget
        [store_id] => 1
        [product_type_id] => simple
        [product_status_changed] => 1
    )

[is_in_stock] => 1
[is_salable] => 1
)

任何帮助都是最好的!

解决方案

使用以下代码指定要保存产品的商店:

代码语言:javascript
复制
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-26 23:14:39

在设置多个商店的情况下,不要忘记设置网站。

票数 3
EN

Stack Overflow用户

发布于 2012-04-26 22:17:53

您不会保存产品。

添加:

代码语言:javascript
复制
try{
    $product->save();
} catch (Exception $e) {
    echo $e->getMessage();
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10335279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档