2

I am trying to implement a module in Magento 1.9. And i want to enable the customer to select a specific items or all items from cart before place the order. For example customer add the item A, and item B in the cart. Then during checkout at first step he will decide for which items he want to place order

Option 1: Item A only. After place the order Item B will remain in cart

Option 2: Item B only. After place the order Item A will remain in cart

Option 3: Item A and B. After place the order cart will be empty.

Can you please let me know any idea how can i achieve the above explained functionality? Thank you very much.

0

Magento have the wish-list concept. You can use that for this functionality.

Initially you can use wish-list instead of cart and then at the time of place order you can give the selection to user which item they have to place order.

That item you can move in cart and checkout, rest will be there in wish-list.

If you don't want to show wish-list then you can rename it and use as a cart.

  • @Anish actually when we add the items to the cart it also apply the promotion and all other rules related to the cart. So according to your logic i can not get the cart related functionality on wish list. And secondly we want to maintain a wish list separately for wishlist feature. :( – Abbas Feb 2 '17 at 6:18
0

I think that what you're asking is (close to) impossible. At the end of the checkout, Magento will convert the quote into an order. The quote contains all products that are in your cart, along with other stuff like totals, shipping costs, etc. So having items in your cart that need to remain in the cart, while others are converted to order items. That would at least take some heavy modifications in the checkout logic in Magento.

Therefor I'd go for the wishlist, that's also said by Ashish. When you add a product to your cart, the shopping cart promotion rules will be applied to the quote. But only for as long as those rules are active. After that, when you review the cart, the rules will be disabled/removed for the quote as well. And when you add a product to the cart at a later moment (from the wishlist), the rules will still be applied.

0

We can achieve it using the two carts for one customer at the same time. When customer will choose the items then remaining items move to the temporary cart. And after place the order or for edit the selected items we can get the un-selected items from the temporary cart against the customer. I have implemented it using the described way. I hope it will help some one else.

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_CustomOrder>
            <version>0.1.0</version>
        </Namespace_CustomOrder>
    </modules>

    <global>
        <models>
            <Namespace_customorder>
                <class>Namespace_CustomOrder_Model</class>
            </Namespace_customorder>
        </models>

        <blocks>
           <!-- <customorder>
                <class>Namespace_CustomOrder_Block</class>
            </customorder>-->

            <checkout><!-- alias of the block group you are rewriting -->
                <rewrite><!-- reserved tag: specify that you are rewriting something -->
                    <cart>Namespace_CustomOrder_Block_Checkout_Cart</cart> <!-- tag: the rest of the alias of the class you are rewriting. value: the name of your class that rewrites the core class -->
                </rewrite>
            </checkout>
        </blocks>

        <resources>
            <Namespace_customorder_setup>
                <setup>
                    <module>Namespace_CustomOrder</module>
                </setup>
            </Namespace_customorder_setup>
        </resources>

        <helpers>
            <Namespace_customorder>
                <class>Namespace_CustomOrder_Helper</class>
            </Namespace_customorder>
        </helpers>

        <events>
            <sales_order_place_after>
                <observers>
                    <Namespace_customorder>
                        <type>singleton</type>
                        <class>Namespace_customorder/observer</class>
                        <method>handleOrderComplete</method>
                    </Namespace_customorder>
                </observers>
            </sales_order_place_after>
        </events>
    </global>

    <frontend>
        <routers>
            <!-- <Namespace_customorder>
                <use>standard</use>
                <args>
                    <module>Namespace_CustomOrder</module>
                    <frontName>customorder</frontName>
                </args>
            </Namespace_customorder>-->
            <!-- Overriding the checkout controller for custom order checkout -->
            <checkout>
                <args>
                    <modules>
                        <Namespace_CustomOrder before="Mage_Checkout">Namespace_CustomOrder</Namespace_CustomOrder>
                    </modules>
                </args>
            </checkout>
        </routers>

    </frontend>
</config>

Block/Checkout/Cart.php

class Namespace_CustomOrder_Block_Checkout_Cart extends Mage_Checkout_Block_Cart
{
    /**
     * Return customer quote items
     *
     * @return array
     */
    public function getItems()
    {
        $currentQuoteItems = parent::getItems();
        foreach ($currentQuoteItems as $currentQuoteItem){
            $currentQuoteItem->setSelected(true);
        }
        $temporaryQuoteItems = Mage::helper('Namespace_customorder')->getTemporaryQuote()->getAllVisibleItems();
        return array_merge($temporaryQuoteItems,$currentQuoteItems);
    }
}

controllers/CartController.php

<?php
require_once 'Mage/Checkout/controllers/CartController.php';

class Namespace_CustomOrder_CartController extends Mage_Checkout_CartController
{
    public function updatePostAction()
    {
        if (!$this->_validateFormKey()) {
            $this->_redirect('*/*/');
            return;
        }

        $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
        if($updateAction == 'custom_checkout'){
            $temporaryQuote = Mage::helper('Namespace_customorder')->getTemporaryQuote();
            $currentQuote = $this->_getCart()->getQuote();
//            $temporaryQuote->removeAllItems();
//            $temporaryQuote->collectTotals()->save();
            $selectedItems = $this->getRequest()->getParam('selected_items');
            $currentCartVisibleItems = $this->_getCart()->getQuote()->getAllVisibleItems();
            foreach ($currentCartVisibleItems as $visibleItem){
                if(!in_array($visibleItem->getId(), $selectedItems)){
                    $this->moveItem($visibleItem, $temporaryQuote);
                    $currentQuote->removeItem($visibleItem->getId());
                }
            }
           // $currentQuote->collectTotals()->save();

            $temporaryCartVisibleItems = $temporaryQuote->getAllVisibleItems();
            foreach ($temporaryCartVisibleItems as $visibleItem){
                if(in_array($visibleItem->getId(), $selectedItems)){
                    $this->moveItem($visibleItem, $currentQuote);
                    $temporaryQuote->removeItem($visibleItem->getId());

                }
            }

            $currentQuote->collectTotals()->save();
            $temporaryQuote->collectTotals()->save();
            $this->_goBack();
        }else {
            return parent::updatePostAction(); // TODO: Change the autogenerated stub
        }
    }

    private function moveItem($item, $quote){
            $found = false;
            foreach ($quote->getAllVisibleItems() as $quoteItem) {
                if ($quoteItem->compare($item)) {
                    $quoteItem->setQty($quoteItem->getQty() + $item->getQty());
                    $found = true;
                    break;
                }
            }

            if (!$found) {
                $newItem = clone $item;
                $quote->addItem($newItem);
                if ($item->getHasChildren()) {
                    foreach ($item->getChildren() as $child) {
                        $newChild = clone $child;
                        $newChild->setParentItem($newItem);
                        $quote->addItem($newChild);
                    }
                }
            }


        $quote->collectTotals()->save();
    }

}

Helper/Data.php

class Namespace_CustomOrder_Helper_Data extends Mage_Core_Helper_Abstract {

    public function getTemporaryQuote(){
        $temporaryQuote = null;
        $customerSession = Mage::getSingleton('customer/session');
        if(!($temporaryQuote = $customerSession->getTemporaryQuote()))
        {
            $customer = $customerSession->getCustomer();
            $temporaryQuote = Mage::getModel('sales/quote')
                ->getCollection('sales/quote')
                ->addFieldToFilter('customer_id', $customer->getId())
                ->addFieldToFilter('is_temporary', TRUE)
                ->getFirstItem();
            if (!$temporaryQuote->getId()) {
                $temporaryQuote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId())
                    ->setIsActive(0)
                    ->setCustomer($customer)
                    ->setCustomerId($customer->getId())
                    ->setIsTemporary(TRUE)
                    ->save();
            }
            $customerSession->setTemporaryQuote($temporaryQuote);
        }
        return $temporaryQuote;
    }

    public function setTemporaryAsCurrentQuote(){
        if($temporaryQuote = Mage::helper('Namespace_customorder')->getTemporaryQuote()) {

            $temporaryQuote->setIsActive(1)->setIsTemporary(0)->save();
            Mage::getSingleton('customer/session')->setTemporaryQuote(null);
        }
    }
}

Model/Observer.php

class Namespace_CustomOrder_Helper_Data extends Mage_Core_Helper_Abstract {

    public function getTemporaryQuote(){
        $temporaryQuote = null;
        $customerSession = Mage::getSingleton('customer/session');
        if(!($temporaryQuote = $customerSession->getTemporaryQuote()))
        {
            $customer = $customerSession->getCustomer();
            $temporaryQuote = Mage::getModel('sales/quote')
                ->getCollection('sales/quote')
                ->addFieldToFilter('customer_id', $customer->getId())
                ->addFieldToFilter('is_temporary', TRUE)
                ->getFirstItem();
            if (!$temporaryQuote->getId()) {
                $temporaryQuote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId())
                    ->setIsActive(0)
                    ->setCustomer($customer)
                    ->setCustomerId($customer->getId())
                    ->setIsTemporary(TRUE)
                    ->save();
            }
            $customerSession->setTemporaryQuote($temporaryQuote);
        }
        return $temporaryQuote;
    }

    public function setTemporaryAsCurrentQuote(){
        if($temporaryQuote = Mage::helper('Namespace_customorder')->getTemporaryQuote()) {

            $temporaryQuote->setIsActive(1)->setIsTemporary(0)->save();
            Mage::getSingleton('customer/session')->setTemporaryQuote(null);
        }
    }
}

sql/namespace_customorder_setup/install-0.1.0.php

$installer = Mage::getResourceModel('sales/setup', 'default_setup');
$installer->startSetup();


//$installer->getConnection()->dropColumn($installer->getTable('sales_flat_quote_item'), 'selected');
//$installer->getConnection()->dropColumn($installer->getTable('sales_flat_order_item'), 'selected');



/*$installer->addAttribute('quote_item', 'selected', array(
    'type' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
    'required' => false,
    'default' => true
));

$installer->addAttribute('order_item', 'selected', array(
    'type' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
    'required' => false,
    'default' => true
));*/


$installer->addAttribute('quote', 'is_temporary', array(
    'type' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
    'required' => false,
    'default' => false
));

$installer->endSetup();

To call my controller i have also edited the checkout/cart.phtml and for selection checkout/cart/item/default.phtml

  • In the posted code and actual code which i execute i have changed just namespace name. – Abbas Feb 7 '17 at 5:23
  • Could you update the Model/Observer.php seem this file was not correct – Tuyen Nguyen May 27 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.