快速一键订购功能

时间:2013-03-01 17:08:56

标签: magento

立即启动,Magento平台要求客户注册一个帐户,将他们的商品添加到购物车,然后检查出来。如何在不需要添加到购物车的情况下去除所有这些麻烦并实施一键订购功能?

要演示,请查看以下链接:http://royalglasses.pk/index.php/brands/ray-ban-pakistan/ray-ban-aviator-exclusive.html

在右侧,您将看到“订购单”部分,客户可以在其中输入信息并签出,并为他们希望购买的每件商品进行检查。

我还应该提到,商店只在城市内运送,而且只有一种付款方式,即“货到付款”。因此,通常Magento交易的最后两个步骤(送货方式和付款信息)对我的客户来说无用。

1 个答案:

答案 0 :(得分: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.