Admin Stock Status Change OnClick with AJAX
I'm wanting to practice/learn AJAX by making the stock status of a product change OnClick w/o a page reload in the admin. Any pointers?
I installed Add to Cart w/ AJAX to get a glimpse at it, but I also had one page check-out installed and felt a bit lost in the thousands of lines of code haha.
Disclaimer: I might be a little early in asking advice on this, because I haven't tried much of anything yet.
Should I add a separate custom admin/changestock.php or just try to squeeze it into admin/categories.php? Any security issues that might make this harder to learn with?
Thanks,
b
Re: Admin Stock Status Change OnClick with AJAX
To maybe address the last first, generally it takes more than a single file to incorporate ajax related code at least to maintain the structure generally expected. There's the javascript/jquery to "collect" the information, then there's a file that is accessed based on the ajax call which then digs into the php side with the result provided back to the javascript/jquery to be processed.
There is always some amount of security items to be considered or concerned about; however, by starting in your admin area there is a little bit less concern because the file(s) are not directly accessible from outside the admin folder.
As to other examples that perhaps are less involved, there's Dynamic Price Updater (https://www.zen-cart.com/downloads.php?do=file&id=1301) that uses ajax style calls (haven't received authoritative feedback of the quality of its use), and there is a little more convoluted/involved is product attribute image replaces main product image: https://www.zen-cart.com/downloads.php?do=file&id=699.
As to the overall concept, if understand correctly you want to be able to click on the quantity, modify it and then when you move away from it you want that to be the value seen by customers. For what it's worth I think there is already a plugin that does that or similar I think called quick status update, though I think it still uses/expects a "save" button to be pressed to cause the update action. My concern with such a "quick" update by moving away from the text block is that the number could be accidentally saved with whatever was entered which could be grossly large or small. Too large and someone could overpurchase, too small (0?) and a product will appear as out-of-stock. Having a sort of "save" button at least forces some level of thought in the modification. The other part of all that is how the system responds just at the verge of the session ending...
Re: Admin Stock Status Change OnClick with AJAX
Quote:
Originally Posted by
mc12345678
To maybe address the last first, generally it takes more than a single file to incorporate ajax related code at least to maintain the structure generally expected. There's the javascript/jquery to "collect" the information, then there's a file that is accessed based on the ajax call which then digs into the php side with the result provided back to the javascript/jquery to be processed.
There is always some amount of security items to be considered or concerned about; however, by starting in your admin area there is a little bit less concern because the file(s) are not directly accessible from outside the admin folder.
As to other examples that perhaps are less involved, there's Dynamic Price Updater (
https://www.zen-cart.com/downloads.php?do=file&id=1301) that uses ajax style calls (haven't received authoritative feedback of the quality of its use), and there is a little more convoluted/involved is product attribute image replaces main product image:
https://www.zen-cart.com/downloads.php?do=file&id=699.
As to the overall concept, if understand correctly you want to be able to click on the quantity, modify it and then when you move away from it you want that to be the value seen by customers. For what it's worth I think there is already a plugin that does that or similar I think called quick status update, though I think it still uses/expects a "save" button to be pressed to cause the update action. My concern with such a "quick" update by moving away from the text block is that the number could be accidentally saved with whatever was entered which could be grossly large or small. Too large and someone could overpurchase, too small (0?) and a product will appear as out-of-stock. Having a sort of "save" button at least forces some level of thought in the modification. The other part of all that is how the system responds just at the verge of the session ending...
Thanks for all the info, I'm mostly interested in just changing the status from the "in-stock" (or 1) to "out-of-stock" (to 0). Which is represented in the admin as a green check or red x. I suppose changing the quantity would be interesting to do too, but I tend to not change that value w/o creating a new product.
Re: Admin Stock Status Change OnClick with AJAX
Quote:
Originally Posted by
wolfderby
Thanks for all the info, I'm mostly interested in just changing the status from the "in-stock" (or 1) to "out-of-stock" (to 0). Which is represented in the admin as a green check or red x. I suppose changing the quantity would be interesting to do too, but I tend to not change that value w/o creating a new product.
That *does* make more sense now. I was wondering how just the onclick event alone would support stock level modifications and also would be an improvement for the admin screen. One thing that I've seen in using ajax style calls in the admin though and I haven't gone chasing it relates to meeting/exceeding the timeout at the point the "button" is clicked and how the system responds. Ie. In one specific application whether written properly or not was that the "data area" turned into a login screen that then returned back to the work area causing a work area within a work area instead of refreshing the screen to be one "logical" view instead of one within another... not likely a particular result expected to be seen, but the same "thought" would exist, did the "value" really get updated or what should happen at that point?
Re: Admin Stock Status Change OnClick with AJAX
Quote:
Originally Posted by
mc12345678
There is always some amount of security items to be considered or concerned about; however, by starting in your admin area there is a little bit less concern because the file(s) are not directly accessible from outside the admin folder.
only true if the first lines of code are something like:
PHP Code:
<?php
require('includes/application_top.php');
i have the turning on and off of items available from the categories.php page. not sure if that is a base ZC function or something from a plugin or something i added. in addition, i also created a separate inventory page which allows you turn off an item and/or update the inventory value for a group of items.
none of these, however, were done using ajax.
i suppose if you are having page load time issues on the admin side, i would investigate where the holdup might be. if on the other hand, you are doing this project to better familiarize yourself with ajax calls and how they work, i am all for it!
good luck!
Re: Admin Stock Status Change OnClick with AJAX
I saw a second or third reason to support something like this besides the educational value.
If the number of items or height or whatever would cause the screen position to change by clicking on the default "button" or it is desired to perform the action on multiple items (likely affected by the above) then an in place operation would be more desirable and provide an improved work flow.
Simple things can make it better and I'm pretty sure that the dev team would consider the result for further incorporation.
Re: Admin Stock Status Change OnClick with AJAX
Quote:
Originally Posted by
mc12345678
I saw a second or third reason to support something like this besides the educational value.
If the number of items or height or whatever would cause the screen position to change by clicking on the default "button" or it is desired to perform the action on multiple items (likely affected by the above) then an in place operation would be more desirable and provide an improved work flow.
Simple things can make it better and I'm pretty sure that the dev team would consider the result for further incorporation.
Yeah I think it could/would be a good improvement to core functionality. It is definitely just something small that I'm hoping to work on just for the educational value added by using ajax.
Re: Admin Stock Status Change OnClick with AJAX
So I finally figured this out, I just need to find time to clean up my results before I can share them, will probably double check changes against a fresh install first, then share.