Custom Shipping Add-On

General ShopSite user discussion

Custom Shipping Add-On

Postby c.wagner » Tue Nov 10, 2020 3:01 pm

I am starting to write a custom shipping add-on in PHP. I started with a simple file to see if I could make sure ShopSite was connecting to the add-on. My code looks like:

Code: Select all
echo "status=pass" . "\n";
   echo "option_count=1" . "\n";
   echo "s1option=LTL Freight Shipping" . "\n";
   echo "s1price=300.00";


This works fine, and I was able to connect ShopSite to my add-on. When checking out on the site, I was shown a drop down menu to show LTL Freight Shipping with a $300 cost. Now obviously this add on doesn't do anything. Next step is to get the information ShopSite is passing to the add-on. I don't seam to be able to get anything. After several hours of guessing, I figured I would just dump the entire contents of the post data and see what is there. I just added the line
Code: Select all
file_put_contents("post.txt", "3" . print_r($_REQUEST, true));
to my code. After reviewing the log file all I found was an empty array. Turns out ShopSite is not sending any data to my add-on at all. I figure this has to be some kind of configuration error on the ShopSite end. Any ideas where to look?

Thanks
c.wagner
 
Posts: 17
Joined: Fri May 28, 2010 9:23 am
Location: Norfolk, VA

Re: Custom Shipping Add-On

Postby loren_d_c » Tue Nov 10, 2020 3:24 pm

Are you connecting to your custom script via a URL, or by executing a local file? With the local file option, the parameters coming from ShopSite aren't being POST'ed (that's an HTTP thing), it's passed as command line parameters. Search for 'php order api' or 'php custom shipping' in this forum to see examples of parsing the parameters in PHP.

-Loren
loren_d_c
 
Posts: 2569
Joined: Fri Aug 04, 2006 12:02 pm
Location: Anywhere

Re: Custom Shipping Add-On

Postby c.wagner » Wed Nov 11, 2020 7:56 am

I am connecting to my custom script via URL. It is technically a local file, but it is not located in the CGI-BIN directory. The script is located in a subdomain on my server. My server uses CGI-wrap which would require additional set up for me to execute PHP files from within the CGI directory. (Something I know is possible, but I am not familiar with.)

I did take your advise and searched the forum and found the suggestion to get the variables from stdin. I tried that and am still getting no data from ShopSite.

Here is the code I used to try to grab the post data using stdin. It is basically copied directly from another post on this forum.
Code: Select all
// Read in POST parameters sent by ShopSite
   $stdin = fopen("php://stdin","r");
   fscanf($stdin,"%{$SERVER['CONTENT_LENGTH']}s",$query_string);

   // And now we split the params into the $params array.
   parse_str($query_string,$params);
   
   file_put_contents("post.txt", print_r($params, true));


I really don't have any clue where to go from here. Any ideas would be appreciated.
c.wagner
 
Posts: 17
Joined: Fri May 28, 2010 9:23 am
Location: Norfolk, VA

Re: Custom Shipping Add-On

Postby loren_d_c » Wed Nov 11, 2020 1:19 pm

PHP only populates POST data into the $_POST array for certain Content-Types like application/x-www-form-urlencoded (what a browser typically uses when it submits a web form). Other applications that are submitting POST data, such as ShopSite, json, etc, aren't using that content type, so you have to populate your own array from the input data, similar to what you found in the other forum posts. The issue is that so far everyone in those other posts are doing local file execution, so $stdin = fopen("php://stdin","r"); works for them. But when a file is POSTed to via the webserver, stdin isn't where the data is found. Try $stdin = file_get_contents("php://input"); instead. This worked for me:

Code: Select all
   $stdin = file_get_contents("php://input");
   parse_str($stdin,$params);
   file_put_contents("post.txt", print_r($params, true));


Then I modified the output to show one of the parameter values in my cart:

Code: Select all
   echo "status=pass" . "\n";
   echo "option_count=1" . "\n";
   echo "s1option=LTL Freight Shipping to " . $params["cust_zip"] . "\n";
   echo "s1price=300.00";


-Loren
loren_d_c
 
Posts: 2569
Joined: Fri Aug 04, 2006 12:02 pm
Location: Anywhere


Return to User Forum

Who is online

Users browsing this forum: No registered users and 26 guests

cron