Hello Drupal Community,
I am still learning AJAX, I know enough PHP to get myself in trouble, and I have a basic question.
I searched around and can't find what I'm looking for partly because I'm struggling of exactly what to search for to find the answer.
In particular, I have an online store (powered by Ubercart) where selecting different types of a product's configuration, such as a red shirt or a blue shirt, alters the price that the user sees displayed on the website automatically via (I believe) AJAX.
To simplify my question, please assume there is a PHP array as follows:
$option['color']
$option['price']
Where the color and price are the values that appear on the end-user's browser when the page is loaded, for instance:
"Red shirt for $10", where $option['color'] and $option['price'] equal "Red" and "10" respectively and by default with each page load, from a form with radio buttons for the option to select a Red shirt or a Blue shirt.
THE ISSUE: If I simply include my typical troubleshooting of "print_r($option)" in my page.tpl.php, I'll get the original default array for the "Red" and "10", but of course this doesn't change when the user selects the radio button for Blue where the text & price displayed on the webpage change to, for instance, "Blue shirt for $15".
I understand why this is so, therefore,
MY QUESTION: What can I do to trouble shoot PHP variables that are able to be altered by the user on their browser? How can I get these new values outputted in real time for my troubleshooting purposes?
I did some searching and found advice to use Firebug, which my Firefox has, but I've looked through the Console, Script, and DOM tabs, and using the search filter nothing comes up when I enter the variable name I'm working on.
Additional information, if this helps, is that the function of the module that automatically updates what the end-user sees has the code placed inside a module_name_form_alter(&$form, $form_state, $form_id) form-alter function. Is there some way to be able to print out what the PHP variables of the altered form have become after each interaction of the website?
Thank you very much for your help and sorry if this is long winded. I'm trying to provide as much info as I can. I am posting this in the Community Forum instead of in an individual module issue tracker because I've wanted to do this for not only different modules but for Drupal core as well. Knowing how to do this will greatly help me out in all areas of my Drupal experience.
Regards,
BigMike
Comments
What sort of issue are you
What sort of issue are you trying to trouble shoot?
Specifically I am having an
Specifically I am having an issue between stock levels and the UC Stock Notify module, however everything has been functioning perfectly fine for a few years now and works everywhere else except for one particular node's chosen product attribute, such as selecting the radio button for a Blue shirt.
Flowchart of the process:
- User clicks on the Radio button for a "Blue shirt"
- First UC Stock Notify jumps in to check the inventory level of the product selected.
-- If the stock level is greater than zero, then UC Stock Notify doesn't alter anything and the standard "Add to Cart" button appears.
-- If the stock level is zero or less than zero, then UC Stock Notify jumps in and alters the form by removing the "Add to Cart" button and inserting a new button that I've themed to read "Notify when back in stock".
Proper Behavior:
- Clicking a "Add to Cart" button uses AJAX (via the "Ubercart AJAX Cart" module) to seamlessly add the product to the user's cart and display a nice pop-up message that the product was added to their cart -- "Blue Shirt was added to your cart".
- Clicking a "Notify when back in stock" button reloads the page with a default message atop the page -- "Thank you for your interest in this product. We will notify you when it is available again" -- and a) adds their UID to a notification list for the specific product attribute configuration (ie. which radio button was selected), or b) provides a box for guests to enter their email to be notified when the product is back in stock.
What is happening instead:
I have a particular product attribute configured as in stock, ie Blue shirts are in stock. When the radio button to select the Blue shirt is clicked, the usual "Add to Cart" button appears. BUT, when the "Add to Cart" button is clicked, something very strange is happening:
- A nice pop-up message appears BUT the out-of-stock message of "Thank you for your interest in this product....." is somehow appearing in the pop-up instead of the "Blue shirt was added to your cart"
- Then the pop-up disappears and nothing else happens. The page does not reload, the user is not added to the product's notification list in the database, nothing is added to his or her shopping cart, just nothing happens.
It's exactly a combination of the two independent features: You get the nice pop-up as if we were adding the product to the cart but then you get the "Thank you for your interest..." message that isn't supposed to appear until the page reloads and the user has been added to a notification list.
I've experienced issues with stock where we accidentally have the same product number or SKU listed on two different nodes, where one node has the SKU configured as quantity "0" stock and the other as some positive number, which results in both nodes being able to add the product to the shopping cart despite one of them being out of stock. So I've searched the database a dozen times and have verified its not a SKU issue, I've even changed the SKU just to test this and so far no dice.
It is working perfectly fine on all other nodes that I have tested and in more than probably two years this is the first time I've ever had any issue with the UC Stock Notify module and I've not made any recent modifications that I can think of to cause this. It just seems so random I'm just about ready to delete the node and just copy everything over to a new node.
In the end here is what I am trying to trouble shoot so I can learn what is going on instead of ignoring it.
Here is the full function, but I don't think it's necessary to read it all so scroll down after this for a truncated version to save your time:
Here is a truncated version that I'm following in my mind to help me figure out what is happening:
My Exact Request: I am trying to troubleshoot the $model PHP variable. This changes each time at the end-user when a different radio button is clicked.
I'd like to know if $model is indeed the exact specific product attribute of the exact SKU that I've entered as out of stock, or is $model for some reason not being updated and is only using the default variables ('red' and '10').
Here are the two functions in question:
and
My contention is that if the $result is coming up null, then just the default stuff from the node is being filled in for $model, which isn't gonna work when my node has multiple attributes - or product adjustments - in use.
Thoughts?
BigMike
Using FirePHP and Devel's dfb
Using FirePHP and Devel's dfb function has always worked well for me. Instructions here may help you.
Oh wow this looks very
Oh wow this looks very promising, thank you! I will check this out and report back-!
Mike
Ok I just found a quick way
Ok I just found a quick way to troubleshoot this!!
If I simply insert a t-string, drupal_set_message(t()), in the function that is being ran each time someone clicks on either my 'Add to Cart' or my 'Notify when in stock' buttons, the t-string appears in the popup right in the browser.
Also, I found I don't have to reload the page each time, since its using AJAX (I think), if I make a change to the t-string and then click Add to Cart, the message appears along with any other regular messages in the pop-up.
:-)
I discovered that the variable $model [using drupal_set_message(t($model))] (which is the part number) is not being updated each time someone chooses a different product configuration. It's a great start, and now I am tracing the issue up the code to see what's going on!
BigMike
Ok, I got this to work
Ok, I got this to work through debugging using drupal_set_message.
I traced the issue to not being able to enter this function:
So I used,
To see what I was working with and the $form_state array is actually structured as:
Interestingly, on other pages it does use the [post] key, and thus is working. So for some reason on this node and this node only (so far it's the only one known to not be working), it uses a [values] key instead of a [post] key, which the module is looking for.
Everything seems to be functioning everywhere else so I inserted code for this case as shown in the 2nd IF statement here:
Not sure why in the world this node is different. Yes of course it's the same node type as the rest.
But for now it is working.
Question: Does anyone think this is a security issue? Why would I have a $form_state['post'] present on some nodes and a $form_state['values'] present on another?
Just in case this is a security issue, I've added an additional check for the NID so that my "TEMPORARY FIX" IF statement will only work on the specific node that is giving us problems.
Regards,
BigMike
$form_state['values'] would
$form_state['values'] would be the proper variable to use.
Interesting. Thank you for
Interesting. Thank you for the vote of confidence. I'm gonna troubleshoot this a bit more and see why the module uses ['post'], and possibly also report it to the module developers.
Thank you to everyone's help and have a great day!
Mike
Bug report posted to Ubercart
Bug report posted to Ubercart Stock Notify, https://drupal.org/node/2043585
Thanks
Mike