Magento1.8 - 创建订单的最佳脚本是什么?

时间:2017-03-08 22:40:47

标签: php magento magento-1.8

Iam new in magento,我想创建用于创建订单的脚本 我已经搜索了很多脚本,我得到了这个:

<?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('customer@example.com');

// 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());
?>

但是当我运行这个脚本时,它会给我错误&#34;页面不能正常工作&#34;。

请你帮我解决这个问题,或者给我创建订单的脚本。

注意&#34;我通过&#34; www.site.com/createorder.php"

运行此脚本

由于

1 个答案:

答案 0 :(得分: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的形式给出四个参数,依此类推。你可以相应地改变它。