2011-11-09 83 views
3

我正在將第三方數據庫的產品導入到我的magento網站。我在網上找到了一個很好的PHP教程。如何將多個描述的產品導入magento

但是,本教程沒有介紹如何將多個描述分配給基於商店的單個產品。

在我的例子中,我有一個既有英文又有法文說明的產品。一個用於我的法國商店,另一個用於英文,我怎樣才能將它們導入到magento中。

我還需要爲標題urlkey完成此操作,併爲每個商店分配不同的類別。

以下是本教程給出的代碼。

<?php 
require_once('/path/to/magento/app/Mage.php'); 
umask(0); 

// Set an Admin Session 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
Mage::getSingleton('core/session', array('name'=>'adminhtml')); 
$userModel = Mage::getModel('admin/user'); 
$userModel->setUserId(1); 
$session = Mage::getSingleton('admin/session'); 
$session->setUser($userModel); 
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl()); 

// Then we see if the product exists already, by SKU since that is unique to each product 
$product = Mage::getModel('catalog/product') 
->loadByAttribute('sku',$_product['sku']); 

if(!$product){ 
// product does not exist so we will be creating a new one. 

$product = new Mage_Catalog_Model_Product(); 

$product->setTypeId('simple'); 
$product->setWeight(1.0000); 
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); 
$product->setStatus(1); 
$product->setSku('UNIQUESKUHERE'); 
$product->setTaxClassId(0); 
$product->setWebsiteIDs(array(0)); // your website ids 
$product->setStoreIDs(array(0)); // your store ids 
$product->setStockData(array(
    'is_in_stock' => 1, 
    'qty' => 99999, 
    'manage_stock' => 0, 
)); 
} 

// set the rest of the product information here that can be set on either new/update 
$product->setAttributeSetId(9); // the product attribute set to use 
$product->setName('Product Title'); 
$product->setCategoryIds(array(0,1,2,3)); // array of categories it will relate to 
$product->setDescription('Description'); 
$product->setShortDescription('Short Description'); 
$product->setPrice(9.99); 

// set the product images as such 
// $image is a full path to the image. I found it to only work when I put all the images I wanted to import into the {magento_path}/media/catalog/products - I just created my own folder called import and it read from those images on import. 
$image = '/path/to/magento/media/catalog/products/import/image.jpg'; 

$product->setMediaGallery (array('images'=>array(), 'values'=>array())); 
$product->addImageToMediaGallery ($image, array ('image'), false, false); 
$product->addImageToMediaGallery ($image, array ('small_image'), false, false); 
$product->addImageToMediaGallery ($image, array ('thumbnail'), false, false); 

// setting custom attributes. for example for a custom attribute called special_attribute 
// special_attribute will be used on all examples below for the various attribute types 
$product->setSpecialAttribute('value here'); 

// setting a Yes/No Attribute 
$product->setSpecialField(1); 

// setting a Selection Attribute 
$product->setSpecialAttribute($idOfAttributeOption); //specify the ID of the attribute option, eg you creteated an option called Blue in special_attribute it was assigned an ID of some number. Use that number. 

// setting a Mutli-Selection Attribute 
$data['special_attribute'] = '101 , 102 , 103'; // coma separated string of option IDs. As ID , ID (mind the spaces before and after coma, it worked for me like that) 
$product->setData($data); 

try{ 
$product->save(); 
} catch(Exception $e){ 
echo $e->getMessage(); 
//handle your error 
} 
?> 

回答

0

我最終還是選擇了magmi做這個工作。

如果您要從其他系統導入舊數據,則需要以編程方式創建此CSV。

的這個CSV應該是什麼樣子的一個例子是here

要爲一個產品做多的描述,簡單的複製產品的排和下面複製它,店內碼字段/網站字段更改爲代碼您需要不同的描述,並在描述字段中輸入新的描述。

現在,當您使用Magmi上傳您的CSV時,您將獲得一個產品,其中有兩個或更多與相關商店/網站相關的描述。

隨時發表評論,並提出更多問題,這對我來說是一個頭痛的問題,我很樂意爲您解除類似的挫折。

1

您是否在使用網站,商店或店鋪瀏覽量?這很重要,因爲如果您使用的是商店瀏覽而不是網站,則無法將稅收/增值稅和定價設置爲「價格點」。

$product=Mage::getModel('catalog/product')->setWebsiteIds(whatever)->setStoreId(whatever)->load(whatever) 

您可以隨時使用數據流配置文件這一點,只需導出你的產品,擺在田間地頭更新,包括一個「商店」欄目。把它重新裝回來,工作很好。

+0

謝謝,思考後,我決定使用CSV加載產品。麻煩是在magento 1.6中,類別是通過名稱而不是id來指定的。我有同名的類別,所以magento不知道哪一個放置我的產品。這真讓我很沮喪。我試過通過創建一個category_ids列來指定category id,但是magento忽略了它。 –

相關問題