Magento set quantity in shopping cart -> quantity not available - magento

I use Magento 1.5.1 to power a fashion webshop. I noticed a strange behaviour from Magento which I hope you can help me resolve:
Here is the scenario:
An item has 5 pieces in stock.
A user adds 5 to its cart
In the mean time someone has bought 1 item so 5 is not available any more. New stock is 4. In my opinion the user should be able to reset the qty to 4.
The user tries to reset the qty to 4. That, however,does not work. All I get at this stage is that the qty is not available and the quantity stays at 5.
EDIT:
To clarify step 4 a little bit more:
I receive a message that the item is out of stock once I try to set the qty to 4.
Is this a known Magento bug? How can I check what is causing this?
Thanks

Oke, finally found a workaround:
\app\code\core\mage\checkout\model\cart.php Line 383 - 386
change:
$item->setQty($qty);
if ($item->getHasError()) {
Mage::throwException($item->getMessage());
}
to:
$oldqty = $item->getQty();
$item->setQty($qty);
if ($item->getHasError() && $qty > $oldqty) {
Mage::throwException($item->getMessage());
}
Now it checks if the new qty is lower then the old qty. If so, continue. Otherwise do the old behaviour.

Related

Magento Cart Rule BUG - Wrongly applied when "less than" and configurable product

I just found out that Magento seems to have a bug since 1.8 relating to cart rules.
let's say we have some configurable products and want to add a "discount" for a specific product if the qty is less then 50. In my case it a surcharge not a discount (you can easily add negative discount so it'll get surcharge by changing two files see http://php.quicoto.com/extra-fee-shopping-cart-price-rules-magento/).
so what does magento do?
1) checks if rule is valid for that product
2) if not it checks if it is a configurable product, then takes the first simple product, and check the rule against that.
in this case true cause qty is less then 50 ( cause this simple product is not even in cart.... )
extending the rule by a "less then 50 and more then 1" didn't worked.
$product = $object->getProduct();
if (!($product instanceof Mage_Catalog_Model_Product)) {
$product = Mage::getModel('catalog/product')->load($object->getProductId());
}
// here, everythign correct. $valid is false cause item is less then x times in cart..
$valid = parent::validate($object);
// this part makes no sense, cause he's checking on a child which is not in cart.
/** /
if (!$valid && $product->getTypeId() == Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
$children = $object->getChildren();
$valid = $children && $this->validate($children[0]);
}/**/
this small snippet is related to it, and in my eyes it doesn't make any sense. why the rule should be checked against the first product of a configurable one? why randomly check the rule against some other product?
does anyone has an idea about that?
my solution for now, just comment this line out ... ;-) and the rule get applied as it should.
greets
felix
here's an image about the rule in magento backend
Looks like $object is instance of Mage_Sales_Quote_Item. If so, it explains why rule is being checked against first child - because it is the only child of configurable product in cart. It can't be more than one child of particular configurable product item in the cart at the same time

magento automatc in stock when updating qty

How to do so that the stock status goes from "not in stock" to "in-stock" when we update qty from 0?
I have to update 600 products from qty = 0 to 1-10. I was hoping that i could use the plugin: mass product updater to do the task. But the plugin will lose its purpose if I need to change stock status on every products manually.
If you have decided to update all products' quantity and stock status at once, then you can write this code in any controller's action and hit that controller's action in browser.
$_products = Mage::getModel('catalog/product')->getCollection();
foreach($_products as $_product){
$product = Mage::getModel('catalog/product')->load($_product['entity_id']);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
$stockItem->setData('manage_stock', 1);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('use_config_notify_stock_qty', 0);
$stockItem->setData('qty', 10);
try{
$stockItem->save();
$product->save();
}catch(Exception $e){
Mage::log($e->getMessage(),null,'mohit.log');
}
}
I hope, my answer could solve your problem.
If not please comment. :)
Please take a look at Magmi:
http://sourceforge.net/projects/magmi/
I have used it to import/update millions of products within minutes ( 30-45 minutes ).
The best part is that the csv column headers are build based on the ids of product fields/attributes so any time you'd like to update a field you can right click, inspect element in your admin panel and get the column header desired.
If you've already committed to your product import of choice you could write an observer that fires off on each product save, check if product quantity is > 0 and set the product to 'in stock' then save.
https://magento.stackexchange.com/questions/9067/catalog-product-save-after-event-for-massaction

To add discount to the product which is 15 days old from created date

I want to add discount to the product when that product exceeds more than 15 days from created date.
Generally i know how to add discount to the product, but i felt this is difficult to set the discount with that condition. It make me as so confused. I have found where to add that attribute in combo box.
\app\code\core\Mage\SalesRule\Model\Rule\Condition\address.php
I got created date of product by $product->getCreatedAt()
But i totally confused "how to do that action?". If anybody know, Please help me guys!
Here i am givin you some brief idea to make customization in catalog rule to check if your product is old from created with 15 days and more.
Try to build your own module to manage this kind of catalog rule in your applicaion
you can take understand from this tutorial to manage catalog rule
specially just go throw last 4 slides to make your own rule to achieve your goal.
I have simply used shopping cart price rules by the following way.
first i had created product attribute named as days. Then I have set the value to that days textbox as programmatically as follows
require_once("app/Mage.php");
Mage::app();
$productCollection = Mage::getModel('catalog/product')->getCollection();
$date2=Date("Y-m-d");
foreach($productCollection as $_product)
{
$product = Mage::getModel('catalog/product')->load($_product->getEntityId());
$date1=$product->getCreatedAt();
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$product->setDays($days);
$product->save();
}
After that i have added that product attribute promo rules. Then i set the condition as if differerence between dates which are from product creation date to now is more than 15 days, to add discount to that particular product.

Magento Bundle Product - Multiple Options with Same products. How to stop Magento from hiding the repetitive products?

I am trying to setup a bundle product within Magento. This product should allow the customer to select 4 free products to include with the bundle. These products can be all different or 4 of the same product.
For example
Free Product 1
Free Product 2
Free Product 3
A customer could select four of Free Product 1, or one of Free Product 1 & 2, with two of Free Product 3.
I am using 4 drop-down input types which each have all three Free products as options. So a customer can choose any of the three products for each Free Gift line item.
Magento is only displaying one of the drop-down select lists, I believe due to the fact that each drop-down contains the same product list.
Where would I need to look to stop Magento from checking if the product options are already listed in a previous selection?
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();

restricting items in cart and calculating

I'm currently trying to create a 'Sample' functionality on my Magento site.
The samples are free but when there more than 5 in the cart, then the total needs to be $10.
I can add the 'sample' products to the cart programatically that's no problem.
My main problem is:
Checking how many Sample products are in the cart (all have a value of $0)
If there are 5 or more, then the total needs to be $10+tax (or add this to the current total)
There cannot be more than 10 samples in the cart at one time(so no more than 10 $0 products)
Many thanks
To loop through the items in your cart you can loop through
foreach (Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems() as $item) {
if ($item->getPrice() == 0) {
//this item is a sample
}
}
To prevent things from adding to the cart you can overwrite the addProduct method in app/code/core/Mage/Checkout/Model/Cart.php to add your own custom logic to prevent products from being added to the cart when you don't want them. To make sure you are overriding correctly, you can learn more at http://alanstorm.com/magento_upgrade_rewrite_override

Resources