I am building a WordPress shopping cart plugin that will tie into our server through a URL that will produce an XML page, which I will then have to parse the info to the WordPress page.
The server contains a session id (ShoppingCart.SessionID)
that I need to get and use to help store which button was clicked and then pull the product info and add it to the cart. Each button
is given a product id
via shortcode (product_id="some#")
from the page owner and that ID is pulled from the server to display the product's info.
How the buttons are set up:
In the shortcode file, I am using a function and have the button set up as
<?php
add_shortcode('add_cart_button', 'add_cart_button_handler');
function add_cart_button_handler($atts) {
extract(shortcode_atts(array(
'product_id' => '',
), $atts));
return print_add_cart_button_for_product($product_id, $atts);
}
In the main plugin file:
This file handles the printing of the button and the function to display the cart
<?php
session_id();
session_start();
// This function prints the button with its assigned $product_id
function print_add_cart_button_for_product($product_id, $atts = array()) {
$replacement .= '<form method="POST" class="cart-button-form" style="display:inline" action="">';
$replacement .= '<input type="hidden" name="bmt_cart_product" value="' . $product_id . '" />';
$replacement .= '</form>';
$replacement .= '</div>';
return $replacement;
}
Here is the function for what is supposed to be displaying the cart/product info:
function show_shopping_cart_handler($atts) {
$output = "";
$form = '';
$output .= '<table style="width: 100%;">';
$output .= '<tr class="cart_item_row">
<th class="cart_item_name_th">' . (__("Item Name", "wordpress-simple-paypal-shopping-cart")) . '</th><th class="cart_price_th">' . (__("Price", "wordpress-simple-paypal-shopping-cart")) . '</th><th></th>
</tr>';
# For now, I am using this to print the headers that display the
# ShoppingCart.SessionID="somevalue", but on every button click it changes the value of the session id.
if (isset($_POST['add_cart_submit'])) {
// The server reads the ID from "&PRODUCTID=product#"
$id = "&PRODUCTID=" . $_POST['cart_product'];
$url = "https://secure.bmtmicro.com/cart?CID=2/WP" . $id;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded
",
'method' => 'GET',
'content' => http_build_query($data)
)
);
//$context = stream_context_create($options);
$context = stream_context_set_default($options);
$output .= file_get_contents($url, false, $context);
}
print_r(get_headers($url));
$output .= '</table>';
return $output;
}
What the XML page looks like with an example product
Not sure if this is needed, but the more info provided, the better (right?). The product ID that is sent will populate these fields and keep adding a new row and then I will parse the necessary fields to the WordPress page.
<shoppingcart sessionid="88582813">
<producttable>
<row number="0">
<productid>22804</productid>
<productname>XFree86 CD</productname>
<quantity>1</quantity>
<productprice>$15.00</productprice>
<discount>$0.00</discount>
<rowtotal>$15.00</rowtotal>
</row>
</producttable>
</shoppingcart>
I am pretty new to PHP and learning as I go (reading/research), but have not been able to find the solution or steps to tackle my issue. Essentially, I believe my question should be (in short):
How can I grab the ShoppingCart.SessionID
from the 3rd party server and use it to populate the cart without refreshing the Session so more than one product will display when more than one button is clicked? Also, is there a better/more productive way to accomplish what I am trying to do?
Thanks in advance and let me know if there's any info I need to add to the question!