Magento - Mage_Core_Exception:请选择有效的付款方式

时间:2016-02-16 03:22:50

标签: magento

我使用以下代码

主动创建订单
function createSaleOrderForMagento()
    {

        $customer_id = $this->sale_lib->get_customer();
        $items = $this->sale_lib->get_cart();
        if($customer_id==-1)
        {
            return;
        }
        $cust_info = $this->Customer->get_info($customer_id);
        $primaryAddress = $cust_info->getPrimaryShippingAddress();
        require_once '../app/Mage.php';
        Mage::init();

        $websiteId = Mage::app()->getWebsite()->getId();
        $store = Mage::app()->getStore();
        // Start New Sales Order Quote
        $storeId=$store->getId();
        $quote = Mage::getModel('sales/quote')->setStoreId($storeId);

        // Set Sales Order Quote Currency
         $baseCurrencyCode = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
            $currentCurrencyCode = Mage::app()->getStore($storeId)->getCurrentCurrencyCode();
        $quote->setCurrency('VND');
        $customer = Mage::getModel('customer/customer')->load($customer_id);



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

        // Configure Notification
        $quote->setSendCconfirmation(0);



        foreach($items as $line=>$item)
        {


            $product=Mage::getModel('catalog/product')->load($item['item_id']);
            $quote->addProduct($product,new Varien_Object(array('qty'   => 1)));

        }


        // Set Sales Order Billing Address
        // set Billing Address
        $billing = $customer->getDefaultBillingAddress();
        $billingAddress = Mage::getModel('sales/order_address')
            ->setStoreId($storeId)
            ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
            ->setCustomerId($customer->getId())
            ->setCustomerAddressId($customer->getDefaultBilling())
            ->setCustomer_address_id($billing->getEntityId())
            ->setPrefix($billing->getPrefix())
            ->setFirstname($billing->getFirstname())
            ->setMiddlename($billing->getMiddlename())
            ->setLastname($billing->getLastname())
            ->setSuffix($billing->getSuffix())
            ->setCompany($billing->getCompany())
            ->setStreet($billing->getStreet())
            ->setCity($billing->getCity())
            ->setCountry_id($billing->getCountryId())
            ->setRegion($billing->getRegion())
            ->setRegion_id($billing->getRegionId())
            ->setPostcode($billing->getPostcode())
            ->setTelephone($billing->getTelephone())
            ->setFax($billing->getFax());
        $quote->setBillingAddress($billingAddress);


        // Set Sales Order Shipping Address
        $shipping = $customer->getDefaultShippingAddress();
        $shippingAddress = Mage::getModel('sales/order_address')
            ->setStoreId($storeId)
            ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
            ->setCustomerId($customer->getId())
            ->setCustomerAddressId($customer->getDefaultShipping())
            ->setCustomer_address_id($shipping->getEntityId())
            ->setPrefix($shipping->getPrefix())
            ->setFirstname($shipping->getFirstname())
            ->setMiddlename($shipping->getMiddlename())
            ->setLastname($shipping->getLastname())
            ->setSuffix($shipping->getSuffix())
            ->setCompany($shipping->getCompany())
            ->setStreet($shipping->getStreet())
            ->setCity($shipping->getCity())
            ->setCountry_id($shipping->getCountryId())
            ->setRegion($shipping->getRegion())
            ->setRegion_id($shipping->getRegionId())
            ->setPostcode($shipping->getPostcode())
            ->setTelephone($shipping->getTelephone())
            ->setFax($shipping->getFax())->setShippingMethod('freeshipping_freeshipping');

        $quote->setShippingAddress($shippingAddress)
            ->setShipping_method('freeshipping_freeshipping')
        ->setPaymentMethod('checkmo');
        $quote->save();

        $quote->getShippingAddress()
            ->setShippingMethod('freeshipping_freeshipping')
            ->setCollectShippingRates(true)
            ->setPaymentMethod('checkmo')
            ->collectTotals();

        $quote->save();

        // Create Order From Quote
        $service = Mage::getModel('sales/service_quote', $quote);
        //var_dump($service);

        $service->submitAll();
        $increment_id = $service->getOrder()->getRealOrderId();

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

        // Finished
        return $increment_id;
    } 

但我收到了一个错误:

  

(!)致命错误:未捕获的异常' Mage_Core_Exception'同   消息'请选择有效的付款方式。'在   第595行的C:\ wamp \ www \ pmc \ app \ Mage.php       (!)Mage_Core_Exception:请选择有效的付款方式。在第595行的C:\ wamp \ www \ pmc \ app \ Mage.php

请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

如果检查输出此错误的情况,您将看到下一步:

    if (!($this->getQuote()->getPayment()->getMethod())) {
        Mage::throwException(Mage::helper('sales')->__('Please select a valid payment method.'));
    }

这意味着您需要初始化Mage_Sales_Model_Quote_Payment模型并设置您的付款方式:

$quote->getPayment()->setMethod('checkmo');