【问题标题】:Magento - set currency when creating order programatically?Magento - 以编程方式创建订单时设置货币?
【发布时间】:2013-09-14 15:27:10
【问题描述】:

我有一个支持多币种(英镑、欧元、美元)的开发者 magento。我正在尝试以 Euro 针对我的开发 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');

如何启用电子邮件通知?

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

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

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

    必须在$quote-&gt;collectTotals()-&gt;save();之前定义

    【讨论】:

      【解决方案2】:

      您拥有一家使用 3 种货币的商店还是拥有多家使用一种货币的商店?如果您有第二个选项必须选择使用正确货币的商店,现在您使用基础货币设置基本商店,以便 magento 使用英镑创建订单

      $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
      

      【讨论】:

        【解决方案3】:

        如果你正在使用报价,你可以使用这个:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-11
          • 1970-01-01
          • 1970-01-01
          • 2021-03-01
          • 1970-01-01
          • 2013-05-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多