Add product to cart on customer registration in magento - magento

I want that when customer register product (which is choosen in backend) should get added to cart. I have done this :
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load(154);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, 1)->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
in AccountController.php in createPostAction().
But it is not showing but whenever customer buys anything it gets added into cart. I am doing anything wrong...?
-Thnx in advance.

The problem is, the quote object in the session has already collected totals and would not do it again when the cart is saved. Because of this the quote's item count is zero, even though a quote item has been successfully added and saved. Modify your code like this:
$cart->product($product, 1);
$cart->getQuote()->setData('totals_collected_flag', false);
$cart->save();
And this should solve the problem.
A suggestion: would it not be better to implement this functionality in an observer listening to the customer_register_success event?

Related

How to change atribute value for all products in one category

let me first explain my problem, I need to change my atribute for free shipping for all the products in a single category. I know how to read atribute value, because I am displaying banner when a product has a free shipping atribute.
Now, what if I have to set these atribute values for all products in a single category?
What will be the best way to achieve that?
It would be very usefull if, I could change values from backend.
I have found, that you can add an atribute for a category, but sometimes, those atributes won't be the same.
I am using Magento 1.9.2
Thank you!
It's not possible to make filter products by category n admin panel. Simple script will make this
<?php
require 'app/Mage.php';
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4); //visibility in catalog,search
foreach($products as $product) {
$product->setAttribute('new value');
$product->save();
}
Just create new php file, put it in main Magento diroctory and run by cli or url.

Magento relevant product when product is added to cart

I am programming an extension which adds relevant products to the cart once you add an item to the cart. Example if you are buying a pen I am going to add paper to the cart. Bundled packages are not an option since I need to match certain conditions. I tried the following:
I set up an event listener "sales_quote_item_collection_products_after_load" scan all the products in cart and add the relevant products. Sadly you have to reload the cart in order to make the products appear.
I used this code in, my event listener, to add products to cart:
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
// Add a product (simple); id:12, qty: 3
$cart->addProduct(12, 3);
$cart->save()
The strange thing is that removing products using the cart helper works (without refreshing):
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $productId) {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
}
}
Is there a way to tell Magento to "requote" or what would you recommend? I also thought of adding the product, at the add to cart listener. But in that case I will need to implement it as well on update and remove, so it will work correctly. Using sales_quote_item_collection_products_after_load after load seemed to be the best option, since I have everything in one place.
You need to change in your code
$product_model = Mage::getModel('catalog/product');
$product_id =35;
$my_product = $product_model->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => 1));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Could I get Status Shopping Cart in magento from anywhere

Let me know how can I get status of shopping cart in magento from anywhere ?
I mean that Is It possible to get an error like
- your item "marlboro" count has been exceeded ! At present .
There is no so much in stock, please decrease it
Thanks
You can use something like that on an event that is fired on every page like controller_front_init_before
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$product = $item->getProduct();
$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')
->loadByProduct($product)->getQty();
if ($stocklevel<$item->getQty()){
...the logic that will permit to show the customer that the requested
product is not avaiable in this quantity...
}
}

magento same product with many quantity different size change one time

I have a little experience in magento. All my products have custom size in option.
All products have different sizes and different prices.
Customer adds one product with quantity 5 to cart. So 5 products of this size are added to cart. When the customer adds another product with different size all products in cart change to this size.
How can i prevent this behavior?
Unless you're doing this programmatically (that is writing the code), there's no way to do this.
When Magento adds a product, it first looks into the quote / shopping cart to see if one already exists. If one does, it pulls that one and adds to the quantity. There is no way to turn this off.
Programmatically, you very manually add an item to a shopping cart. This is how...
$cart = Mage::getSingleton("checkout/cart");
foreach ($products_to_add as $product_id => $custom_options) {
$product = Mage::getModel("catalog/product")->load($product_id);
$options = new Varien_Object(array("options" => $custom_options,
"qty" => 1));
// some products may result in multiple products getting added to cart
// I beleive this pulls them all and sets the custom options accordingly
$add_all = $product->getTypeInstance(true)
->prepareForCartAdvanced($options, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL);
foreach ($add_all as $add_me) {
$item = Mage::getModel('sales/quote_item');
$item->setStoreId(Mage::app()->getStore()->getId());
$item->setOptions($add_me->getCustomOptions())
->setProduct($add_me);
$item->setQty(1);
$cart->getQuote()->addItem($item);
}
}
// when done adding all the items, finally call save on the cart
$cart->save();

Magento : catalog_product_save_before with mass action

I've made a new mass action to refresh some attributes on selected products based on a algorithm.
I need to trigger "catalog_product_save_before" as if I was saving my product trough the standard admin product creation/edition.
Thanks a lot for your help.
Code sample :
<catalog_product_save_before>
<observers>
<SaponeWebConcept_AttributeToCategories>
<type>singleton</type>
<class>attributetocategories/observer</class>
<method>beforeSave</method>
</SaponeWebConcept_AttributeToCategories>
</observers>
</catalog_product_save_before>
Where it works (standard product creation/edition)
$event = $observer->getEvent();
$product = $event->getProduct();
//And then I edit my product
Where it doesn't : I get my $productID, load it, then save, but the above code isn't triggered.
$product = Mage::getModel('catalog/product')->load($productID);
//Some verifications
$product->save();
$productID is set in a foreach where I got all selected products ID of the product grid.
Mass action doesn't fire catalog_product_save_before event.
use catalog_product_attribute_update_before event instead and then do nested product load/save
I just did it in my magento 1.6.2 code and it worked fine.
loop through product ids{
$product = Mage::getModel('catalog/product')->load($productID);
... some code ...
$product->save();
}
be sure to set all important changes to a product before nested $product->save() invocation so that this event handler has all product attributes in correct state
I haven't looked it up in code, but I'm guessing your mass action is not firing a save at all. As far as I can remember a mass action save jumps into the same save model method as the regular save does.
You need to call the setDataChanges() method on the product object to indicate something was/will be changed.
Here is a simple example from my code:
Mage::getModel('catalog/product')->load($productId)->setDataChanges(true)->save();
This looks a bit odd on its own, but I have an observer, which will do some magic on product save.
If you have a look at Mage_Core_Model_Abstract::save() you can see it checks if the product needs to be saved:
if (!$this->_hasModelChanged()) {
return $this;
}
Therefore if you don't make any changes to the product or don't tell it that there are data changes, then it assumes there is nothing to save and the catalog_product_save_before won't be triggered.

Resources