I´m using this Code (in cartsontroller.php) to add a extra Product to Cart Page - but I dont see it in Cart itself?
There are no Errors or something like that...
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(25);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Related
As the title asks, when a customer logs into their account I would like to clear the shopping cart of any items it contains.
I would like to do this in Magento 1.9. Can it be done?
Here is the code to delete all shopping cart items of currently logged in single customer:
$cart = Mage::getSingleton('checkout/cart');
$quoteItems = Mage::getSingleton('checkout/session')
->getQuote()
->getItemsCollection();
foreach( $quoteItems as $item ){
$cart->removeItem( $item->getId() );
}
$cart->save();
first of all i added a product to category suppose id 4 programmatically by $product->save();. Then i get id of added product also i had mentioned visibility to catalog, search and enabled true and stock is maintained.
but i call that product to add to cart by ajax request it shows error the product could not be found. please suggest me the solutions. thank you..
my code is as below
$product1 = Mage::getModel('catalog/product')->setStoreId(
Mage::app()->getStore()->getId()
)->load($product->getId());
$params = array(
'product' => $product1->getId(),
'qty' => 1,
'value' => $product1->getPrice()
);
try {
print_r($product1);
$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product1, $params);
$cart->save();
} catch (Exception $ex) {
echo $ex->getMessage();
}
the above code is to add custom product to cart.
i had checked that the product is adding successfully to the backend but error throws when adding to cart
Try adding to quote instead,
$product = Mage::getModel('catalog/product')->load($productId);
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($product, 1);
$quote->collectTotals()->save();
I need to load a cart by a quoteId, cause I want to add a product to a different cart than the current cart. Is this possible?
TIA!
$cartId = 99;
$cart = Mage::getModel('sales/quote')->load($cartId);
$productId = 55;
$product = Mage::getModel('catalog/product')->load($productId());
$cart->addProduct($product);
I try to add a extra product to the cart. I have created a observer for this.
<?php
class WP_Plugadapter_Model_Observer
{
public function hookToControllerActionPostDispatch($observer)
{
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToAddToCartAfter($observer)
{
$request = $observer->getEvent()->getRequest()->getParams();
$_product = Mage::getModel('catalog/product')->load($request['product']);
$extra_functions = $_product->getExtra_functions();
if(!empty($extra_functions)){
$extra_functions = explode(',', $extra_functions);
if(array_search('121', $extra_functions)){
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
$cart->save();
if (!$cart->getQuote()->getHasError()){
Mage::log("Product ADD TO CART is added to cart.");
}else{
Mage::log("BOEM");
}
}
}
}
}
When i check mine system log i see the following log message. Product ADD TO CART is added to cart.
I have no clue what i'm doing wrong. When a load the script standalone it's working fine.
For example:
<?php
include_once '../app/Mage.php';
Mage::app();
umask(0);
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
$cart->save();
Is it possible that in a observer you have it to do it in a different way?
The problem is that cart's quote object is not saved to the database and later in the request processing is overwritten by the quote object from the session. Why the cart quote is not saved is quite confusing. The save method of the quote model expects that the internal property _hasDataChanges is set to true. This property is, however, remains at false, even though a product was added to the quote.
You can force that property to be set to true by adding some data (any property would do) to the quote using the setData method:
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
//force _hasDataChanges to true
$cart->getQuote()->setData('updated', true);
$cart->save();
Alternatively you can use the checkout session quote object to add a product to the cart
if(array_search('121', $extra_functions)){
$cart = Mage::getSingleton('checkout/cart');
$qty = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote()
->addProduct(
Mage::getModel('catalog/product')->load(10934),
$qty)
->save();
$cart->save();
if (!$cart->getQuote()->getHasError()){
Mage::log("Product ADD TO CART is added to cart.");
}else{
Mage::log("BOEM");
}
}
I have a small script, which fills my magento shopping cart with products. It's used for a quickorder form. So it gets called with /quickorder.php?sku1=123&qty1=1&sku2=124&qty2=1 etc.
It's working well for my default store, but for the second store, it seems as it always wants to fill the default shopping cart. At least it redirects me to the default cart url.
How can I get this working to add products to my cart whatever store I'm currently in?
Thanks in advance
<?php
require_once 'app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend')); // Session erzeugen
$Cart = Mage::getSingleton('checkout/cart'); // Instanz zum Warenkorb
for ($count=1; $_POST['sku'.$count]!=''; $count++) {
$sku = $_POST['sku'.$count];
$qty = $_POST['qty'.$count];
$catprod = Mage::getModel('catalog/product');
$product_id = $catprod->getIdBySku($sku);
if ($product_id) {
$ProdObj = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($product_id); // Produkt laden
$Cart->addProduct($ProdObj, $qty); // Produkt in den Warenkorb einfügen
Mage::getSingleton('checkout/session')->addSuccess('Artikelnummer "'.$sku.'" wurde '.$qty.'x hinzugefügt.');
} else Mage::getSingleton('checkout/session')->addError('Artikelnummer "'.$sku.'" wurde nicht gefunden.');
}
$Cart->save(); // Warenkorb speichern
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
header('Location: '. Mage::getUrl('checkout/cart'));
?>
You can get store id by product, like below :
$productModel = Mage::getModel('catalog/product');
$product_id = $productModel->load($object->getId());
$store_id = $product_id->getStoreId();