I have found some code to update product details
$_product->setData('manage_stock', 1);
$_product->setData('qty', $newQty);
$_product->save();
What is Difference between $_product->setData() & $_product->save() here ?
$_product->setData('qty', 10); is assign the value to the column(qty,etc).
$_product->save(); is save the that value to the DB.
I will show you an example. Suppose we have a product A with quantity 1 by default. Now we need to programmatically change the quatity of the product to 5. So for that we have wrote the following code.
Without save()
$_product = Mage::Registry('current_product');
$qty_before = $_product->getQty();
$_product->setData('qty', 5);
$qty_after = $_product->getQty();
echo "Quantity Available before setting the quantity property =".$qty_before."<br>";
echo "Quantity Available after setting the quantity property =".$qty_after."<br>";
Now it will show the following result
Quantity Available before setting the quantity property = 1.
Quantity Available after setting the quantity property = 5.
Now we have commented out the third line of code and then refreshed our page. Now our result will be look like this.
Quantity Available before setting the quantity property = 1.
Quantity Available after setting the quantity property = 5.
With save()
$_product = Mage::Registry('current_product');
$qty_before = $_product->getQty();
$_product->setData('qty', 5);
$qty_after = $_product->getQty();
$_product->save();
echo "Quantity Available before setting the quantity property =".$qty_before."<br>";
echo "Quantity Available after setting the quantity property =".$qty_after."<br>";
Now it will show the following result
Quantity Available before setting the quantity property = 1.
Quantity Available after setting the quantity property = 5.
Now, again we have commented out the third line of code and then refreshed our page. Now our result will be look like this.
Quantity Available before setting the quantity property = 5.
Quantity Available after setting the quantity property = 5.
So what is the difference? If we are setting a property means, it will update that field value for that time. But actually the value is not saving in database. In order to save the data to the database, we have to use save() method. Otherwise your changes that you made by setData() will be ignored by Magento.
If you load a product and then just use setData for any attribute, the product will show that value temporarily on that page only.
but if you use. $_product->save(); after the setData that value will be permanently saved in database and can be displayed on other pages as well.
Related
I have a server action to update product costs when selected in tree view:
bom_obj = env["mrp.bom"]
for product in object.browse(context.get('active_ids')):
price = 0
bom = bom_obj._bom_find(product=product)
if bom:
price = product._calc_price(bom)
product.write({'standard_price':price})
But This unfortunately only selects records visible in tree view, not ALL the records in product.product
I tried:
bom_obj = env["mrp.bom"]
product_obj = env["product.product"]
product_ids = product_obj.search(cr, uid, [])
for product in product_ids:
price = 0
bom = bom_obj._bom_find(product=product)
if bom:
price = product._calc_price(bom)
product.write({'standard_price':price})
Could you please tell me how to loop through each record of product.product.
OR
instead, tell me how I would go about update price of record selected in m2o field.
I know how to trigger the code in server action, I just need to know how to get record from m2o field. I will use this when product_id is changed in Sale Order Line, to update the price as it is selected.
Thanks
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
Each product in magento has indiviual minimum qty for to be in stock
I use magmi to update the stock from an external csv file.
Issue is that on successful updation of csv , magmi does not refer to minimum qty value to set the product "in stock" or "out of stock"
So all my products endup being "in Stock" and only when I edit and save the product in admin it sets it right.
above scenario explained again:
if Current qty= 1 & mim_qty = 05 and in csv qty= 100
after magmi run (re-indexing done)
new qty= 100 and i can see at front end at list.phtml
next
Current qty= 100 & mim_qty = 05 and in csv qty= 3
after magmi run (re-indexing done)
new qty= 3 and **i can see at front end at list.phtml**
following setting is common in bot case
Manage stock = 1;
use_config_manage_stock = 1;
min_qty = 05;
If you want min_qty to be parsed by magmi, then you need to provide it as input to magmi beside qty value.
Magmi relies 95% on input data , not current existing DB data (except not to replicate select/multiselect options,or checking if a product exists, getting attributes metadata)
So , min_qty is not checked against existing value but input value. if no min_qty is set on input, then magmi does not update is_in_stock based on existing value of min_qty.
That's a behaviour i could enhance in next release.
In class Magmi_ProductImportEngine under function updateStock()
Instead of:
$mqty=(isset($item["min_qty"])?$item["min_qty"]:0);
I have added:
$gsql = "SELECT min_qty FROM cataloginventory_stock_item WHERE product_id=?";
$grvalue = $this->selectAll($gsql, array($pid));
foreach($grvalue as $gcalminqty) {
$gfinalminqty = $gcalminqty['min_qty'];
}
$gfinalminqty = (isset($gfinalminqty) ? $gfinalminqty : 0);
$mqty = (isset($item["min_qty"]) ? $item["min_qty"] : $gfinalminqty);
This looks for min_qty in CSV, if not read from Magento, otherwhise use default.
I want to add additional price with product(simple) price, I am trying to do this with the help of custom attributes. I add a custom attribute "Margin Price" and I want to add up this custom attribute value (margin price) with the base price of the product in the template file.
I am updating all product price after each 5 minutes by cron job, thats why I think I have to do add margin price with base product price by this way.
I added it successfully in product list page and in product view page, but have problem with how to add this margin price with base price in the cart and onepage checkout?
Here is the code on the product list page and same for the product detail page which works fine for me in magento 1.6.x.
$regularPrice = number_format($_product->getFinalPrice(), 2);
//echo $regularPrice = $this->getPriceHtml($_product, true
$priceWithoutComma = str_replace(",", "",$regularPrice);
settype($priceWithoutComma, "float");
$marPrice = $_product->getMarginPrice();
settype($marPrice, "integer");
$finalPrice = $priceWithoutComma + $marPrice;
echo $finalPrice.Mage::app()->getLocale()->currency(Mage::app()->getStore()->
getCurrentCurrencyCode())->getSymbol();
I am doing this right way or I have to changes the whole process?
Looks like you might need to consider a different approach. The reason being that echoing the price from a template file does not modify the price of the item in any way. It simply outputs a calculation.
You'll need to learn a bit about event listeners for this one to work.
Here's a blog post of mine on how to do this.
I am working on ajax module for Shopping cart in Magento. Consider i have a configurable product with 2 simple products configured as its two sizes (Small an Medium). When user selects and adds the item to cart, i cannot see the specific product id (small) in the url.
But instead supper_attribute is posted to my controller.
Is it possible for me to get the actual product id of size "Small" with the super attribute.
Below is my supper attribute array
[super_attribute] => Array
(
[129] => 128
)
129 = attribute_id (Size)
128 = attribute value (Small)
Please suggest me in this scenario. Please let me know if my question is not clear.
Thanks
Try this:
$childProduct = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($request->getData('super_attribute'), $product);
Where $product is the configurable product object.
For the Class Reference