Magmi stock status issue - magento

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.

Related

Qty available by location (again)

I know this was discussed before but it's not really working for me. How can i get qty available for location or warehouse my product is now.
(Most of the answers are in old API and this one not really working for me)
class ProductProduct(models.Model):
_inherit = 'product.template'
available_qty = fields.Integer(
string='Qty By Loc',
compute='product_qty_location_check',
)
def product_qty_location_check(self):
if self:
self.available_qty = self.with_context({'location' : self.source_location.id}).qty_‌​available
AttributeError: 'product.template' object has no attribute 'source_location'
To get the quantity by location you need to search the location with the product_id in stock.quant
Use the below sample in your compute function:
quant_sr = self.env["stock.quant"].search([('location_id','=',self.source_location.id),('product_id','=',self.product_id.id)])
qty = 0.0
for quant in quant_sr:
qty += quant.qty
print qty
First you have to find out your location and/or warehouse. There are enough possibilities to do so:
use a wizard which has a field
search by reference e.g. self.env.ref('my_module.my_location')
use other sources
Now you can use them on product.product's _product_available(). Just call that method on one or more products (recordssets) and evaluate the result of this method. To filter for locations and/or warehouses use the context. For example:
# get a recordset of all products
products = self.env['product.product'].search([])
# get a special location (my_location)
location = self.env.ref('my_module.my_location')
quantities = products.with_context(location=location.id)._product_available()
# print the quantities
for product_id, quantity_dict in quantities.iteritems():
print product_id
print quantity_dict
The same is possible with with_context(warehouse=warehouse.id) or even with list of IDs or names: with_context(location=[1,2,3]), with_context(location="My Location")

Server Action: Loop through each record of model

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

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

What is Difference between $_product->setData() & $_product->save()?

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.

Magento: Import product prices for an additional website?

I have a Magento installation, with two websites on it:
Retail (default)
Trade
Currently all the prices have been imported as default and so the prices are set the same on both websites. I now need to import the lower prices just for the trade website.
I know this can be done manually per product, but how do I go about importing these prices (with their SKU's so that they only apply to the trade store?
Any help much appreciated!
I suggest doing a Product Export first so you can see all of the columns that are used. Locate a SKU from your Trade store and see what the values are for that column.
You should see a column called _product_websites. In my installation, this column has "base" in it. On yours it will probably say "base" and "trade" (whatever you specified for your website code). You can sort by this column in Excel or other spreadsheet software and remove all of the rows that just have "base" in it so you're left with "trade". Now you can update your prices, save the sheet and re-import your file.
hth
You can simply follow the following Magento blog post:
http://www.blog.magepsycho.com/updating-product-prices-in-magento-in-easier-faster-way/
Just you need to add store_id filter in following method as:
function _updatePrices($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newPrice       = $data[1];
$storeId = $data[2];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();
 
    $sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
                SET  cped.value = ?
            WHERE  cped.attribute_id = ?
            AND cped.entity_id = ?
AND store_id = ?";
    $connection->query($sql, array($newPrice, $attributeId, $productId, $storeId));
}
Of course you need to use third column of prices.csv for store_id.
Let me know if that helps.

Resources