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



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

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;
        }
    }

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

我认为你想使用$order->getIncrementId()而不是$order->getId()

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

相关内容

  • 没有找到相关文章

最新更新