Magento Forum

   
Products are not visible in cart
 
tesarm
Jr. Member
 
Total Posts:  3
Joined:  2012-09-06
 

I work in Magento 1.7.

I programmatically create product after user clicks “add to cart” and add to cart newly created products. Duplicating works well, but in my cart page, I do not see any products (it happens only if I have empty cart). I also don’t see how many products in cart I have in menu.

It’s invisible until I go to order and go through billing and shipping information.

Here is my code:

protected function addProductToCart($product_id$params{

    $product 
Mage::getModel('catalog/product')->load($product_id);

    try 
{
        $session 
Mage::getSingleton('customer/session');
        
$customer_id $session->getId();

        
$cart Mage::getModel('sales/quote')->loadByCustomer($customer_id);

        
$cart->addProduct($product);

        
$cart->save();

        
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

        
$this->_getSession()->setCartWasUpdated(true);

        
// @todo remove wishlist observer processAddToCart

        
Mage::dispatchEvent('checkout_cart_add_product_complete',
            array(
'product' => $product'request' => $this->getRequest(), 'response' => $this->getResponse())
        );


        if (!
$this->_getSession()->getNoCartRedirect(true)) {
            
if (!$cart->getHasError()) {
                $message 
$this->__('%s was added to your shopping cart.'Mage::helper('core')->escapeHtml($product->getName()));
                
$this->_getSession()->addSuccess($message);
            
}
            $this
->_goBack();
        
}

    } 
catch (Exception $e{

    }
}

As I said above - problem above happens only if cart is empty.

Do you have any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
aposadi
Jr. Member
 
Total Posts:  19
Joined:  2013-01-06
 

Mmm…

I think that in this line:

$cart Mage::getModel('sales/quote')->loadByCustomer($customer_id);

your $cart is null.... You haven’t product in cart, so the quote in not created yet…

try to use a log after that:

$cart Mage::getModel('sales/quote')->loadByCustomer($customer_id);
Mage::log($cart,null,'custom.log');

And remember that if $cart is null you catch a exception in :

$cart->addProduct($product);

 
Magento Community Magento Community
Magento Community
Magento Community
 
tesarm
Jr. Member
 
Total Posts:  3
Joined:  2012-09-06
 

Cart is not null

there is var_dump($cart);

object(Mage_Sales_Model_Quote)#64 (21) { ["_eventPrefix":protected]=> string(11) "sales_quote" ["_eventObject":protected]=> string(5) "quote" ["_customer":protected]=> NULL ["_addresses":protected]=> NULL ["_items":protected]=> NULL ["_payments":protected]=> NULL ["_errorInfoGroups":protected]=> array(0) { } ["_preventSaving":protected]=> bool(false) ["_resourceName":protected]=> string(11) "sales/quote" ["_resource":protected]=> NULL ["_resourceCollectionName":protected]=> string(22) "sales/quote_collection" ["_cacheTag":protected]=> bool(false) ["_dataSaveAllowed":protected]=> bool(true) ["_isObjectNew":protected]=> NULL ["_data":protected]=> array(51) { ["entity_id"]=> string(1) "7" ["store_id"]=> string(1) "1" ["created_at"]=> string(19) "2013-05-12 09:42:39" ["updated_at"]=> string(19) "2013-05-21 20:27:48" ["converted_at"]=> NULL ["is_active"]=> string(1) "1" ["is_virtual"]=> string(1) "0" ["is_multi_shipping"]=> string(1) "0" ["items_count"]=> string(1) "0" ["items_qty"]=> string(6) "0.0000" ["orig_order_id"]=> string(1) "0" ["store_to_base_rate"]=> string(6) "1.0000" ["store_to_quote_rate"]=> string(6) "1.0000" ["base_currency_code"]=> string(3) "CZK" ["store_currency_code"]=> string(3) "CZK" ["quote_currency_code"]=> string(3) "CZK" ["grand_total"]=> string(6) "0.0000" ["base_grand_total"]=> string(6) "0.0000" ["checkout_method"]=> NULL ["customer_id"]=> string(1) "3" ["customer_tax_class_id"]=> string(1) "3" ["customer_group_id"]=> string(1) "1" ["customer_email"]=> string(16) "tesarm@gmail.com" ["customer_prefix"]=> NULL ["customer_firstname"]=> string(6) "Michal" ["customer_middlename"]=> NULL ["customer_lastname"]=> string(6) "TesaÅ™" ["customer_suffix"]=> NULL ["customer_dob"]=> string(19) "2013-04-14 23:54:25" ["customer_note"]=> NULL ["customer_note_notify"]=> string(1) "1" ["customer_is_guest"]=> string(1) "0" ["remote_ip"]=> string(13) "85.71.120.101" ["applied_rule_ids"]=> NULL ["reserved_order_id"]=> NULL ["password_hash"]=> NULL ["coupon_code"]=> NULL ["global_currency_code"]=> string(3) "CZK" ["base_to_global_rate"]=> string(6) "1.0000" ["base_to_quote_rate"]=> string(6) "1.0000" ["customer_taxvat"]=> NULL ["customer_gender"]=> string(1) "0" ["subtotal"]=> string(6) "0.0000" ["base_subtotal"]=> string(6) "0.0000" ["subtotal_with_discount"]=> string(6) "0.0000" ["base_subtotal_with_discount"]=> string(6) "0.0000" ["is_changed"]=> string(1) "1" ["trigger_recollect"]=> string(1) "0" ["ext_shipping_info"]=> NULL ["gift_message_id"]=> NULL ["is_persistent"]=> string(1) "0" } ["_hasDataChanges":protected]=> bool(true) ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL ["_isDeleted":protected]=> bool(false) ["_oldFieldsMap":protected]=> array(0) { } ["_syncFieldsMap":protected]=> array(0) { } }

 
Magento Community Magento Community
Magento Community
Magento Community
 
aposadi
Jr. Member
 
Total Posts:  19
Joined:  2013-01-06
 

Have you tried to take a vardump of you cart after the $cart->addProduct?

Can you post the results? one with the empty cart (where you catch the error) and one with products in cart.
So we can check the difference.

another thing for debugging this error: can you add a log in the catch session? similar to:

catch (Exception $e{
                 Mage
::log("error ".$e,null,"custom.log");
    
}

We will find the bug smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top