Skip to content

Instantly share code, notes, and snippets.

@mjc
Created July 22, 2010 00:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjc/485383 to your computer and use it in GitHub Desktop.
Save mjc/485383 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Get OSCommerce Cart Contents from session
Plugin URI: http://325i.org/osc
Description: allows you to retrieve the cart contents from your OSCommerce install. Requires that your cookie path would include both OSC and wordpress. Works with OSCommerce 2.2RC2.
Author: Michael J. Cohen <mjc@kernel.org>
Version: 1
Author URI: http://325i.org
*/
/**
* this is a real hack
*
* we use the osCsid cookie to grab the session from the mysql database
* then we unserialize the cart and use that data to construct output
* that looks like the box output in OSC
*
* @todo clean up to be a proper wordpress plugin
* @todo throw in some exception handling
* @todo handle configurable items better, products_id looks like 1{4}1{3}5 for those
* @todo desctable -> attrDescTable
* @todo multilingual
*/
$database_user = "changeme";
$database_pass = "changeme";
$database_name = 'changeme';
$database_host = "changeme";
$session_table = "sessions";
$product_description_table = 'products_description';
$attribute_table = 'products_attributes';
$product_info_url = 'http://home.325i.org/oscommerce/product_info.php';
$BOX_SHOPPING_CART_EMPTY = "There's nothing in your cart, kiddo!";
if(!class_exists('shoppingCart')) {
class shoppingCart
{
var $displayProducts, $totalCost, $session, $productInfoURL;
var $user, $pass, $dbname, $dbhost, $sesstable, $desctable, $attrtable;
var $proddesc = "products_description",$prodtable="products";
var $oscDb;
function __construct($user, $pass, $dbname, $dbhost, $sesstable, $desctable, $attrtable, $sesskey, $prodInfoURL)
{
$this->user = $user;
$this->pass = $pass;
$this->dbname = $dbname;
$this->dbhost = $dbhost;
$this->sesstable = $sesstable;
$this->desctable = $desctable;
$this->attrtable = $attrtable;
$this->sesskey = $sesskey;
$this->productInfoURL = $prodInfoURL;
$this->oscDb = new MySQLi( $this->host, $this->user, $this->pass, $this->dbname );
}
function retrieve_session()
{
$query = "select value from " . $this->sesstable . " where sesskey = '" . $this->sesskey . "' and expiry > " . time();
$sessionResult = $this->oscDb->query($query); // @todo prepare, handle exceptions
if (!$sessionResult || !$sessionResult->num_rows > 0) {
// @todo close sessionResult correctly, throw exception
return false;
}
$resultArray = $sessionResult->fetch_array(MYSQLI_ASSOC);
$sessionResult->close();
// @todo exception if it doesn't have cart| at beginning
$this->session = unserialize(preg_replace("/^\w+\|(.+)$/", "$1", $resultArray['value']));
return true;
}
function get_attribute_total($product_id,$attributes) {
$price = 0;
while (list($option, $value) = each($attributes)) {
$priceQuery = "select options_values_price, price_prefix from " .
$this->attrtable . " where products_id = '" . (int)$product_id .
"' and options_id = '" . (int)$option . "' and options_values_id = '" .
(int)$value . "'";
$attribute_price_result = $this->oscDb->query($priceQuery);
/**
* @todo there should only be one result... test that
*/
$resultArray = $attribute_price_result->fetch_array();
if ($resultArray[1] = "+")
$price += $resultArray[0];
else if ($resultArray[1] = "-")
$price += $resultArray[0];
}
return (float)$price;
}
function get_product($cartItem, $key)
{
$product = array();
$product_id = $this->tep_get_prid($key);
$query = "SELECT products_name,products_price FROM `".$this->prodtable."` as p, `".$this->proddesc."` as pd WHERE p.products_id = ".$this->tep_get_prid($key) . " AND pd.products_id = ".$this->tep_get_prid($key)." AND pd.language_id=1";
$msiRes = $this->oscDb->query($query);
$prodArr = $msiRes->fetch_array();
$product['qty'] = $cartItem['qty'];
$product['price'] = (float)$prodArr['products_price'];
$product['products_name'] = $prodArr['products_name'];
if (array_key_exists("attributes", $cartItem)) {
$product['price'] += $this->get_attribute_total($product_id,$cartItem['attributes']);
}
return $product;
}
function build_contents() {
if ($this->session->contents) {
$foo = array_keys($this->session->contents);
foreach ($foo as $key) {
$this->displayProducts[] = $this->get_product($this->session->contents[$key], $key);
}
}
}
function get_total() {
if (count($this->displayProducts) <= 0) {
$this->build_cart();
}
$total = array();
foreach ($this->displayProducts as $item) {
$total = $item['qty'] * $item['price'];
}
return $total;
}
function build_cart() {
if (!$this->retrieve_session()) return false;
if (!$this->build_contents()) return false;
}
/**
* adapted from oscommerce's includes/boxes/shopping_cart.php
*
*/
function get_html() {
global $BOX_SHOPPING_CART_EMPTY;
$cart_contents_string = '';
if (count($this->displayProducts) > 0) {
$cart_contents_string = '<table border="0" width="100%" cellspacing="0" cellpadding="0">';;
for ($i=0, $n=count($this->displayProducts); $i<$n; $i++) {
$cart_contents_string .= '<tr><td align="right" valign="top" class="infoBoxContents">';
$cart_contents_string .= '<span class="infoBoxContents">';
$cart_contents_string .= $this->displayProducts[$i]['qty'] . '&nbsp;x&nbsp;</span></td><td valign="top" class="infoBoxContents"><a class="infobox-cart" href="' .$this->productInfoURL. '?products_id=' . $this->displayProducts[$i]['products_id'] . '">';
$cart_contents_string .= '<span class="infoBoxContents">';
$cart_contents_string .= $this->displayProducts[$i]['products_name'] . '</span></a></td></tr>';
}
$cart_contents_string .= "<td></td>";
$cart_contents_string .= '<td align="right">$'.$this->get_total()."</td></tr>";
$cart_contents_string .= '</table>';
} else {
$cart_contents_string .= $BOX_SHOPPING_CART_EMPTY;
}
return $cart_contents_string;
}
/**
* from OSCommerce
* translates product IDs with attributes to regular product IDs
*/
function tep_get_prid($uprid) {
$pieces = explode('{', $uprid);
if (is_numeric($pieces[0])) {
return $pieces[0];
} else {
return false;
}
}
////
// Return a product ID with attributes
function tep_get_uprid($prid, $params)
{
if (is_numeric($prid)) {
$uprid = $prid;
if (is_array($params) && (sizeof($params) > 0)) {
$attributes_check = true;
$attributes_ids = '';
reset($params);
while (list($option, $value) = each($params)) {
if (is_numeric($option) && is_numeric($value)) {
$attributes_ids .= '{' . (int)$option . '}' . (int)$value;
} else {
$attributes_check = false;
break;
}
}
if ($attributes_check == true) {
$uprid .= $attributes_ids;
}
}
} else {
$uprid = tep_get_prid($prid);
if (is_numeric($uprid)) {
if (strpos($prid, '{') !== false) {
$attributes_check = true;
$attributes_ids = '';
// strpos()+1 to remove up to and including the first { which would create an empty array element in explode()
$attributes = explode('{', substr($prid, strpos($prid, '{')+1));
for ($i=0, $n=sizeof($attributes); $i<$n; $i++) {
$pair = explode('}', $attributes[$i]);
if (is_numeric($pair[0]) && is_numeric($pair[1])) {
$attributes_ids .= '{' . (int)$pair[0] . '}' . (int)$pair[1];
} else {
$attributes_check = false;
break;
}
}
if ($attributes_check == true) {
$uprid .= $attributes_ids;
}
}
} else {
return false;
}
}
return $uprid;
}
}
}
if (!function_exists('wp_get_osc_cart_contents')) {
function wp_get_osc_cart_contents()
{
global $database_user,$database_pass,$database_name,$database_host, $session_table, $product_description_table, $attribute_table, $session_key, $product_info_url;
if (array_key_exists("osCsid", $_COOKIE))
$session_key = $_COOKIE['osCsid'];
else return false; // @TODO handle this better
$cart = new shoppingCart($database_user, $database_pass, $database_name, $database_host, $session_table, $product_description_table, $attribute_table, $session_key,$product_info_url);
$cart->build_cart();
echo $cart->get_html();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment