Update Magento stocks with CSV and PHP script - php

Just found this great article: http://www.blog.magepsycho.com/updating-product-qty-in-magento-in-an-easier-faster-way/ but something works not very well.
When I have a CSV with the columns "sku" and "is_in_stock", and I use the script, it also sets the "qty" to 0 when "is_in_stock" is 0. How is that possible? Because I only want to update "is_in_stock", and not the "qty".
Can you help me with this?
Thanks in advance!

The linked script ignores the header row and treats the second column as the quantity, regardless that you named it "is_in_stock". That is why "qty" is set to 0.
If all you need is a quick way to mark some items as unstocked:
Go to the admin, then Manage Products.
Search for those you want to change and put a checkmark next to them.
In the Actions box select Update Attributes then click Submit.
On the Inventory tab click the Change box for Stock Availability then select Out of Stock.
Click Save.

You could use this script instead, http://www.sonassi.com/knowledge-base/magento-kb/mass-update-stock-levels-in-magento-fast/
As a side note, if you have 5000 rows in your CSV, you should retain the previously uploaded file, then perform a diff on the rows that have actually changed - that way, you're only going to be modifying a much smaller number of products.

Related

Reorder pictures in a webgallery

I have a webgallery (made with laravel) and would like to add the possibility to reorder the images... Now, I have thought of several approaches but for every aproach i find that there should be a better way of doing it.
the gallery does not use javascript, so ones changes have been made it needs to be sumbitted and reloaded to reflect the changes
The main difficulties are:
how to store the order in the database? an additional Integer column?
how to add a picture "between" two others?
how to handle it at a frontend level?
So far the best ideas I had are these:
a column with integers, order by clause on this column. Frontend: a move up and a move down button.
problems of this solution: it needs a refresh after each single movement. it needs to identify the previous/next picture and swap the number with that one. To move a single pic from the end of the gallery to the top it takes forever.
a column with integers, automatically prefilled in steps of 100, order by this column + upload time in case of same numbers, Frontend: a textbox where the user can specify the integers for each picture and a submit button.
problems of this solution: does not look very professional. solves all the problems of the previous solution
same as previous solution but with double values to be able to insert pictures without limits.
They all dont seem the real deal.. Any sugestion on how to do it properly is welcome. thanks
I have done that kind of sorting in OpenCart products list (custom backend design)
Sort order was additional column order INT(11) in database
We had 3 input fields: up/down/custom
Where custom was dropdown of all indexes from 1 to max-items.
All inputs does the same:
Take new order value and shift all elements except itself. Up or down shift depends if you move element to front or to back of current position
UPDATE order FROM products SET order = :newOrder WHERE id = :currentItemId
if ($newOrder > $oldOrder)
UPDATE order FROM products SET order + 1 WHERE order >= :newOrder AND id != :currentItemId
else
UPDATE order FROM products SET order - 1 WHERE order <= :newOrder AND id != :currentItemId
Inserting does the same update, just first query becomes INSERT INTO
To get rid of ugly refresh of page on every action we do Ajax requests and re-sorted DOM with jQuery

PHP - parsing big data and keeping them up to date

I use PHP for solving this task. But that's not the point.
Every morning I get four files each of them contains nearly 6000-8000 records having the following form:
Product name
Package
Producer
Price with tax
Expire date
Rest
Series
Parsing these records I get the table of products. Later clients make orders so I need to keep id of item in the order table. (clients would like to see history of purchases)
All is fine. The arising problem is that one day any of suppliers can send completely different price list. I.e. some products will be removed and others will be added. So it would be completely wrong to rely upon they order in the price list.
What I've come to is to parse catalog blindly adding all items one time. Every next time when I get catalog I need to add only new items and remove the old one from DB. (though not actually removing but just marking as deleted so that no new purchases would be possible )
For deciding whether item is new or not I will retrieve record by record from Excel file and check "Product name", "package", "producer", "series" fields in conjunction against the table with products.
If no such item is found I assume that this is a new item and I'll add it to DB.
What to do with deleted items? I'm not warned about deletion of them. So I can't find out what items are missing in the new Excel file. The solution is to scan DB item by item and see whether all items in DB are present in Excel file. If some item is missing then I will mark it as deleted.
Once deleted items can be back for selling. So I will need to select all deleted items and check one by one against Excel file. If item appears in Excel file - I add it back.
It's worth to note that some suppliers give their catalogs as Excel files and others as DBF files as for now. Who knows what formats will come in the future. Also the number of suppliers is supposed to grow up (next month we get 2 more into play).
My question. Is there any better way to do it more efficient? I'm afraid that my method is too straightforward.
Having 8000 records and doing 3 checks I will get O^2 complexity for every price list making whole search through MySQL Db. Perhaps it will work for 8000 records but I'm sure it's going to fail when one day I get price list with let's say 10^5 record.
Is there a better way to organise it?
Thanks.

How to define when you should hide "Show More" button?

I have a mysql table of products.
I have a product listing page where User has a button "Show more" to load more products on the page.
After clicking on this button jQuery calculates how many displayed products are on the page (N) and makes an AJAX request to the table for getting 20 more products (N+20).
Now I should create a trigger when I should hide the button.
Which trigger I should choose? What is the best way to define that there are no products.
Please, help.
I have no idea how have you reached that functionality since you are not sharing the code and thus I cannot give you the most accurate help, only this one:
when loading the page and retrieving the first X rows from DB, retrieve the amount of all rows (matching the search criteria) as well
make this count visible to the template
in jQuery, count how many rows have you already loaded
this means, that on page start you are displaying X rows, the amount of total rows is Y, and the number of currently displayed rows is Z
immediately after the page is loaded you need to compare if Z <= Y then do not display button else display the button
if the button was displayed I guess you are listening to click event on this button and then performing your AJAX request, here after the AJAX is successfully completed, you need to do Z += X and again compare if Z > Y then hide the button
That should be your logic, now turn it into your code.
There are 2 ways I guess,
The simple way would be counting number of response rows that's returned by your AJAX function. If it's smaller than 20 then hide the button.. However, if your records number is a multiple of 20 then there will be a glitch in the last response, because it won't hide the button.
The perfect way is to calculate number of records in your table, then save that data in your js variable. Then compare that variable value with the number of times you ask for another record.. If they have same value then hide the button, if not then show the button.
It depends on the frequency of product list updating.
If the amount of products changes often (say, more than once a day or so), I would execute a query on each AJAX request, checking if I have more items (for example, checking if N+20 >= count(id) of products), and on the callback hide the button.
Otherwise, I would just inject the amount of products as a JS parameter into the page, validate against it on each click (N+20 <= count_products), and save myself some AJAX \ SQL loading time.
you can get the count of all lines using PHP and save it in an hidden field or in a variable after that you can test (using jquery) to see the current number of records in the page (you are already doing that ) then test it with the number in the hidden field/variable and show/hide your button
Finally I choose next variant:
I want to load more 20 products.
In backend code I switch this number to 21.
So then in jQuery I show just 20 BUT I check if there one more product I should NOT hide the button.
That's it.
Thanks guys anyway!

Combining multiple rows or results with the same title to form drop down menus with PHP and MySQL

So I am picking up a project that was quit halfway through by the last guy so that I could get some more practice with PHP and databases. I have run into a problem, and I am sure it is common enough that there is a standard solution, but I am unable to find one.
The db I am working with has 4,600, so reorganizing is out of the question. It is a db of liquers for a wholesaler. Here is what the results page looks like currently:
What I am trying to set it up so the results are returned in list form, with only one title and dropdown menus for the different sizes/prices of products that looks like this:
The problem is that there are multiple entries in the db for each product. In this example there are 3, while some have 1, and some have 2.
I am really not sure how to go about this, and any help would be greatly appreciated. Thank you.
I'm not sure about the PHP syntax, but pseudocode here's what you could do:
allProductsReturnedFromMySQL = QueryYourDatabaseForAllProducts()
Hashtable[productId, List[productSizes]] dropDownsByProduct;
Hashtable[productId, commonProductInformation] uniqueProducts;
foreach (product in allProductsReturnedFromMySQL) {
if product.productId not in uniqueProducts
then add it with the product information that does not vary
if product.productId not in dropDownsByProduct
then add it with an empty list
append the size of this product to the corresponding list in dropDownsByProduct
}
After that little bit of logic you'll have all your unique products with the common properties for each one, and a way to fetch the corresponding sizes drop down. If you wanted to do this purely in SQL to minimize the data that's transferred, you could do something like this:
-- this would get you your products
select distinct id, property1, property2 from product
-- this would get you your drop downs by product
select id, size from product order by id
You can then build the same drop down hashtable by iterating through the second result set.
I'm not sure if this is the best way, but I've always approached this by altering the query so that it is sorted by product name. Then as you iterate through the rows, check to see if the product name matches the one you just processed. If it's the same, then this row is a different size of the same project.

PHP> Sort query results by name while letting each letter be on the top sometimes

I'm currently working on a site that will display a list of online shops,
Each shop will be stored on my database and I'll be using PHP to select and display them.
But since those shops will pay me, I want to let each shop to be on the top of the list sometimes,
(for example if the shop name starts with a "Z", they will probably complain for being on the bottom of the list all the time, so I want to keep it fair).
So I thought about letting each letter be on the top of the list for an hour, but i have no idea how to do that..
Is that even possible?
Thanks in advance!
I'd show a separate box and call it "today's pick" or something with just one shop in it. That way you can push the shops starting with "Z" to the top once in a while and at the same time keep the user experience of a list of shops which is sorted normally.
Then use the database to save which shop has been in the "today's pick"-box how many times to get them all up there equally.
There's no sane way (that I'm aware of) to handle this directly in SQL without adding a "priority" field to your schema (although it's possible, it would be convoluted at best). That said, here are two suggestions:
Modify your schema
Simply add a "priority" field to the relevant schema and sort by priority, name (or whatever the default is). You will of course need to reset the priority field every hour, but this is a fairly trivial task.
Handle it in PHP
Carry out the query as per usual.
Grab all the results into an array.
Re-prioritise as required based on the current hour. (You'll need to array_splice the item(s) you want to bump out of the array and then array_unshift them to the top.)
Output based on the array.
This will of course become more convoluted/less efficient if you need to handle pagination, but the basic idea is the same.
A nice solution would be to add another column to the database with the shop names, and call it something like "last_shown" then when you show this shop, update the column with a timestamp, and each select do something like:
"SELECT name,link FROM shops ORDER BY last_shown DESC"
then in php you could check
<?php
if($row['last_shown']+3600 > now()){
//run select but in ASC order
//update the new row's column to the current timestamp
}
?>
that way it will only update once an hour, but otherwise it will keep selecting the shop at the top of the list for the hour
sorry it's a bit of a mess i just typed this out quickly at work
You can add
1) a extra column as shown_times in schema
2) order by shown_times asc
3) & as a shop is shown you would +1
or
Another solution :
You can even use ORDER BY RAND()

Categories