2013-10-02 64 views
0

我試圖使用$ cart-> addProduct($ product,$ request)將圖像推送至結帳購物車;在結帳購物車中的文件上傳Magento

如果我在普通產品中使用自定義選項,這是我的請求的樣子。

$_FILES: 
    array(2) { 
    ["options_76_file"] => array(5) { 
    ["name"] => string(14) "Conference.jpg" 
    ["type"] => string(10) "image/jpeg" 
    ["tmp_name"] => string(14) "/tmp/phpkKRgHA" 
    ["error"] => int(0) 
    ["size"] => int(938613) 
    } 
    ["options_80_file"] => array(5) { 
    ["name"] => string(16) "capra-felice.jpg" 
    ["type"] => string(10) "image/jpeg" 
    ["tmp_name"] => string(14) "/tmp/php8SXzIk" 
    ["error"] => int(0) 
    ["size"] => int(93196) 
    } 
} 

Request: 
array(7) { 
    ["uenc"] => string(76) "aHR0cDovL2Rldi5rd2lrd2ViLmNvbS5hdS9uc2ovYWNjZXNzc29yaWVzL3NpbmdsZXQuaHRtbA,," 
    ["product"] => string(2) "22" 
    ["related_product"] => string(0) "" 
    ["options_76_file_action"] => string(8) "save_new" 
    ["options"] => array(1) { 
    [77] => string(3) "251" 
    } 
    ["options_80_file_action"] => string(8) "save_new" 
    ["qty"] => string(1) "1" 
} 

正如你可能已經注意到我正在傳遞2張圖片。 現在我正在嘗試從我的自定義控制器中得到同樣的結果。我設法將產品添加到購物車,但我無法找到負責將文件保存到訂單的功能。 有誰知道Magento如何處理這個? 感謝 Soipo

+0

有天然的Magento沒有功能,節省saveOrder進程中的文件。你必須用普通的php函數/代碼來處理你的文件。 – OSdave

+0

嗨OSdave,我設法保存我的文件沒有問題,但我不能保存選項中的名稱。因此,圖片在結帳時不會顯示,並且會顯示在訂單概覽中。 – soipo

回答

1

我發現在博客上的代碼,

// the path of the file, relative to Magento base directory. 
// For example /media/image.jpg 
$image = "YOURFILE.JPG"; 
// the ID of the product 
$product_id = XXX; 

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

$cart = Mage::getModel('checkout/cart'); 
$cart->init(); 
$params = array(
'product' => $product_id, 
'qty' => 1, 
'options' => array(
12345 => array(
'quote_path' => $image, 
'secret_key' => substr(md5(file_get_contents(Mage::getBaseDir() . $image)), 0, 20)), 
) 
); 

$cart->addProduct($product, $params); 
$cart->save(); 

Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 

爲了這確保了陣列看起來像這樣:

$title= $option['name']; 
       $image = DS."media".DS."logos".DS.$title; 
       $path = Mage::getBaseDir().$image; 

       $imgSize = getimagesize($path); 
       $size = filesize($path); 

       $array = array(
       'type' => "application/octet-stream", 
       'title' => $title, 
       'size' => $size , 
       'width' => $imgSize[0], 
       'height' => $imgSize[1], 
       'quote_path'=> $image, 
       'order_path'=> $image, 

       'secret_key' => substr(md5(file_get_contents($path)), 0, 20)); 

       $options[$key] = $array; 
相關問題