2012-03-21 126 views
0

我有個Magento的情況下,我使用下面的代碼:如何以編程方式在Magento中設置產品屬性?

include_once '../app/Mage.php'; 
Mage::app(); 

try{ 

    $product_id = ''; 

    // get query string 
    if (!isset($_GET['product_id'])) { $product_id = ''; } else { $product_id = $_GET['product_id']; } 
    if (!isset($_GET['qty'])) { $qty = '1'; } else { $qty = $_GET['qty']; } 

    $product = Mage::getModel('catalog/product')->load($product_id); 

    $session = Mage::getSingleton('core/session', array('name'=>'frontend')); 
    $cart = Mage::helper('checkout/cart')->getCart(); 

    $cart->addProduct($product, $qty); 

    $session->setLastAddedProductId($product->getId()); 
    $session->setCartWasUpdated(true); 

    $cart->save(); 

    $result = "{'result':'success'}"; 
    echo $result; 

} catch (Exception $e) { 
    $result = "{'result':'error'"; 
    $result .= ", 'message': '".$e->getMessage()."'}"; 
    echo $result; 
} 

不過,我得到一個錯誤,指出「請指定產品所需的選項(S)。」 如何在前面的代碼中設置一個名爲「Colors」的屬性並將其默認爲「black」? 謝謝!

回答

0

要添加配置,以你需要爲它的屬性指定PARAMS購物車。
數據示例:

 [product] => 3 
     [related_product] => 
     [super_attribute] => Array 
      (
       [75] => 20 
       [85] => 15 
      ) 

     [qty] => 1 

這裏75 - 是 「顏色」 attribute_id,20 - >色的 'option_id' 「黑」
85 - 是 「大小」 attribute_id,15 - > option_id大小的'大'。
因此,檢查你的數據庫是什麼attribute_id Color屬性和「黑」色option_id的。

+0

謝謝,塞爾吉!我能夠找到我需要的屬性ID,並且效果很好。 – Katzumi 2012-03-22 18:42:00

相關問題