2013-09-23 12 views
1

我只爲虛擬產品創建之後,所以我跳過送貨地址 這是我的代碼:編程創造秩序的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; 
     } 
    } 

後,我下訂單並付款,重定向我回到購物車頁面而不是成功頁面。但訂單成功處理。我錯過了一些步驟? Thx如果可以幫助。

回答

0

我認爲你要使用$order->getIncrementId()代替$order->getId()

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