Adding Product To Cart Remotely

What hooks or functions would I need to post to where to get a product added to a cart remotely?



There’s a separate site that will have the proper product id’s and a quantity field, I want to be able to submit that to the cart which will be on a different site.



Any suggestions?

You can create a file named addons/my_changes/controllers/customer/checkout.pre.php with the content below.

You can then create links in your other application like:

/index.php?dispatch=my_changes.remote_checkout.abc-123

Where 'abc-123' is the 'product_code' of the item you want added to the cart and to start the checkout process.

If you want to simply add to the cart without going into checkout (not recommended) then you'd change 'remote_checkout' to 'remote_add'.


```php

/*****************************
* Copyright 2009, 1st Source IT, LLC
* All rights reserved.
* Permission granted for use as
* long as this copyright notice, associated text and
* links remain in tact.
* Licensed for a single domain and a single instance of EZ-cart.
* Additional licenses can be purchased for additonal sites.
*
* http://www.ez-ms.com
* http://www.ez-om.com
* http://www.1sit.com*
*
* End copyright notification
*/
if ( !defined('AREA') ) die('Access denied');

if( $mode == 'remote_checkout' || $mode == 'remote_add' ) {
// Cart is empty, create it
if (empty($_SESSION['cart'])) {
fn_clear_cart($_SESSION['cart']);
} else {
$cart =& $_SESSION['cart'];
}

// $action holds the product_code (not product_id). $dispatch_extra otpionally holds the quantity.
// So need to lookup the code and get product ID
$product_code = ACTION;
$amount = DISPATCH_EXTRA;
if( !$product_code ) {
return array(CONTROLLER_STATUS_REDIRECT, INDEX_SCRIPT);
}
$product_id = db_get_field("SELECT product_id FROM ?:products WHERE product_code=?s LIMIT 1", $product_code);
if( empty($product_id) ) {
fn_set_notification('E', 'Error', "No such product '".$product_code."'", true);
return array(CONTROLLER_STATUS_REDIRECT, $index_script);
}
// Add to cart button was pressed for single product on advanced list
$_REQUEST['product_data'][$product_id]['amount'] = $amount ? $amount : 1;
if( empty($_REQUEST['product_data']) && $_SERVER['REQUEST_METHOD'] != 'POST' ) {
$_REQUEST['product_data'][$product_id] = fn_get_product_data($product_id, $auth);
}

fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
fn_save_cart_content($cart, $auth['user_id']);

$previous_state = md5(serialize($cart['products']));
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
$_SESSION['cart'] = $cart;
if (md5(serialize($cart['products'])) != $previous_state) {
$msg = fn_get_lang_var('text_product_added_to');
$msg = str_replace('[target]', strtolower(fn_get_lang_var('cart')), $msg);
// fn_set_notification('N', fn_get_lang_var('notice'), $msg);
$cart['recalculate'] = true;
}

$_suffix = ".checkout";

if (defined('AJAX_REQUEST')) {
$view->assign('cart_amount', $cart['amount']);
$view->assign('cart_subtotal', $cart['display_subtotal']);
$view->assign('force_items_deletion', true);

// The redirection is made in order to update the page content to see changes made in the cart when adding a product to it from the 'view cart' or 'checkout' pages.
if (strpos($_SERVER['HTTP_REFERER'], 'dispatch=checkout.cart') || strpos($_SERVER['HTTP_REFERER'], 'dispatch=checkout.checkout')) {
$ajax->assign('force_redirection', $_SERVER['HTTP_REFERER']);
}

$view->display('views/checkout/components/cart_status.tpl');
$ajax->assign('force_redirection', INDEX_SCRIPT."?dispatch=checkout$_suffix");
exit;
}

// If not just an add, force to checkout
if( $mode == 'remote_add' )
$_suffix = ".cart";
fn_redirect(INDEX_SCRIPT."?dispatch=checkout$_suffix");
}
return array(CONTROLLER_STATUS_OK);
?>

```

so this direct add to cart to checkout?

no need to click checkout anymore?

Wow, that's fantastic. I can modify that to POST over a tonne of products to cart. I already finished my script to add products to inventory remotely. So now I'm nearly done!



Thanks a lot dude!

Not sure about posting a tonne of products to the cart. The goal is to allow a user to click a link (in a wordpress page for instance) and add that product code to the cart and go through the checkout process.

I actually built another site that's so drastically different from cs-cart that it was it's own site. Also cs-cart has some horrible css and markup so it's easier to work outside of it. I built a whole system in YiiFramework.



When new products are entered in the backend, it automatically creates a dummy product on the cs-cart site. People will have a shopping cart on this other site, and when they checkout, it transfers the cart to cs-cart.

thanks this works great.

any way to select product options also remotely with the add to cart function?

You'd have to add them as additional parameters and the provide for parsing them out in the script that adds them to the cart. Added complexity. Would suggest you used option combinations and have each combination be related to a separate SKU. Then there's no need to extend the script but might make displaying on the parent page more voluminous.

I am using the separate SKU's now.

I am at +1800 sku's, but will have to expand to over +100000 later,

Thought that one sku with several options would eliminate the massive number of sku's needed…



would be great to have the parameters , index.php?dispatch=my_changes.remote_add.&option1=&option2=



Do you know the needed parameter names?



I do know php, but i am not familiar with cs-cart…

If you would like the code extended to support a custom name/value pair that would set a product option when the product is added to the cart (or goes to checkout), please go to my site and request a quote. Note that it would require an exact match of both the name and the value for the option to be set correctly in the cart.

To request a quote, go to: http://www.ez-ms.com/get-a-quote.html

[font=“Verdana”]Thank you TBIRNSETH. You are very very generous with your time.[/font]

hi every body,

I try to put into cart from remote website by using api.

This input to cart need to be process in product_configurator mode ?!

[color=#282828][font=Verdana]TBIRNSETH I understand your remote process and before changing my process for yours and sending to cart from virtual form. [/font][/color][color=#282828][font=Verdana]I would like to know if some try this before ?! And if there is a tutorial to do it ? [/font][/color]

[color=#282828][font=Verdana]At this time I success to communicate down to get (products) with api. And I all my trying to put inside cart are unsuccessfull. Do I need to use [/font][/color]fn_product_configurator_pre_add_to_cart or fn_product_configurator_add_to_cart ?

Tks for any help

Best regards







[color=#282828][font=Verdana] [/font][/color]

Sorry, I can't address the configurator products… I completely understand why cs-cart has dropped this addon after trying to work with it a couple of years ago for a “gift basket” client. The code base is pretty much a mess. It works for a specific application but is not very flexible. It might have changed, but back when I tried to work with it, you couldn't even select an option to a product being added (I.e. color or flavor). It had to be setup as a separate SKU.

hi tbirnseth,

First of all I don't see you answer before sending my direct mail. my apologise !!

Well I'm agree with you about this configurator. It's really not perfect. But for my project and it's enough

and more usefull than options !! I choose product configurator because options inventory couldn't be use here.

In fact options needs inventory link with sku product and in this project all options will need separate inventory from sku product.

So I use product configurator with individual sku for all products. So now I need to put product configurator from outside hosting.

As my personnal and direct mail wich way is the best to doing this using api ? from get method as your checkout.pre.php ?

Thanks for your answer.

Best regards.

MMovies

The free script I provided is intended to work for the simple case where the SKU relates to a product in the products table. Other hybrid skus aren't supported. Happy to look at your custom needs, but it becomes a development project.

Hi tbirnseth,

thanks for your reply and i agree it's a developpment…sorry.

I take your base addon to developp my own with multi cases for my need with product configurator.

I don't understand why I could not call with success same functions as you in a api mode…

later when I have more time I'll ask to support what's wrong…thanks a lot

I'll give you the url when final project will be finish and again thanks a lot for your time.

Best regards

MMovies

Hi

I have been trying to get this to work on version 4.1.2 can someone help me the path given above is



“You can create a file named addons/my_changes/controllers/customer/checkout.pre.php”



can someone tell me exactly the root path to locate this file, I am not using the default theme.



Alan

Here is a reposting of the V4 version. In V4 it should be installed as app/addons/my_changes/frontend/checkout.pre.php

This version also supports more than one product being specified (see the comments).



Ignore the 'ajax_add' mode. It was an experiment but the session/cart was not maintained as expected. This does NOT support specifying options to a product.


```php

/*****************************
* Copyright 2009, 2010, 2011, 2012, 2013, 2014 1st Source IT, LLC
* All rights reserved.
* Permission granted for use as
* long as this copyright notice, associated text and
* links remain in tact.
* Licensed for a single domain and a single instance of EZ-cart.
* Additional licenses can be purchased for additonal sites.
*
* http://www.ez-ms.com
* http://www.ez-om.com
* http://www.1sit.com*
*
* End copyright notification
*/

/** Usage:
** Your link name
** Note format of product codes is:
** pcode@quantity|next_pcode
**
** Modes: remote_checkout, remote_add, ajax_add
** Action: products (see above)
** Dispatch Extra: default quantity
** Parameters: c_code - coupon code
**/

if ( !defined('AREA') ) die('Access denied');
use Tygh\Registry;

if( $mode == 'remote_checkout' || $mode == 'remote_add' || $mode == 'ajax_add' ) {
// Cart is empty, create it
if (empty($_SESSION['cart'])) {
fn_clear_cart($_SESSION['cart']);
}
$cart =& $_SESSION['cart'];

// $action holds the product_code (not product_id). $dispatch_extra otpionally holds the quantity.
// So need to lookup the code and get product ID
$product_codes = explode('|', $action);
$default_quantity = $dispatch_extra ? dispatch_extra : 1; // Default quantity
if( empty($product_codes) ) {
fn_set_notification('E', $mode, "No products", 'K');
return array(CONTROLLER_STATUS_REDIRECT, "/");
}

if( empty($auth) )
$auth =& $_SESSION['auth'];

foreach($product_codes as $p_str) {
list($product_code, $amount) = explode('@', $p_str);
$product_code = trim($product_code);
if( empty($amount) )
$amount = $default_quantity;

$product_id = db_get_field("SELECT product_id FROM ?:products WHERE product_code=?s LIMIT 1", $product_code);
if( empty($product_id) ) {
fn_set_notification('E', 'Error', "No such product '".$product_code."'", true);
return array(CONTROLLER_STATUS_REDIRECT, $index_script);
}
// Add to cart button was pressed for single product on advanced list
if( empty($_REQUEST['product_data']) && $_SERVER['REQUEST_METHOD'] != 'POST' ) {
$product_data = $_REQUEST['product_data'][$product_id] = fn_get_product_data($product_id, $auth);
}
$product_data['amount'] = $_REQUEST['product_data'][$product_id]['amount'] = $amount ? $amount : 1;
fn_add_product_to_cart(array($product_id => $product_data), $cart, $auth);
}

// Check for coupon code
if( $coupon_code = empty($_REQUEST['c_code']) ? '' : $_REQUEST['c_code'] ) {
$cart['pending_coupon'] = $coupon_code;
$cart['recalculate'] = true;
}

fn_save_cart_content($cart, $auth['user_id']);

$previous_state = md5(serialize($cart['products']));
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
// $_SESSION['cart'] = $cart;
if (md5(serialize($cart['products'])) != $previous_state) {
$msg = fn_get_lang_var('text_product_added_to');
$msg = str_replace('[target]', strtolower(fn_get_lang_var('cart')), $msg);
// fn_set_notification('N', fn_get_lang_var('notice'), $msg);
$cart['recalculate'] = true;
}

$_suffix = ".checkout";

if (defined('AJAX_REQUEST') && $mode != 'ajax_add' ) {
$view->assign('cart_amount', $cart['amount']);
$view->assign('cart_subtotal', $cart['display_subtotal']);
$view->assign('force_items_deletion', true);

// The redirection is made in order to update the page content to see changes made in the cart when adding a product to it from the 'view cart' or 'checkout' pages.
if (strpos($_SERVER['HTTP_REFERER'], 'dispatch=checkout.cart') || strpos($_SERVER['HTTP_REFERER'], 'dispatch=checkout.checkout')) {
$ajax->assign('force_redirection', $_SERVER['HTTP_REFERER']);
}

$view->display('views/checkout/components/cart_status.tpl');
$ajax->assign('force_redirection', $index_script."?dispatch=checkout$_suffix");
exit;
}
if( $mode == 'ajax_add' )
fn_redirect($_SERVER['HTTP_REFERER']);

// If not just an add, force to checkout
if( $mode == 'remote_add' )
$_suffix = ".cart";
fn_redirect("checkout$_suffix");
}
return array(CONTROLLER_STATUS_OK);
?>

```

[size=4]HI[font=arial,helvetica,sans-serif] tbirnseth[/font][/size]

Thanks for posting this but I may be being a bit thick. I placed the file instructed but when I test the link it goes to the cart but the cart is always empty. I have put a test link here [url=“ability.ie”]ability.ie

Can you tell me were I am going wrong, also the coupon_code, Is that a gift cert code?



Alan

It's going to checkout.checkout and then it's finding the cart is empty. Do your require https for your checkout? If you add under http and the go to checkout under https, then the cart may not have any contents. I would have to add some diagnostics to figure out what's happening at your site and I'm sorry, but I don't have time to investigate site specific issues right now.