dpzr52746 2013-10-08 15:43
浏览 6
已采纳

如何用php创建分组产品? (Magento 1.7)

I'm trying to create grouped product and attach simple one to it. In admin section both of them are available, but in frontend available only simple product.

If I enter into product edit mode in admin panel (indifferent simple or grouped) and just save it without changing anything, grouped product will appear in frontend.

I can't figure out where is a problem.

// create grouped product
$product = Mage::getModel('catalog/product');
$product->setTypeId( 'grouped' );
$product->setAttributeSetId( 13 );
$product->setSku( 'K101596245' );
...
$product->setVisibility( 4 );
$product->setStatus( 1 );
$product->setTaxClassId( 0 );
$product->setStockData(array(
   'is_in_stock' => 0,
));
$product->save();
unset($product);


// create simple product
$simpleProduct = Mage::getModel('catalog/product');
$simpleProduct->setData( 'type_id', 'simple' );
...
$simpleProduct->setData( 'status', 1 );
$simpleProduct->setData( 'price', $values['retailPrice'] );
$simpleProduct->setData( 'special_price', $values['price'] );
$simpleProduct->setData( 'stock_data', array(
        'is_in_stock' => 1,
        'qty' => 9,
     ));
$simpleProduct->save();


// attach simple to grouped
Mage::getModel('catalog/product_link_api')->assign("grouped", $product->getId(), $simpleProduct->getId());

The same result with:

// attach simple to grouped
$relation_data = array();
$relation_data[ $simpleProduct->getId() ] = array('qty' => 9, 'position' => 0);
$product->setGroupedLinkData($relation_data);
$product->save();
  • 写回答

1条回答 默认 最新

  • dtu36380 2013-10-08 23:41
    关注

    Solution was found!

    First script add grouped product, then simple. Stock of grouped product should be updated!

    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
    $stockItem->setData('is_in_stock', 1);
    $stockItem->save();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀