Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    69
    Plugin Contributions
    0

    Default How come I can't modify $_POST variables in an observer

    Hello,

    I have a gift product that allows the user to enter a gift recipient address. I am writing a piece of code that sends the address to the USPS and replaces it with a scrubbed address. Technically, what I do when I get the new address is update the $_POST['id']['txt_XX'] variables.

    Currently I have the code in includes/modules/pages/product_info/header.php and it works just fine. HOWEVER, when I try to move the code to the update function in an observer, it ceases to function in that any $_POST variable that I update fails to be passed to the cart.

    Can anybody explain why modifying $_POST variables in an observer doesn't work?

    Thank you,

    Kevin



    http://www.yesmagazine.org/store/ind...products_id=16

  2. #2
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: How come I can't modify $_POST variables in an observer

    Are you perhaps hooking your observer at a place where the POST variable contents have already been read and processed? ie: too late in the process?
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    Jul 2007
    Posts
    69
    Plugin Contributions
    0

    Default Re: How come I can't modify $_POST variables in an observer

    My observer is responding to NOTIFY_HEADER_END_PRODUCT_INFO. Definitely before it's being acted on.

    I found this post at php.net: http://us.php.net/manual/en/language...ined.php#74620

    The guy says "Dealing with "superglobals" and functions is not as straightforward as it may seem when you're doing manipulations. (follow the link for the rest of his post). "

    He points out that superglobals can't necessarily be changed in functions, but doesn't say why or what to do about it. I was hoping somebody here might have some insight. Surely ZenCart users have attempted to modify $_POST variables in an observer before?

    Kevin

  4. #4
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: How come I can't modify $_POST variables in an observer

    What's in your observer class?
    Are you sure it's firing?
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  5. #5
    Join Date
    Jul 2007
    Posts
    69
    Plugin Contributions
    0

    Default Re: How come I can't modify $_POST variables in an observer

    Yes, it's definitely firing. What happens is I take a set of address fields, send them to the post office, and replace the $_POST variables with the response. Any $_POST variable that I touch comes up blank in the shopping cart. For testing purpose, I commented out code so that only some of the address fields are updated while others are left untouched. The ones I don't touch come through fine. The ones I touch come through blank.

    Here's the relevant part of the code at the end of the notifier:

    function update(&$class, $eventID, $paramArray) {

    //
    // Code omitted that sends address to USPS
    //

    //Update POST variables and call cart add function (will redirect to cart)
    $_POST['id'][txt_7] = $Address;
    //$_POST['id'][txt_8] = $Address2;
    $_POST['id'][txt_9] = $City;
    //$_POST['id'][txt_10] = $State;
    $_POST['id'][txt_11] = $Zip;

    global $goto, $parameters;
    $_SESSION['cart']->actionAddProduct($goto, $parameters);

    }

    As you can see, address2 and state are commented out and they will show up in the shopping cart. Here's the relevant product if you want to see for yourself:

    http://www.yesmagazine.org/store/ind...products_id=70


    For comparison, this other product does not trigger the notifier, instead it runs the same code placed in includes/modules/pages/product_info/header.php:

    http://www.yesmagazine.org/store/ind...products_id=16

    All the $_POST address variables can be modified just fine in this case.

    Kevin

  6. #6
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: How come I can't modify $_POST variables in an observer

    It doesn't make sense to me why you're doing any of this in /includes/modules/pages/product_info/header_php.php

    When someone adds a product to their cart, under normal circumstances, they are taken to the shopping cart page ... which means that they *leave* the product_info page, meaning that /includes/modules/pages/product_info/header_php.php never fires.

    And, if you've changed your cart to not go to the shopping cart page during add-to-cart, then even if you've got it changed to stay on the product_info page, that page's header_php.php is still the wrong place to be handling the submitted data.

    I'm inclined to suggest that you put your add-to-cart validation code into a new file located at: /includes/extra_cart_actions/my_giftsub_address_validation.php

    And in there, put all your validation code.

    You can follow the programming logic in /includes/main_cart_actions.php (don't edit this file.) You'll see that the extra-cart-actions scripts can pre-process the data, and then when they're finished, the regular add-to-cart operations will happen normally.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  7. #7
    Join Date
    Jul 2007
    Posts
    69
    Plugin Contributions
    0

    Default Re: How come I can't modify $_POST variables in an observer

    Good point. I had reworked the form to submit back to the product page out of ignorance of another way to do it. I'll look into reworking it to be more ZenCart standard and see if it helps.

    Thanks for your input Dr. Byte!

    Kevin

 

 

Similar Threads

  1. setting $_SESSION variables in an Observer Class
    By tcarden in forum Contribution-Writing Guidelines
    Replies: 1
    Last Post: 13 Feb 2013, 06:37 AM
  2. v139h what is $_POST['x'] and $_POST['y'] in checkout_confirmation?
    By linjuming in forum General Questions
    Replies: 0
    Last Post: 21 Jul 2012, 04:46 AM
  3. Reading $_Post variables from checkout_shipping page?
    By pcaliban in forum General Questions
    Replies: 8
    Last Post: 3 Nov 2010, 07:21 PM
  4. How can we record where the orders come from?
    By anewstone in forum General Questions
    Replies: 1
    Last Post: 19 Feb 2009, 10:30 AM
  5. Observer to modify price....Step 3 shows old price?
    By fishkorp in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 15 Dec 2006, 02:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR