1

In my magento project I have created a superuser who can directly add product to customers cart from his cart. Here I am loading customer quote by their emailId and adding each product from super user cart to customer quote, Here I have an issue that if superuser is trying to add more than half of the product inventory then the customer cart product quantity updated with the full inventory. (if total inventory is 100 and superuser trying to share with quantity 60 then customer cart quantity will be 100)

    try{
    $customer = Mage::getModel("customer/customer"); 
    $customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
    $buyerEmail = $this->getRequest()->getParam('email');

    $customer->loadByEmail($buyerEmail);
    if(!$customer->getId()){
        Mage::getSingleton('core/session')->addError($buyerEmail .' is not exist');
        $this->_redirect('checkout/cart');
        return;
    }
    $quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
    $quoteId = $quote->getId();
    if(!$quoteId){
        $store_id = Mage::app()->getStore()->getId();
        $quote = Mage::getModel('sales/quote');
        $quote->setStoreId($store_id);
        $quote->assignCustomer($customer);

    }
    $superUser = Mage::getSingleton('customer/session')->getCustomer();
    $cart = Mage::getModel('sales/quote')->loadByCustomer($superUser);
    foreach ($cart->getAllItems() as $item) {   
        $productQty = $item->getQty();
        $productId = $item->getProduct()->getId();
        foreach($quote->getAllVisibleItems() as $item) {
            if ($item->getData('product_id') == $productId) {
                $quote->removeItem($item->getId())->save();
            }
            if ($item->getData('is_shared_item') == 1) {
                $quote->removeItem($item->getId())->save();
            }
        }
        $_product = Mage::getModel('catalog/product')->load($productId);
        $quote->addProduct($_product, $productQty);
        $this->updateSharedItem($quote, $productId);
    }
    $quote->collectTotals()->save();

    Mage::getSingleton('core/session')->addSuccess('Customer cart updated to '. $buyerEmail .' Successfully');

    }catch(Exception $e){
        $this->log($e->getMessage());
        Mage::getSingleton('core/session')->addError('Customer cart not updated please try again');
    }
    $this->_redirect('checkout/cart');

1 Answer 1

1

Magento Already have a function to copy one cart to another.

See the sample snippet.

$cart = Mage::getModel('sales/quote')->loadByCustomer($superUser);
$quote = Mage::getModel('sales/quote')->loadByCustomer($customer);

$quote->merge($cart);
$quote->collectTotals()->save();

Try using above code. This may resolve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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