Magento:以编程方式创建Magento订单

时间:2016-04-19 13:14:40

标签: magento magento-1.7 magento-1.9 magento-1.8

我想在magento中以编程方式创建订单,以下是我的脚本但是收到错误。安装了带有样本数据的demo magento。

致命错误:在第108行的F:\ wamp \ www \ magento1910sampledata \ createorder.php中的非对象上调用成员函数getRealOrderId()

    <?php
ini_set("display_errors","1");
include 'app/Mage.php';
Mage::app();

echo "1-";
$email = "pranav@test.com";
$productids = array(448, 450);
//$websiteId = Mage::app()->getWebsite()->getId();
$websiteId = 1;
$store = Mage::app()->getStore();
// Start New Sales Order Quote
$quote = Mage::getModel('sales/quote')->setStoreId($store->getId());
echo "2-";
// Set Sales Order Quote Currency
$quote->setCurrency($order->AdjustmentAmount->currencyID);
$customer = Mage::getModel('customer/customer')
    ->setWebsiteId($websiteId)
    ->loadByEmail($email);
if ($customer->getId() == "") {
    $customer = Mage::getModel('customer/customer');
    $customer->setWebsiteId($websiteId)
        ->setStore($store)
        ->setFirstname('Test')
        ->setLastname('Order')
        ->setEmail($email)
        ->setPassword("password");
    $customer->save();
}
echo "3-";
// Assign Customer To Sales Order Quote
$quote->assignCustomer($customer);
$quote->setIsSuperMode(true);
// Configure Notification
$quote->setSendCconfirmation(1);
echo "4-";
foreach ($productsids as $id) {
    $product = Mage::getModel('catalog/product')->load($id);
    $quote->addProduct($product, new Varien_Object(array('qty' => 1)));
}
echo "5-";
// Set Sales Order Billing Address
$billingAddress = $quote->getBillingAddress()->addData(array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => 'Test',
    'middlename' => '',
    'lastname' => 'Order',
    'suffix' => '',
    'company' => '',
    'street' => array(
        '0' => '601 Blackburn Road',
        '1' => 'Blackburn'
    ),
    'city' => 'Notting Hill',
    'country_id' => 'AU',
    'region' => '491',
    'postcode' => '3168',
    'telephone' => '123456',
    'fax' => '123456',
    'vat_id' => '',
    'save_in_address_book' => 1
));
echo "6-";
// Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => 'Test',
    'middlename' => '',
    'lastname' => 'Order',
    'suffix' => '',
    'company' => '',
    'street' => array(
        '0' => '601 Blackburn Road',
        '1' => 'Blackburn'
    ),
    'city' => 'Notting Hill',
    'country_id' => 'AU',
    'region' => '491',
    'postcode' => '3168',
    'telephone' => '123456',
    'fax' => '123456',
    'vat_id' => '',
    'save_in_address_book' => 1
));
if ($shipprice == 0) {
    $shipmethod = 'freeshipping_freeshipping';
}
echo "7-";
// Collect Rates and Set Shipping & Payment Method
$shippingAddress->setCollectShippingRates(true)
    ->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate')
    ->setPaymentMethod('checkmo');
echo "7.1-";
// Set Sales Order Payment
$quote->getPayment()->importData(array('method' => 'checkmo'));
echo "8-";
// Collect Totals & Save Quote
$quote->collectTotals()->save();
echo "8.1-";
// Create Order From Quote
$service = Mage::getModel('sales/service_quote', $quote);
echo "8.2-";
$service->submitAll();
echo "8.3-";
$increment_id = $service->getOrder()->getRealOrderId();
echo "9-";
// Resource Clean-Up
$quote = $customer = $service = null;
echo "10-";
// Finished
echo $increment_id;
?>

2 个答案:

答案 0 :(得分:2)

尝试按照脚本以编程方式添加顺序。

$transaction = Mage::getSingleton('core/resource_transaction');

    $order = Mage::getModel('sales/order')
      ->setIncrementId('increment_id')
      ->setStoreId('store_id_here')
      ->setStatus('order_status')
      ->setHoldBeforeState('hold_before_state')
      ->setHoldBeforeStatus('hold_before_status')
      ->setIsVirtual('is_virtual')
      ->setBaseCurrencyCode('base_currency_code')
      ->setStoreCurrencyCode('store_currency_code')
      ->setGlobalCurrencyCode('store_currency_code')
      ->setOrderCurrencyCode('order_currency_code');

    // Set Customer data          
    $order->setCustomerEmail('customer_email')
    ->setCustomerFirstname('customer_firstname')
    ->setCustomerLastname('customer_lasttname')
    ->setCustomerIsGuest(1)
    ->setCustomerGroupId(0);    


    // Set Billing Address
    $billingAddress = Mage::getModel('sales/order_address')
        ->setStoreId('store_id_here')
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
        ->setCustomerAddressId('customer_address_id')
        ->setPrefix('prefix')
        ->setFirstname('firstname')
        ->setMiddlename('middlename')
        ->setLastname('lastname')
        ->setSuffix('suffix')
        ->setCompany('company')
        ->setStreet('street')
        ->setCity('city')
        ->setCountryId('country_id')
        ->setRegion('region')
        ->setPostcode('postcode')
        ->setTelephone('telephone')
        ->setFax('fax');

    $order->setBillingAddress($billingAddress);

    // Set Shipping Address
    $shippingAddress = Mage::getModel('sales/order_address')
        ->setStoreId('store_id_here')
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
        ->setCustomerAddressId('customer_address_id')
        ->setPrefix('prefix')
        ->setFirstname('firstname')
        ->setMiddlename('middlename')
        ->setLastname('lastname')
        ->setSuffix('suffix')
        ->setCompany('company')
        ->setStreet('street')
        ->setCity('city')
        ->setCountry_id('country_id')
        ->setRegion('region')
        ->setPostcode('postcode')
        ->setTelephone('telephone')
        ->setFax('fax') ;

    if(!'is_virtual'){
    $order->setShippingAddress($shippingAddress)
    ->setShippingMethod('shipping_method')
    ->setShippingDescription('shipping_method');
    }   

    // payement method 
    $orderPayment = Mage::getModel('sales/order_payment')
    ->setStoreId('store_id_here')
    ->setCustomerPaymentId(0)
    ->setMethod('checkmo')
    ->setPoNumber(' - ');
    $order->setPayment($orderPayment);

    $orderItem = Mage::getModel('sales/order_item')
            ->setStoreId('store_id_here')
            ->setQuoteItemId(0)
            ->setQuoteParentItemId(NULL)
            ->setSku('product_sku') 
            ->setProductType('product_type')
            ->setProductOptions(unserialize('product_option'))
            ->setQtyBackordered(NULL)
            ->setTotalQtyOrdered('qty_ordered')
            ->setQtyOrdered('qty_ordered')
            ->setName('product_name')
            ->setPrice('original_price')
            ->setBasePrice('base_original_price')
            ->setOriginalPrice('original_price')
            ->setBaseOriginalPrice('base_original_price')
            ->setRowWeight('row_weight')
            ->setPriceInclTax('price_incl_tax')
            ->setBasePriceInclTax('base_price_incl_tax')
            ->setTaxAmount('product_tax_amount')
            ->setBaseTaxAmount('product_base_tax_amount')
            ->setTaxPercent('product_tax_percent')
            ->setDiscountAmount('product_discount')
            ->setBaseDiscountAmount('product_base_discount')
            ->setDiscountPercent('product_discount_percent')
            ->setRowTotal('row_total')
            ->setBaseRowTotal('base_row_total');

    $order->addItem($orderItem);


    $order->setShippingAmount('shipping_amount');
    $order->setBaseShippingAmount('base_shipping_amount');

    //Apply Discount
    $order->setBaseDiscountAmount('base_discount_amount');
    $order->setDiscountAmount('discount_amount');

    //Apply Tax
    $order->setBaseTaxAmount('base_tax_amount');
    $order->setTaxAmount('tax_amount');

    $order->setSubtotal('subtotal')      
    ->setBaseSubtotal('base_subtotal')  
    ->setGrandTotal('grand_total')      
    ->setBaseGrandTotal('base_grand_total')
    ->setShippingTaxAmount('shipping_tax_amount')      
    ->setBaseShippingTaxAmount('base_shipping_tax_amount')      
    ->setBaseToGlobalRate('base_to_global_rate')      
    ->setBaseToOrderRate('base_to_order_rate')      
    ->setStoreToBaseRate('store_to_base_rate')      
    ->setStoreToOrderRate('store_to_order_rate')      
    ->setSubtotalInclTax('subtotal_incl_tax')      
    ->setBaseSubtotalInclTax('base_subtotal_incl_tax')      
    ->setCouponCode('coupon_code') 
    ->setDiscountDescription('coupon_code') 
    ->setShippingInclTax('shipping_incl_tax') 
    ->setBaseShippingInclTax('base_shipping_incl_tax') 
    ->setTotalQtyOrdered('total_qty_ordered')
    ->setRemoteIp('remote_ip');

    $transaction->addObject($order);
    $transaction->addCommitCallback(array($order, 'place'));
    $transaction->addCommitCallback(array($order, 'save'));

    $transaction->save();

答案 1 :(得分:0)

Create Order programatically:
$quote = Mage::getModel('sales/quote')->load($quote_id);
$quote->setQuoteCurrencyCode($currency_code);
if ($customerId) {
   $customer = Mage::getModel('customer/customer')->load($customerId);
     $quote->assignCustomer($customer);
    } else {
        $quote->setCustomerEmail($email);
        $quote->setCustomerIsGuest(true);
    }

    if ($shipping_address_id) {
        $shippingAddressData = Mage::getModel('customer/address')->load($shipping_address_id)->getData();
    } else {
        $shippingAddressData = $shippingData;
        unset($shippingAddressData['region']);
    }

    if ($billing_address_id) {
        $billingAddressData = Mage::getModel('customer/address')->load($billing_address_id)->getData();

    } else {
        $billingAddressData = $billingData;
        unset($shippingAddressData['region']);
    }


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

    $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
            ->setShippingMethod($shippingMethod['method'])
            ->setPaymentMethod($paymentMethod['method']);

 $quote->getPayment()->importData(array('method' => $paymentMethod['method']));
    try {
        $quote->collectTotals()->save();

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

        $order = $service->getOrder();
        $quotes = Mage::getModel('sales/quote')
            ->load($order->getQuoteId());
            if ($quotes->getId())
            {
                $quotes->setIsActive(false)
                ->setReservedOrderId(NULL)
                ->save();

            }