magento same product with many quantity different size change one time - magento

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();

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);

Configurable products not searchable in Magento

My site has a need to have all products as configurable with no associated products (for now). So the site is mostly used as a catalog. We use configurable as opposed to simple products because we anticipate in the future we will add the associated products.
However, I notice that configurable products without associated products are not searchable. I tried to work around this by looking at this piece of code inside the Mage_CatalogSearch_Model_Resource_Search_Collection class.
// search in catalogindex for products as part of configurable/grouped/bundle products (current store)
$where = array();
foreach ($options as $option) {
$where[] = sprintf('(attribute_id=%d AND value=%d)', $option['attribute_id'], $option['option_id']);
}
if ($where) {
$selects[] = (string)$this->getConnection()->select()
->from($resource->getTableName('catalogindex/eav'), 'entity_id')
->where(implode(' OR ', $where))
->where("store_id={$storeId}");
}
I tried to comment out this code but still returns empty. Which code should I comment out?
Thank you
Ensure that the visibility is set to "Catalog, Search" and then under Inventory select "In stock" under stock availability or override the "Manage Stock" setting to "No".
Then of course ensure that indexes and cache are up to date.

How to get preconfigure price from product view page to listing page in magento

Recently i came across the issue is When i display price of bundle product on listing page which shows either lowest price or highest price total by making total of all product within a bundle.
As i have set few product to default selected within a group so, on the product view page that default poduct price from perticualar group has been calculated in final total. but on price of product listing page count the minimum amount from the group of product.
So, what happens that customer view the product detail from product listing where it shows the lowest price but, on product view page it shows different price because now it counts default product price instead of minimum price from the group.
I want to display pre-configured product price from view page to product listing page.
Thanks in advance!
// load product
$product = new Mage_Catalog_Model_Product();
$product->load(165);
$priceModel = $product->getPriceModel();
// get options
$block = Mage::getSingleton('core/layout')->createBlock('bundle/catalog_product_view_type_bundle');
$options = $block->setProduct($product)->getOptions();
$price = 0;
foreach ($options as $option) {
$selection = $option->getDefaultSelection();
if ($selection === null) {
continue;
}
$price += $priceModel->getSelectionPreFinalPrice($product, $selection, $selection->getSelectionQty());
}

Add product to cart on customer registration in 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?

Resources