【问题标题】:Magento1.8 - What is the best script for creating order?Magento1.8 - 创建订单的最佳脚本是什么?
【发布时间】:2017-07-29 17:27:38
【问题描述】:

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

但是当我运行这个脚本时,它给了我错误“页面不工作”。

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

注意“我通过“www.site.com/createorder.php”运行这个脚本

谢谢

【问题讨论】:

  • 附上错误截图

标签: php magento magento-1.8


【解决方案1】:

我试过一次,效果很好:

<?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等。你可以相应地改变它。

【讨论】:

  • 感谢您的代码,但是当我运行代码时遇到问题:uae.arabianoud.com/create/doorder.php 它给了我“找不到产品”。即使产品 ID 是正确的,我也这样添加了产品 ID:尝试 { $product_ids = array( $_GET['product_id_122'], $_GET['product_id_48'], $_GET['product_id_50'], $_GET['product_id_49' ]); for($i=0;$i
  • 这显然不起作用,代码将如下所示:$_GET['122'] 而不是 $_GET['product_id_122'] 等等,让我知道这是否适合你
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
相关资源
最近更新 更多