1

我正在尝试使用 PHP 在 Magento Community Edition 1.7 中创建一个分组产品,但会绕圈子。我可以按照本指南创建一个简单的产品:

http://inhoo.net/ecommerce/magento/programming-magento/programatically-manually-creating-simple-magento-product/

但我想创建分组产品。

4

2 回答 2

4

以防其他人想知道完整的代码。这里是。希望它可以帮助某人。

$product = new Mage_Catalog_Model_Product();

if (file_exists($imagePath)) {
    $product->addImageToMediaGallery($imagePath,array('thumbnail','small_image','image'),false,false);
}else{
    $fp = fopen('temp/missingImages.txt', 'a');
    fwrite($fp, $sku."\n");
    fclose($fp);
}


$product->setSku($sku.'-grouped');
$product->setAttributeSetId(4);
$product->setTypeId('grouped');
$product->setName($title);
$product->setSubtitle($subtitle);
$product->setCategoryIds($magentoCatId);
$product->setWebsiteIDs(array(1)); 
$product->setDescription($description);
$product->setShortDescription($description);
$product->setPrice($price);
$product->setAuthor($author);
$product->setEditor($editor);
$product->setTable_of_contents($contents);
$product->setFormat($format);               
$product->setWidth($width);
$product->setHeight($height);   
$product->setNumber_of_pages($pagenumber);              
$product->setPublisher($publisher);
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
$product->setStockData(array(
    'is_in_stock' => 1,
    'manage_stock' => 0,
    'use_config_manage_stock' => 1
));

try {
    $product->save();
    echo 'Grouped Product ID: '.$product->getId()." - SKU - ".$sku."-grouped<br/>";
    $group_product_id = $product->getId();


    $assocSql = "SELECT * FROM `table` WHERE `groupingId` = '".$sku."' ";
    $assocQuery = mysql_query($assocSql);
    while($assocRow = mysql_fetch_array($assocQuery)){                      
        $simple_product_id[] = Mage::getModel("catalog/product")->getIdBySku( $assocRow['id']);
    }


    $products_links = Mage::getModel('catalog/product_link_api');
    $group_product_id = $product->getId();

    foreach($simple_product_id as $groupedProd){                
        $products_links->assign ("grouped",$group_product_id,$groupedProd); 
    }

}
catch (Exception $ex) {
    echo $ex->getMessage(). " First catch SKU is - ".$sku."<br/>";
    $fp = fopen('temp/missingSku.txt', 'a');
    fwrite($fp, $sku."\n");
    fclose($fp);
}

$cache = Mage::getSingleton('core/cache');
$cache->flush();

朱利安 :)

于 2013-08-28T13:42:42.083 回答
1
Please see below code to create grouped product as we need to add all product in  gruop data 

    $products_links = Mage::getModel(‘catalog/product_link_api’);
    $products_links->assign (“grouped”,$group_prod_id,$new_prod_id);

请参阅参考站点以获取更多信息:http: //inhoo.net/ecommerce/magento/programming-magento/programatically-manually-creating-simple-magento-product/http://blog.onlinebizsoft.com/magento-programmatically -create-grouped-product-for-import-code/

于 2013-08-22T12:40:03.590 回答