2013-03-01 41 views
0

Magento平臺馬上就要求客戶註冊一個賬戶,將其物品添加到購物車,然後檢查出來。我將如何去除所有這些麻煩並實現一鍵訂單功能,而無需添加到購物車?一鍵快速訂單功能

爲了演示,看看這個鏈接:http://royalglasses.pk/index.php/brands/ray-ban-pakistan/ray-ban-aviator-exclusive.html

在右邊,你會看到一個「訂單式」部分,在這裏客戶可以輸入自己的信息,並檢查了他們這樣做的每一個人他們希望購買的物品。

我還要提到的是店裏唯一的船舶在城市裏面,有隻有一個方法支付的,那就是,「貨到付款」。因此,通常Magento交易(發貨方式和付款信息)中的最後兩個步驟對我的客戶無效。

回答

0

我會繼續遵循Magento邏輯,而不會破壞它的正常流程。來自我之前完成的腳本的代碼片段描述了來賓客戶的完整結賬流程。你的應該不會有太大的不同。請不要複製和粘貼,查看代碼並根據需要調整代碼。

 
//Load the product from the posted form and cart and quote models. 
$product = Mage::getModel('catalog/product')->load((int) $_POST['product_id']); 
$cart = Mage::getSingleton('checkout/cart'); 
$quote = $cart->getQuote(); 

//Add the product to the cart and set the session to updated 
$params = array('product' => $product->getId(), 'qty' => (int) $_POST['qty']); 
$cart->addProduct($product, $params); 
$cart->save(); 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 

//Load variables needed for the order process and transaction object to save the order. 
$transaction = Mage::getModel('core/resource_transaction'); 
$storeId = $customer->getStoreId(); 
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId); 

//Load the order model and set some default values 
$order = Mage::getModel('sales/order') 
       ->setIncrementId($reservedOrderId) 
       ->setStoreId($storeId) 
       ->setQuoteId($quote->getId()) 
       ->setGlobalCurrencyCode('USD') 
       ->setBaseCurrencyCode('USD') 
       ->setStoreCurrencyCode('USD') 
       ->setOrderCurrencyCode('USD'); 

//Assign customer as guest, $customer is array from $_POST['customer']  
$order->setCustomerEmail($customer['email']) 
       ->setCustomerFirstname($customer['firstname']) 
       ->setCustomerLastname($customer['lastname']) 
       ->setCustomerGroupId(0) 
       ->setCustomerIsGuest(1); 

//Prepare billing address and add it to the order 
$billingAddress = Mage::getModel('sales/order_address') 
       ->setStoreId($storeId) 
       ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) 
       ->setCustomerId(0) 
       ->setCustomerAddressId(0) 
       ->setCustomerAddressId(0) 
       ->setFirstname($customer['firstname']) 
       ->setLastname($customer['lastname']) 
       ->setStreet($customer['address']) 
       ->setCity($customer['city']) 
       ->setCountryId($customer['country_id']) 
       ->setPostcode($customer['postcode']) 
       ->setTelephone($customer['phone']); 
$order->setBillingAddress($billingAddress); 

//Prepare shipping address and add it to the order, then set the shipping method 
$shippingAddress = Mage::getModel('sales/order_address') 
       ->setStoreId($storeId) 
       ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) 
       ->setCustomerId(0) 
       ->setCustomerAddressId(0) 
       ->setCustomerAddressId(0) 
       ->setFirstname($customer['firstname']) 
       ->setLastname($customer['lastname']) 
       ->setStreet($customer['address']) 
       ->setCity($customer['city']) 
       ->setCountryId($customer['country_id']) 
       ->setPostcode($customer['postcode']) 
       ->setTelephone($customer['phone']); 

//Add the shipping address and the shipping method.    
$order->setShippingAddress($shippingAddress) 
       ->setShippingMethod('yourshipping_code') 
       ->setShippingDescription('Your shipping description'); 

//Add your payment method. 
$orderPayment = Mage::getModel('sales/order_payment') 
            ->setStoreId($storeId) 
            ->setCustomerPaymentId(0) 
            ->setMethod('cod'); //cod is Cash on Delivery 
$order->setPayment($orderPayment); 


//Loop through the items in the quote, create order item and add it to the order, you have only one product, so it will add only one order item. 
foreach ($quote->getItemsCollection() as $item) 
{ 
    $_prod = Mage::getModel('catalog/product')->load($item->getProduct()->getId()); 
    $rowTotal = $item->getCalculationPrice() * $item->getQty(); 
    $orderItem = Mage::getModel('sales/order_item') 
        ->setStoreId($storeId) 
        ->setQuoteItemId(0) 
        ->setQuoteParentItemId(NULL) 
        ->setProductId($_prod->getId()) 
        ->setProductType($_prod->getTypeId()) 
        ->setQtyBackordered(NULL) 
        ->setTotalQtyOrdered($item->getQty()) 
        ->setQtyOrdered($item->getQty()) 
        ->setName($_prod->getName()) 
        ->setSku($_prod->getSku()) 
        ->setPrice($item->getPrice()) 
        ->setBasePrice($item->getPrice()) 
        ->setOriginalPrice($item->getPrice()) 
        ->setRowTotal($rowTotal) 
        ->setBaseRowTotal($rowTotal); 
    $order->addItem($orderItem); 
} 

//Set the totals 
$subTotal = $quote->getGrandTotal(); 
$order->setSubtotal($subTotal) 
     ->setBaseSubtotal($subTotal) 
     ->setGrandTotal($subTotal) 
     ->setBaseGrandTotal($subTotal); 


//First, place the order, then save it.  
$transaction->addObject($order); 
$transaction->addCommitCallback(array($order, 'place')); 
$transaction->addCommitCallback(array($order, 'save')); 
$transaction->save(); 
$quote->save(); 

//Clear the session and the cart. 
Mage::getSingleton('checkout/session')->clear(); 
foreach($quote->getItemsCollection() as $_item) 
{ 
    Mage::getSingleton('checkout/cart')->removeItem($_item->getId())->save(); 
} 

//Redirect the user back to where you want.