2017-03-08 72 views
0

蔭在Magento新的,我想創建訂單 我已經尋找了很多的腳本創建腳本,我得到這個:Magento1.8 - 創建訂單的最佳腳本是什麼?

<?php 

require_once 'app/Mage.php'; 

Mage::app(); 

$quote = Mage::getModel('sales/quote') 
    ->setStoreId(Mage::app()->getStore('default')->getId()); 

    // for guesr orders only: 
    $quote->setCustomerEmail('[email protected]'); 

// add product(s) 
$product = Mage::getModel('catalog/product')->load(431); 
$buyInfo = array(
    'qty' => 1, 
    // custom option id => value id 
    // or 
    // configurable attribute id => value id 
); 
$quote->addProduct($product, new Varien_Object($buyInfo)); 

$addressData = array(
    'firstname' => 'Test', 
    'lastname' => 'Test', 
    'street' => 'Sample Street 10', 
    'city' => 'Somewhere', 
    'postcode' => '123456', 
    'telephone' => '123456', 
    'country_id' => 'SA', 

); 

$billingAddress = $quote->getBillingAddress()->addData($addressData); 
$shippingAddress = $quote->getShippingAddress()->addData($addressData); 

$shippingAddress->setCollectShippingRates(true)->collectShippingRates() 
      ->setShippingMethod('freeshipping_freeshipping') 
      ->setPaymentMethod('cashondelivery'); 

$quote->getPayment()->importData(array('method' => 'cashondelivery')); 

$quote->collectTotals()->save(); 

$service = Mage::getModel('sales/service_quote', $quote); 
$service->submitAll(); 
$order = $service->getOrder(); 

printf("Created order %s\n", $order->getIncrementId()); 
?> 

但是當我運行該腳本它給我的錯誤「頁面無法正常工作」。

你能幫我解決這個問題或給我腳本創建訂單。

注www.site.com/createorder.php 「「我通過運行此腳本」

感謝

+0

附加屏幕截圖的錯誤 – bxN5

回答

0

我想這一次,它的工作原理:

<?php 
include '../app/Mage.php'; 
Mage::app(); 
// Need for start the session 
Mage::getSingleton('core/session', array('name' => 'frontend')); 
try { 

    $product_ids = array(
     $_GET['product_id_1'], 
     $_GET['product_id_2'], 
     $_GET['product_id_3'], 
     $_GET['product_id_4'] 
     ); 
    for($i=0;$i<4;$i++) 
    { 
     addToCart($product_ids[$i]); 
    } 
    /*addToCart($product_id_1); */ 

    Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
    Mage::getSingleton('core/session')->addSuccess('Product added successfully'); 
    header('Location: ' .Mage::getBaseURl(). 'checkout/cart/'); 
} catch (Exception $e) { 
    echo $e->getMessage(); 
} 
function addToCart($product_id) 
{ 
    $qty = '1'; 
    $product = Mage::getModel('catalog/product')->load($product_id); 
    $cart = Mage::getModel('checkout/cart'); 
    $cart->init(); 
    $cart->addProduct($product, array('qty' => $qty)); 
    $cart->save(); 
    return true; 
} 
?> 

鏈接應該是以product_id_1等形式給出四個參數,您可以相應地更改它

+0

感謝您的代碼 但我有問題,當我通過運行代碼: https://uae.arabianoud.com/create/doorder.php 它給我 「產品無法找到。」 即使產品ID是正確的 我增加產品ID是這樣的: 嘗試{ $ product_ids =陣列( $ _GET [ 'product_id_122'], $ _GET [ 'product_id_48'], $ _GET [ 'product_id_50'], $ _GET [ 'product_id_49' ] ); addToCart($ product_ids [$ i]); } } 也可以請你告訴我如何更新客戶的詳細信息和運輸細節 –

+0

這不會工作明顯,代碼會像這樣:$ _GET ['122']而不是$ _GET ['product_id_122' ]等等,讓我知道這是否適合你 –

相關問題