Magento:在后端代码中以编程方式创建订单

2024-03-07

我尝试在 Magento (1.5.1.0) 的后端创建订单。

这是一些代码:

        // Get the product id stored in the optionValue of the widget
        $productId = $order['customIdNumber'];

        // Load the product
        $product = Mage::getModel('catalog/product')->load($productId);

        // Check whether the product could be loaded
        if($product->getId())
        {
            // Get the customer model
            $customer = Mage::getModel('customer/customer');

            // Set the website id associated with the customer
            $customer->setWebsiteId(Mage::app()->getWebsite()->getId());

            // Try to load the customer by email
            $customer->loadByEmail($order['personAddresses'][0]['email']);

            // Check whether the customer not exists
            if(!$customer->getId())
            {
                // Create the customer
                $customer->setEmail($order['personAddresses'][0]['email']);
                $customer->setFirstname($order['personAddresses'][0]['firstName']);
                $customer->setLastname($order['personAddresses'][0]['lastName']);
                $customer->save();
            }

            // Set the esstial order data
            $orderData = array(
                'currency' => $order['currencyCode'],
                'account'  => array(
                    'group_id' => Mage_Customer_Model_Group::NOT_LOGGED_IN_ID,
                    'email'    => $order['personAddresses'][0]['email']
                ),
                'billing_address' => 
                    'firstname'  => $order['personAddresses'][0]['firstName'],
                    'lastname'   => $order['personAddresses'][0]['lastName'],
                    'street'     => $order['personAddresses'][0]['street'],
                    'city'       => $order['personAddresses'][0]['city'],
                    'country_id' => $order['personAddresses'][0]['country'],
                    'region_id'  => 'BW',
                    'postcode'   => $order['personAddresses'][0]['postalCode'],
                    'telephone'  => '0123456789',
                ),
                'comment' => array(
                    'customer_note' => "[Order has been created by the sellaround widget module]\nCustomer message:\n".
                                       $order['personAddresses'][0]['message']
                ),
                'send_confirmation' => false // does that something?
            );

            // Set the shipping address to the billing address
            $orderData['shipping_address'] = $orderData['billing_address'];

            // Set the payment method
            $paymentMethod = 'checkmo';

            // Set the shipping method
            $shippingMethod = 'flatrate_flatrate';


            // Get the backend quote session
            $quoteSession = Mage::getSingleton('adminhtml/session_quote');

            // Set the session store id
            $quoteSession->setStoreId(Mage::app()->getStore('default')->getId());

            // Set the session customer id
            $quoteSession->setCustomerId($customer->getId());


            // Get the backend order create model
            $orderCreate = Mage::getSingleton('adminhtml/sales_order_create');

            // Import the data
            $orderCreate->importPostData($orderData);

            // Calculate the shipping rates
            $orderCreate->collectShippingRates();

            // Set the shipping method
            $orderCreate->setPaymentMethod($paymentMethod);

            // Set the payment method to the payment instance
            $orderCreate->getQuote()->getPayment()->addData(array('method' => $paymentMethod));

            // Set the shipping method
            $orderCreate->setShippingMethod($shippingMethod);

            // Set the quote shipping address shipping method
            $orderCreate->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod);

            // Add the product
            $orderCreate->addProducts(array($product->getId() => array('qty' => 0)));

            // Initialize data for price rules
            $orderCreate->initRuleData();

            // Save the quote
            $orderCreate->saveQuote(); // neccessary?

            // Create the order
            $order = $orderCreate->createOrder();
        }

我总是遇到“请指定运输方式”的异常情况。在Mage_Sales_Model_Service_Quote::_validate在第 293 行。

异常周围的代码行:

    $method= $address->getShippingMethod();
    $rate  = $address->getShippingRateByCode($method);
    if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
        Mage::throwException($helper->__('Please specify a shipping method.'));
    }

有谁知道为什么我会收到此错误?是因为无法加载费率吗? (该产品不是虚拟的)


我之前通过使用我自己创建的运输方法解决了这个问题。

创建订单的代码也从后端单例更改为“前端方法”,如下所示:

// Get the Quote to save the order
$quote = Mage::getModel('sales/quote')->setStoreId($storeId);

// Set the customer
$quote->setCustomer($customer);

// Add the product with the product options
$quote->addProduct($product, new Varien_Object($productOptions));

// Add the address data to the billing address
$billingAddress  = $quote->getBillingAddress()->addData($addressData);

// Add the adress data to the shipping address
$shippingAddress = $quote->getShippingAddress()->addData($addressData);

// Collect the shipping rates
$shippingAddress->setCollectShippingRates(true)->collectShippingRates();

// Set the shipping method /////////// Here i set my own shipping method
$shippingAddress->setShippingMethod($shippingMethod);

// Set the payment method
$shippingAddress->setPaymentMethod($paymentMethod);

// Set the payment method
$quote->getPayment()->importData(array('method' => $paymentMethod));

// Collect the prices
$quote->collectTotals()->save();

// Get the service to submit the order
$service = Mage::getModel('sales/service_quote', $quote);

// Submit the order
$service->submitAll();

// Get the new order
$newOrder = $service->getOrder();


// Get payment instance
$payment = $newOrder->getPayment();

// Set the buyer data
$this->_importPaymentInformation($payment, $order);

// Set the transaction data
$payment->setData('last_trans_id', $order['transNrPurchaseForReceiver']);

// Save the payment changes
$payment->save();

// Set the order state and save the order
$newOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, $comment)->save();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Magento:在后端代码中以编程方式创建订单 的相关文章

  • 在 JQuery ui 自动完成中显示图像

    我有一个带有 JQuery ui 自动完成功能的脚本 可以完美运行 有一个显示用户名字和姓氏的搜索过程 但在我的数据库中 还有用户的图片 我想将其显示在带有名字和姓氏的建议中 数据库中pic包含图片url 剧本 function searc
  • 增加内存限制时出现奇怪的错误

    我使用的是共享托管环境 PHP 的默认内存限制是 32M 我在 Concrete5 设置方面遇到一些问题 当我尝试登录 Concrete5 的管理面板时 出现内存限制错误Allowed memory size of 33554432 byt
  • Php 转换 GMT 格式的时间

    我有这个字符串2012 06 27 16 17 06我想将其转换为 GMT 格式 我怎样才能做到这一点 多谢 Use gmdate http php net manual en function gmdate php 使用以下命令将当前日期
  • 我的设置未保存在 WordPress 主题选项页面中

    我正在尝试创建一个基于 WordPress 设置 API 的主题选项页面 当我在浏览器中检查 options php 页面时 例如http mysite com wordpress wp admin options php http mys
  • PHP strtotime() 未返回正确的月份

    由于当前月份 年份是 2012 年 1 月 为什么以下代码返回 2011 年 12 月而不是 2011 年 11 月 echo date F Y strtotime 2 months 如果有影响的话 这是在 PHP 5 3 0 上 要获得您
  • 纯旧 PHP 对象 (POPO) 一词的确切含义是什么?

    我想了解一下波波 我搜索了 popo 发现它代表 Plain Old Php Object 但我不确定 Plain Old Php Object 的确切含义 我想知道什么是 popo 以及在哪里使用它 谢谢 普通旧 在此处插入语言 对象是一
  • laravel cron 使用错误

    Laravel 错误 cron 使用 usr bin php home sitevk artisan 计划 运行 1 gt gt dev null 2 gt 1 应用 控制台 内核 use Illuminate Console Schedu
  • Composer 无法下载文件

    我正在尝试在命令行上使用作曲家 php composer phar update php composer phar install php composer phar self update php composer phar selfu
  • 安装 OCI8:如何纠正“使用未定义常量 OCI_COMMIT_ON_SUCCESS”错误?

    我正在尝试在 RedHat 服务器 RHEL7 上为我的 Apache 服务器安装 OCI8 此时 当我尝试使用 Symphony 连接到我的服务器时 出现以下错误 异常 ErrorException 使用未定义的常量 OCI COMMIT
  • php动态创建子域的问题

    你好 我通过以下代码在 php 中创建子域 function subd host port ownername passw request sock fsockopen localhost 2082 if sock print Socket
  • 使用 php ping 网站

    我想创建一个 php 脚本 它将 ping 一个域并列出响应时间以及请求的总大小 这将用于监控网站网络 我尝试过curl 这是我到目前为止的代码 function curlTest2 url clearstatcache return if
  • 这是依赖注入吗?这是一种不好的做法吗?

    我有一个小框架 我是这样编码的 我不确定这是否称为依赖注入 我不知道它是否像设计模式 我也不知道并且想知道是否通过 this因为 param 是一种不好的做法 看看这个 不是一个有效的示例 只是将这些代码写入浏览器中以供解释 This is
  • Laravel:使用 Faker 播种多个独特的列

    介绍 怎么样 伙计们 我有一个关于模型工厂和多个独特列的问题 背景 我有一个名为 Image 的模型 该模型将语言支持存储在单独的模型中 图片文字 图片文字 has an image id栏 语言栏和文本栏 图片文字有一个约束MySQL那个
  • 通过 PHP 连接到 socket.io(nodejs)

    我需要通过 php 连接到 websocket 发送数据并立即断开连接 无需等待套接字的响应 我用了大象io http elephant io 但更新库后不起作用 请告诉我如何通过 PHP 连接到 websocket 我也遇到了这个问题 学
  • 如何使用 AJAX/jQuery 显示打印内容?

    所以我试图理解整个 AJAX jQuery 的事情 现在 当我单独运行这个 PHP 脚本时 我必须等待并观察轮子旋转 直到循环完成然后加载 while row mysql fetch array res postcode to storm
  • laravel 5.3 新的 Auth::routes()

    最近开始使用laravel 5 3写博客 但是运行后出现一个问题php artisan make auth 当我运行这个时 它会在我的web php 这是其中的代码 Auth routes Route get home HomeContro
  • 检查一个类是否是另一个类的子类

    我想在不创建实例的情况下检查一个类是否是另一个类的子类 我有一个类 它接收类名作为参数 作为验证过程的一部分 我想检查它是否属于特定的类系列 以防止安全问题等 有什么好的方法可以做到这一点吗 is subclass of http php
  • 访问 Magento 购物车和/或结帐中的运费

    请注意 这个问题是关于运费 而不是价格 有一个重要的区别 即运输方式为店主支付的费用是多少 而不是客户支付的费用 The shipping tablerate数据库表包括一个cost字段 该字段填充在Mage Shipping Model
  • 将二进制数据从 C# 上传到 PHP

    我想将文件从 Windows C 应用程序上传到运行 PHP 的 Web 服务器 我知道 WebClient UploadFile 方法 但我希望能够分块上传文件 以便我可以监控进度并能够暂停 恢复 因此 我正在读取文件的一部分并使用 We
  • 是否可以使用流上下文在 PHP 下使用 FTPS?

    我了解到使用ftpsPHP for Windows 下的 ftp ssl connect 很困难 您被要求进入构建自己的二进制文件以包括 Open SSL 的漫长旅程 我找到了以下建议phpseclib http phpseclib sou

随机推荐