Magento - 以编程方式创建订单时设置货币?

时间:2013-09-14 15:27:11

标签: magento magento-1.7

我有一个启用多种货币的开发magento(GBP,EUR,USD)。我正在尝试在欧元中针对我的dev magento创建销售订单 - 但订单仍以GBP(我的基础货币)创建。

知道我做错了什么吗?到目前为止,这是我的代码:

<?php

// Init Magento In Admin Store Context
require_once '/home/www-data/public_html/app/Mage.php';
$app = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

// Function To Generate Magento Sales Order
function CreateOrder($orderData = array())
{
    // Validate Order Data
    if (!sizeof($orderData)) {
        exit('Invalid Order Data');
    }
    $orderData = (object)$orderData;
    if (!sizeof($orderData->BasketData)) {
        exit('Sales Order Basket Cannot Be Empty');
    }

    // Anticipate Error
    try
    {
        // Start New Sales Order Quote
        $quote = Mage::getModel('sales/quote')
                 ->setStoreId($orderData->StoreId);

        // Set Sales Order Quote Currency
        $quote->setCurrency($orderData->Currency);

        // Load Sales Order Customer
        $customer = Mage::getModel('customer/customer')
                    ->setWebsiteId($orderData->CustomerWebsiteId)
                    ->load($orderData->CustomerId);

        // Assign Customer To Sales Order Quote
        $quote->assignCustomer($customer);

        // Configure Notification
        $quote->setSendCconfirmation($orderData->SendConfirmation ? '1' : '0');

        // Add Products To Sales Order Quote
        foreach ($orderData->BasketData as $orderLine)
        {
            // Add Product To Sales Order Quote
            $quote->addProduct(
                Mage::getModel('catalog/product')->load($orderLine['ProductId']),
                new Varien_Object(array(
                    'price' => floatval($orderLine['Price']),
                    'qty'   => intval($orderLine['Qty'])
                ))
            );
        }

        // Set Sales Order Billing Address
        $billingAddress = $quote->getBillingAddress()->addData(array(
            'firstname'  => $orderData->BillingAddress['Firstname'],
            'lastname'   => $orderData->BillingAddress['Lastname'],
            'company'    => $orderData->BillingAddress['Company'],
            'street'     => array($orderData->BillingAddress['AddressLine1'], $orderData->BillingAddress['AddressLine2']),
            'region_id'  => 0,
            'region'     => $orderData->BillingAddress['Region'],
            'city'       => $orderData->BillingAddress['City'],
            'postcode'   => $orderData->BillingAddress['Postcode'],
            'country_id' => getCountryId($orderData->BillingAddress['Country']),
            'telephone'  => $orderData->BillingAddress['Telephone']
        ));

        // Set Sales Order Shipping Address
        $shippingAddress = $quote->getShippingAddress()->addData(array(
            'firstname'  => $orderData->ShippingAddress['Firstname'],
            'lastname'   => $orderData->ShippingAddress['Lastname'],
            'company'    => $orderData->ShippingAddress['Company'],
            'street'     => array($orderData->ShippingAddress['AddressLine1'], $orderData->ShippingAddress['AddressLine2']),
            'region_id'  => 0,
            'region'     => $orderData->ShippingAddress['Region'],
            'city'       => $orderData->ShippingAddress['City'],
            'postcode'   => $orderData->ShippingAddress['Postcode'],
            'country_id' => getCountryId($orderData->ShippingAddress['Country']),
            'telephone'  => $orderData->ShippingAddress['Telephone']
        ));

        // Collect Rates and Set Shipping & Payment Method
        $shippingAddress->setCollectShippingRates(true)
                        ->collectShippingRates()
                        ->setShippingMethod($orderData->ShippingMethod)
                        ->setPaymentMethod($orderData->PaymentMethod);

        // Set Sales Order Payment
        $quote->getPayment()->importData(array('method' => $orderData->PaymentMethod));

        // Collect Totals & Save Quote
        $quote->collectTotals()->save();

        // Create Order From Quote
        $service = Mage::getModel('sales/service_quote', $quote);
        $service->submitAll();
        $increment_id = $service->getOrder()->getIncrementId();

        // Resource Clean-Up
        $quote = $customer = $service = null;

        // Finished
        return $increment_id;
    }
    catch (Exception $e)
    {
        // Error
        exit($e->getMessage());
    }
}

// HELPER FUNCTION
function getCountryId($countryName) {
    $countryId = '';
    $countryCollection = Mage::getModel('directory/country')->getCollection();
    foreach ($countryCollection as $country) {
        if ($countryName == $country->getName()) {
            $countryId = $country->getCountryId();
            break;
        }
    }
    $countryCollection = null;
    return $countryId;
}

// TEST Order Create Function
echo CreateOrder(array(
    'Currency'          => 'EUR', // Euro
    'StoreId'           => 3,     // Trade Store View
    'CustomerWebsiteId' => 2,     // Trade Website
    'CustomerId'        => 5,     // Latheesan K.
    'SendConfirmation'  => false,
    'BasketData'        => array
    (
        array('ProductId' => 3, 'Price' => 13.00, 'Qty' => 1), // VCF001
        array('ProductId' => 6, 'Price' =>  1.00, 'Qty' => 1), // VCF002
        array('ProductId' => 8, 'Price' => 14.99, 'Qty' => 1), // VCF003
    ),
    'BillingAddress'    => array
    (
        'Firstname'     => 'Latheesan',
        'Lastname'      => 'Kanes',
        'Company'       => 'Intelli B.I. Ltd',
        'AddressLine1'  => 'Unit 1, Eastman Road',
        'AddressLine2'  => 'Acton',
        'Region'        => '',
        'City'          => 'London',
        'Postcode'      => 'W3 7QS',
        'Country'       => 'United Kingdom',
        'Telephone'     => '0208 428 2832',
    ),
    'ShippingAddress'   => array
    (
        'Firstname'     => 'Latheesan',
        'Lastname'      => 'Kanes',
        'Company'       => 'Intelli B.I. Ltd',
        'AddressLine1'  => 'Unit 1, Eastman Road',
        'AddressLine2'  => 'Acton',
        'Region'        => '',
        'City'          => 'London',
        'Postcode'      => 'W3 7QS',
        'Country'       => 'United Kingdom',
        'Telephone'     => '0208 428 2832',
    ),
    'ShippingMethod'    => 'flatrate_flatrate',
    'PaymentMethod'     => 'checkmo'
));

?>

此外,此行未在订单创建时启用电子邮件通知:

// Configure Notification
$quote->setSendCconfirmation($orderData->SendConfirmation ? '1' : '0');

如何启用电子邮件通知?

3 个答案:

答案 0 :(得分:1)

我的解决方案是将货币设置为订单的当前商店

Mage::app()->getStore($storeId)->setCurrentCurrency(Mage::getModel('directory/currency')->load('CHF'));

必须在$quote->collectTotals()->save();

之前定义

答案 1 :(得分:0)

您有一个商店有3种货币或多个商店有一种货币吗?如果您有第二个选项必须选择具有适当货币的商店,现在您使用基础货币设置基本商店,以便magento创建带有GBP的订单

$app = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

或者尝试类似的东西(第一个选项)?

$order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId(0)
->setGlobal_currency_code('USD')
->setBase_currency_code('USD')
->setStore_currency_code('USD')
->setOrder_currency_code('USD');
//Set your store currency USD or any other

答案 2 :(得分:0)

如果您使用的是报价,可以使用:

$quote = Mage::getModel('sales/quote');
$currency = Mage::getModel('directory/currency')->load('USD'); 
$quote->setForcedCurrency($currency);
相关问题