close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Combining an onchange event, for product lookup, with inventory control?

Thread began 5/29/2014 8:49 am by Nathon Jones Web Design | Last modified 6/06/2014 9:30 am by Jason Byrnes | 4608 views | 33 replies |

Nathon Jones Web Design

Combining an onchange event, for product lookup, with inventory control?

We have an onchange event on our product detail (add to cart) page which is linked to a separate ajax file, pricelookup.php. The onchange event allows users to make selections from drop-down menus for each product (colour, etc) and then updates the product code and price on the page accordingly.

Initially I wanted to hide product variables from the drop-down menus, should that product be marked as hidden from the website in my clients admin however, with help from Ray and yourself Jason, we changed this to display a warning message and to hide the Add to Cart button when the corresponding unavailable selection was made by the customer. Great!

However, products that were marked as hidden were still being able to be selected so I added "AND LEDdelete = 2" (2 = No) to the SELECT statement on the pricelookup.php page and, would you believe it, it worked! (a first for me on this project)

So, now, I'm faced with trying to do the same thing but have it respond to the inventory listed for each product which is where I'm stuck.

Following the inventory control tutorials I've got the following code sorted out, and it's working correctly in my clients admin area.

LEDstock - (SELECT Coalesce(SUM(LEDDetailQuantity),0) FROM LEDorder INNER JOIN LEDdetail ON LEDdetail.LEDDetailOrderID = LEDorder.LEDOrderID WHERE LEDOrderDate > LEDProductUpdateDate AND LEDDetailLEDid = LEDid) AS RemainingInventory

My question...

Can I add the above code to the recordset on the pricelookup.php ajax file and, from there, how do I change the onchange event so that it also detects when stock level (RemainingInventory) is listed as 0?

If that's not the correct approach I'd appreciate guidance on how best to acheive this.
Thank you
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

That may work, I cant really say for certain as the lookup ray created is custom coding and is not supported through the form.

looking at your page through Fire Bug, it looks like the price lookup ajax function returns a string separated by a semi colon, see the attached screen shot:
5.60:LWP0038

looks like the first part is the price, and the second part is the item number. after adding that part of the SQL to the query, you would also need to edit the code that creates the Colon separated string to add the remaining inventory.


in the updatePriceDisplay function, the colon sperated string is broken to an array using:

var priceInfo = (silent_form[1]).split(":");


the price is returned through the 0 index of the priceInfo array:
priceInfo[0]

example:
if(priceInfo[0] !="") { document.getElementById("productPrice").innerHTML = "£" + priceInfo[0] + " inc. vat"; }

the item number through the 1 index:
priceInfo[1]

example:
if(priceInfo[1] !="") { document.getElementById("productCode").innerHTML = priceInfo[1]; }

by adding the remaining Invetory to the string, you would reference it using the 2 index:
priceInfo[2]

you could do something like add a hidden form element to the page:

<input type="hidden" name="remaingQuantity" id="remaingQuantity" value="0" />

the add code in the updatePriceDisplay to set the value:

document.getElementById("productCode").value = priceInfo[2];

you also asked this similar question in another thread. In the add to cart button behavior, you can bind the remaining inventory cart column to the remaining inventory hidden element for the calculation that I suggested to work

Where this is custom coding, it is not supported through the forums/

All i can really do is offer suggestions, and examples of the code to use, Note, the examples may need further tweaking, as I can only really give theory here. If you are not able to get this to work on your, you will nee to go through the premiere support service.

Sign in to reply to this post

Nathon Jones Web Design

In the pricelookup.php ajax file I added this to the rsAddtoCartLookup recordset:

LEDstock - (SELECT Coalesce(SUM(LEDDetailQuantity),0) FROM LEDorder INNER JOIN LEDdetail ON LEDdetail.LEDDetailOrderID = LEDorder.LEDOrderID WHERE LEDOrderDate > LEDProductUpdateDate AND LEDDetailLEDid = LEDid) AS RemainingInventory

I then edited the code that creates the colon separate string, on the same page, from this:
<?php echo($row_rsAddToCartLookup['LEDprice']); ?>:<?php echo($row_rsAddToCartLookup['LEDproductcode']); ?>

...to this:
<?php echo($row_rsAddToCartLookup['LEDprice']); ?>:<?php echo($row_rsAddToCartLookup['LEDproductcode']); ?>:<?php echo($row_rsAddToCartLookup['RemainingInventory']); ?>

Checking with Firebug the price lookup ajax function now returns:
6.00:LWP0002:1997

...and this changes as I change selections in the drop-down menus so I'm confident that the price lookup ajax function is working correctly in that it is returning the correct product code, price and corresponding inventory.

So...in order to utilise this as part of the inventory control I've been trying to implement (ahem) I now need to capture/display the inventory value so that I can, for example, use it to hide the Add to Cart button if the value is 0.

I also need to prevent a user from entering a value in the quantity field, on both the Add to Cart and View Cart pages, that is higher than the available inventory.

Capturing/Displaying the inventory value returned by the price lookup ajax function
Is this where I need to change the updatePriceDisplay function? I've referenced the 2 index (naming it productStock) in the updatePriceDisplay function like so...

<script>
function updatePrice() {
silent_submit(document.getElementById("LEDCart_1_ATC"),updatePriceDisplay,"pricelookup.php",false);
}

function updatePriceDisplay() {
var colorVal = "NA";
var dimmerVal = "NA";
var sensorVal = "NA";
var priceInfo = (silent_form[1]).split(":");
if(priceInfo[1] !="") { document.getElementById("productCode").innerHTML = priceInfo[1]; }
if(priceInfo[0] !="") { document.getElementById("productPrice").innerHTML = "£" + priceInfo[0] + " inc. vat"; }
if(priceInfo[2] !="") { document.getElementById("productStock").innerHTML = priceInfo[2]; }

if (document.getElementById("LEDCart_1_Colour_Add").tagName == "SELECT") colorVal = document.getElementById("LEDCart_1_Colour_Add").options[document.getElementById("LEDCart_1_Colour_Add").selectedIndex].value;
if (document.getElementById("LEDCart_1_Dimmable_Add").tagName == "SELECT") dimmerVal = document.getElementById("LEDCart_1_Dimmable_Add").options[document.getElementById("LEDCart_1_Dimmable_Add").selectedIndex].value;
if (document.getElementById("LEDCart_1_Sensor_Add").tagName == "SELECT") sensorVal = document.getElementById("LEDCart_1_Sensor_Add").options[document.getElementById("LEDCart_1_Sensor_Add").selectedIndex].value;

if(priceInfo[0] == "") {
if (colorVal == "" || dimmerVal == "" || sensorVal == "") {
//document.getElementById("LEDCart_1_ATCButton").style.display = "none"
} else {
document.getElementById("productPrice").innerHTML = "<span class='red'>Currently Unavailable</span>";
document.getElementById("LEDCart_1_ATCButton").style.display = "none"
}
} else {
document.getElementById("LEDCart_1_ATCButton").style.display = "inline"
}
}
</script>

...but I'm not clear how I then display this on the Add to Cart page. Looking at how the other elements are displayed on the page, productCode for example, it uses an id like so...

<span id="productCode"><?php echo $row_rsPROD['LEDproductcode']; ?></span>

I would appreciate your help in understanding the theory here.

Preventing a user entering a value in the quantity fields that is higher than the available inventory
You have described adding a hidden form element to the page as follows:
<input type="hidden" name="remaingQuantity" id="remaingQuantity" value="0" />

Once I've established how to capture and display the inventory value that's returned by the price lookup ajax function, should I then use that value in the hidden form element...
<input type="hidden" name="remainingQuantity" id="remainingQuantity" value="<inventory value from price lookup ajax function>" />

This form element is what is binded to the AvailableQuantity column I've created in the eCart object and you'd then said to create a calculation as follows (this was in the "arguing" thread - http://www.webassist.com/forums/posts.php?id=34517) but I was wondering if that was a rogue " at the end?

abs([Quantity] > [AvailableQuantity])?[Quantity] = [AvailableQuantity]:"

Can I ask you to explain what the calculation above is doing though? What is abs, for example?

Thank you for your support and assistance.
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

looks like you added this line to set the product stock

if(priceInfo[2] !="") { document.getElementById("productStock").innerHTML = priceInfo[2]; }

That would work if you had a span with the ID productStock
<span id="productStock"></span>

using the hidden element:
<input type="hidden" name="remaingQuantity" id="remaingQuantity" value="0" />

to set it's value, use:
if(priceInfo[2] !="") { document.getElementById("remaingQuantity").value = priceInfo[2]; }



in the calcualtion:
abs([Quantity] > [AvailableQuantity])?[Quantity] = [AvailableQuantity]:"

abs() is a ternary expresion. Basicly it is a short hand if statement, in the form of:

abs(Question)?true:false


so, the question is:
[Quantity] > [AvailableQuantity] <- is the quatity greater than the available quatity?

if true:
[Quantity] = [AvailableQuantity] <- set Quantity to be the same as available quantity

if false:
'' <- do nothing.

Sign in to reply to this post

Nathon Jones Web Design

Thank you Jason. I have the correct stock level appearing in the hidden form element, depending on selections made by the user from the variable drop down menus.

In the calculation:
abs([Quantity] > [AvailableQuantity])?[Quantity] = [AvailableQuantity]:"

...should the colon and single quote be at the end? I'm getting a "parse error" with that. I looked at the LEDCart_PHP.php file and tried to copy the theory from the other calculations listed but without success (parse errors).

Can you clarify exactly what that needs to be?
Thank you.
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

yes, the colon and single quotes are needed at the end.

What is the full error message?

and can you send a copy of the LEDCart_PHP.php page that is generating the error.

Sign in to reply to this post

Nathon Jones Web Design

Parse error: syntax error, unexpected ';' in /home/led2014/public_html/WA_eCart/LEDCart_PHP.php on line 13

Line 13 is...
$itmObj->Inventory = abs($itmObj->Quantity > $itmObj->AvailableQuantity)?$itmObj->Quantity = $itmObj->AvailableQuantity:;//i

LEDCart_PHP.php attached.

Thank you.
NJ

PS. I did try removing the ; but got similar parse errors. Also, that should be a lower case I at the end but for some reason, on here, it is converting to a capital!

Sign in to reply to this post

Jason ByrnesWebAssist

line 13 is missing the 2 single quotes at the end:

$itmObj->Inventory = abs($itmObj->Quantity > $itmObj->AvailableQuantity)?$itmObj->Quantity = $itmObj->AvailableQuantity:'';//I

Sign in to reply to this post

Nathon Jones Web Design

I pasted it into the eCart Object exactly as you'd posted it...weird. :(

When I add the single quote:
$itmObj->Inventory = abs($itmObj->Quantity > $itmObj->AvailableQuantity)?$itmObj->Quantity = $itmObj->AvailableQuantity:";//I

I'm getting syntax errors all over the place in the rest of the LEDCart_PHP.php file.

Sign in to reply to this post

Nathon Jones Web Design

Sorry, single quotes, not a single double quote...hard to tell. I had to add that at the end of the LEDCart_PHP.php page too. Line 434.

This doesn't prevent a user adding the item to cart though?

Is it possible to remove the add to cart button if the stock level calculation is zero or if the price lookup ajax function returns zero?

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...