I need to write a cron job to update my products inventory and price hourly.
The variables are these.
I have 6000 products with their own IDs which my accounting system uses (not magento).
These 6000 turn into 31,000 products. The reason for this is they are car parts, so a Mazda 3 may use the same part as a Honda civic, users on my site will be searching by make, model, and year (all attributes). The only way I could figure out how to do this by importing 31,000 different parts (so they have 31,000 magento IDs).
So I have also imported my accounting system ID as an attribute (my_part_id) which all products have.
So I would like my cron to search for all products which have my_part_id and update the qty and price.
Can someone please point me in the right direction to get this done? I am very comfortable using PHP and MYSQL, I just don't fully understand the Magenta Database yet and don't want to mess up any table relationships that I don't know about.
There are several possible answers. Let’s consider the most simple of them when you have an array with an attribute value. The idea is simple. You need to load a collection of products under each attribute. Then, specify a value for each attribute. When done, you should save this product and its stock item if needed.
If you have a lot of data, the process may take additional time. However, unlike the direct sql request, all the events are working in default Magento.
<?php public function testAction() { $attributeValues = array ( '1' => array ( 'qty' => 11, 'price' => 100 ), '2' => array ( 'qty' => 121, 'price' => 101 ), '3' => array ( 'qty' => 111, 'price' => 102 ) //etc ); $attributeCode = 'my_part_id'; foreach ($attributeValues as $attributeValue => $attributeParams) { $collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToSelect($attributeCode); $collection->addFieldToFilter($attributeCode, $attributeValue); /** @var Mage_Catalog_Model_Product $item */ foreach ($collection->getItems() as $item) { $qty = $attributeParams['qty']; // get qty of the item $price = $attributeParams['price']; // get price of the item $inStock = $qty > 0 ? 1 : 0; // Check is item in stock // Set price $oldPrice = $item->getPrice(); $item->setPrice($price); // Optional: $item->setSpecialPrice($specialPrice) etc. // Set qty /** @var Mage_CatalogInventory_Model_Stock_Item $stockItem */ $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($item); $oldQty = $stockItem->getQty(); $stockItem->setQty($qty)->setIsInStock($inStock); // Save changes if ($oldPrice != $price) { $item->save(); } if ($oldQty != $qty) { $stockItem->save(); } } } echo 'Done'; exit; }
Hello,
Thank you very much for this.
I will be importing a CSV file, the CSV will contain the 6000 products (my_part_id) all as separate rows.
If I start at your
foreach ($attributeValues as $attributeValue => $attributeParams) {
and loop through my CSV rows, will I achieve the same results?
I won't have all the separate SKUs (31,000) in advance to put in an array, just the (my_part_id). Im not sure if your code is querying all products with that attribute or not
Can you please attach your CSV file?
Hello,
Here is a sample of what it will be
http://www.darkstarmedia.net/wp-content/uploads/2016/06/inventory.csv.zip
we can make it look like whatever is needed, you will notice that is won't just be a number, it will be alpha-numeric is some cases
Hello,
Any ideas about this?