以编程方式创建订单magento成功后重定向到购物车

时间:2013-09-23 08:10:30

标签: magento magento-1.7 checkout

我只为虚拟产品创建,所以我跳过了送货地址 这是我的代码:

public function checkoutAction()
    {
        $productid = Mage::app()->getRequest()->getParam('value');
        $payment = Mage::app()->getRequest()->getParam('payment');
        $session = Mage::getSingleton('customer/session');
        if(!$session->isLoggedIn())
        {
            //login
            $username = Mage::app()->getRequest()->getParam('username');
            $password = Mage::app()->getRequest()->getParam('password');
            try
            {
                $result = $session->login($username,$password);
            }
            catch(Mage_Core_Exception $e)
            {
                $response['status'] = 0;
                $response['message'] = $e->getMessage();
                echo Zend_Json::encode($response);
                return false;
            }

            $session->setCustomerAsLoggedIn($session->getCustomer());
        }
        $cust_id = $session->getId();

        $customer = Mage::getModel('customer/customer')->load($cust_id);

        $quote = Mage::getModel('sales/quote')
                ->setStoreId(Mage::app()->getStore('default')->getId());

        $quote->assignCustomer($customer);


        // add product(s)
        $product = Mage::getModel('catalog/product')->load($productid);

        $buyInfo = array(
                'qty' => 1,
                // custom option id => value id
                // or
                // configurable attribute id => value id
        );
        $quote->addProduct($product, new Varien_Object($buyInfo));

        $billing = $customer->getDefaultBillingAddress();

        if($billing)
        {
            $addressData = array(
                    'firstname' => $billing->getFirstname(),
                    'lastname' => $billing->getLastname(),
                    'street' => $billing->getStreet(),
                    'city' => $billing->getCity(),
                    'postcode' => $billing->getPostcode(),
                    'telephone' => $billing->getTelephone(),
                    'country_id' => $billing->getCountryId(),
                    'region_id' => $billing->getRegionId(), // id from directory_country_region table
            );

            $billingAddress = $quote->getBillingAddress()->addData($addressData);
            /*$shippingAddress = $quote->getShippingAddress()->addData($addressData);

            $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                            ->setShippingMethod('flatrate_flatrate')
                            ->setPaymentMethod('checkmo');*/

            $quote->getPayment()->importData(array('method' => $payment));

            $quote->collectTotals()->save();

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

            $order->setStatus(Mage_Sales_Model_Order::STATE_HOLDED, true)->save();
            $order = Mage::getModel('sales/order')->load($order->getId());


                $session = Mage::getSingleton('checkout/session');
                $session->setLastQuoteId($session->getQuote()->getId())
                        ->setLastSuccessQuoteId($session->getQuote()->getId());

            $response['status'] = 1;
            $response['message'] = $order->getId();
            echo Zend_Json::encode($response);
            return true;
        }
        else
        {
            Mage::getSingleton('customer/session')->addError($this->__('Please fill in your address.'));
            $response['status'] = 2;
            echo Zend_Json::encode($response);
            return false;
        }
    }

在我下订单并付款后,它会将我重定向回购物车页面而不是成功页面。但订单处理成功。我错过了一步吗?如果可以帮助的话。

1 个答案:

答案 0 :(得分:0)

我认为您希望使用$order->getIncrementId()代替$order->getId()

$response['status'] = 1;
$response['message'] = $order->getId();
$this->_redirect('checkout/onepage/success'); //write this code