Magento:以编程方式更改相关产品


Magento: programmatically changing the related products

我正试图将一个产品($odlProduct)的相关产品复制到另一个产品中($newProduct)。我这么做是因为我试图从一个可配置的产品中创建一个简单的产品。

我用过这个代码:

$newProduct->setRelatedProductIds($oldProduct->getRelatedProductIds());

我可以确认相关产品已经添加:

Zend_Debug::dump($newProduct->getRelatedProductIds(), "new product...");

我已经确保使用$newProduct->save()保存了产品;

但是,在产品页面中或在PHP中查询相关产品时,都不会显示任何内容。

我错过了什么?

您可以使用以下代码

$param = array(
    101=>array(
        'position'=>3
    ),
    102=>array(
        'position'=>5
    )
);
$_product->setRelatedLinkData($param);
$_product->save();

因此,setRelatedLinkData需要一个数组,该数组的产品ID为key,其值为array[position为key,integer(position)为value]。