magento - 成功后,以编程方式创建order重定向到 cart

  显示原文与译文双语对照的内容
79 3

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


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如果能帮助。

时间: 原作者:

137 3

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


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

原作者:
...