X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   X-Cart minicart totals and login data on non x-cart pages (https://forum.x-cart.com/showthread.php?t=38469)

minorgod 03-20-2008 09:57 AM

X-Cart minicart totals and login data on non x-cart pages
 
Jon has already posted another method of doing this that may or may not work better for some people depending on what you need to do, but here's what I use in X-Cart 4.1.8. I really didn't want all the x-cart overhead caused by adding xcart's auth.php file to the non-xcart part of my site, and I prefer to use PEAR MDB2/mysqli in most situations. Also, I don't care about letting people log in anywhere except inside x-cart, but when they go to a different part of my site, I still want to display their login info and minicart data, so I use the following scripts...


1. Create a file in your non-xcart part of your site called config.inc.php and it should contain the following:

Code:

<?php
 //include file containing configuration variables
 
define('XCART_START',true);
//this should be the path to your store's config.php file...
require_once("store/config.php");

//debug mode adds a DB error callback to display
//detailed db error info
$debug_mode=false;

//set up some callback functions to handle errors
// Define the app environment (this is: what errors you want to output)

define ('DEBUG_ENV', true);// This function will handle all errors
function handle_pear_error ($error_obj) {    // Be verbose while developing the application
    if (DEBUG_ENV) {
        echo ("<br><br><br>".$error_obj->getMessage()."\n".$error_obj->getDebugInfo());    // Dump a silly message if the site is in production
    } else {
        echo ('Sorry you request can not be processed now. Try again later');
    }
}

// Include the DB abstraction layer which, in this case, is PEAR's MDB2
// To install MDB2 and the necessarry mysql and mysqli drivers from the command line type:
// pear install MDB2
// pear install MDB2#mysql
// pear install MDB2#mysqli

//if that doesn't work, go fish.

require_once("MDB2.php");
$pear_db_identifier="mysqli";

// set PEAR error handling to use the callback function defined above
if($debug_mode){
        PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_pear_error');
}

// connect to the database using the PEAR DB.php abstraction layer so our code will be nice and portable.
// All the variables we're using here are defined in the config.inc.php file which in turn uses the xcart config file.
$dbh = &MDB2::connect("$pear_db_identifier://$sql_user:$sql_password@$sql_host/$sql_db",false);
//load the "Extended" module so we can use functions such as "getAll" and "getRow" and "getOne" and so on.
$dbh->loadModule('Extended');
// set default mode for all resultsets
// to return an associative array with column names as keys.
$dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);


//here is a cute little function we can use to tell if our MDB2 results are errors or not.
function db_error(&$db_result){
    return MDB2::isError($db_result);
}
?>


2. Create another file in the same directory called xcart_session_snooper.php and it should contain the following:
Code:

<?php

require_once("config.inc.php");

//must set up a couple of vars we will need to make xcart work
if(empty($sql_tbl['customers'])){
    $sql_tbl['sessions_data'] = "xcart_sessions_data";
}

//see if we have an xcart session cookie
if(!empty($_COOKIE[$XCART_SESSION_NAME]))
    $sessid = $_COOKIE[$XCART_SESSION_NAME];

//see if we can find an xcart session
$query = "SELECT sessid,data FROM $sql_tbl[sessions_data] WHERE sessid='$sessid'";
$xcart_session = $dbh->getRow($query);

if(db_error($xcart_session)){
    die("could not query x-cart database");
}

if(!empty($xcart_session)){
    $xcart_session['data'] = unserialize(stripslashes($xcart_session['data']));
}

$login  = $xcart_session['data']['login'];
$cart = $xcart_session['data']['cart'];


//get the minicart info...
//we will need to manually decide if we want Fast_Lane_Checkout enabled or not....
$active_modules["Fast_Lane_Checkout"] = true;
if (!empty($cart)) {
    if (!empty($cart["total_cost"])) {
        if (!empty($active_modules["Fast_Lane_Checkout"]))
            $MINICART["total_cost"] = $cart["display_subtotal"];
        else
            $MINICART["total_cost"] = $cart["total_cost"];
    }

    //account for multiple quantities of the same product
    $_temp_productcount = 0;
    if( !empty($cart["products"]) and is_array($cart["products"]) ){
        foreach($cart['products'] as $_tempval){
            $_temp_productcount += $_tempval['amount'];
        }
    }
    $MINICART["total_items"] = $_temp_productcount + ((!empty($cart["giftcerts"]) and is_array($cart["giftcerts"]))?count($cart["giftcerts"]):0);
}

//if we were using smarty we could assign template values here.
?>



Now you should have access to the minicart variables in the rest of you script which you can use as you see fit. I have used this method on standard PHP pages throughout a site, and you can even include the xcart_session_snooper.php file at the top of a Wordpress config file and it doesn't seem to interfere, so I just put my xcart_session_snooper.php file in the main site root directory and then include it from wherever I need it. Works great for me.

Dongan 03-21-2008 03:05 AM

Re: X-Cart minicart totals and login data on non x-cart pages
 
very good share. it would be quite useful for integrating other systems.


All times are GMT -8. The time now is 11:37 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.