Adding Product To Cart Remotely

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'.

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);
?>

Hi,

I have followed the instructions but keep getting a 404 error when trying to access that new controller. Is there something that changed in the newer CScart versions? Also, would it be possible to add comments to a product before adding it to the shopping cart?

Thanks,

GJ.

I would guess you have permissions issues with the files you installed or that you do not have the my_changes addon enabled.

I'm not aware of anything in the script that does not work on any V4 version of cs-cart.

Products don't have comments, orders do. This will add to the cart or take the user directly to checkout (depending on how it's invoked)

It can be extended to add "Customer notes" but be advised that the customer would be able to delete them. I don't believe that internal notes are available until after an order is placed. I.e. the internal notes field is not part of the cart but rather an order "additional fields" object.

I would guess you have permissions issues with the files you installed or that you do not have the my_changes addon enabled.

I'm not aware of anything in the script that does not work on any V4 version of cs-cart.

Products don't have comments, orders do. This will add to the cart or take the user directly to checkout (depending on how it's invoked)

It can be extended to add "Customer notes" but be advised that the customer would be able to delete them. I don't believe that internal notes are available until after an order is placed. I.e. the internal notes field is not part of the cart but rather an order "additional fields" object.

Checked if my_changes are enabled and it states that they are enabled for the current folder (the actual message is in Dutch).

Maybe I missed something else in the config? I uploaded the checkout.pre.php to app/addons/my_changes/controllers/customer/ as documented. I can't set any permissions as this is running on a Windows server (but it didn't work on a Linux box either). Still getting a 404 error whenever I want to add an order.

Path for V4 should be app/addons/my_changes/controllers/frontend/checkout.pre.php

The 'customer' area is for V2/V3.

Path for V4 should be app/addons/my_changes/controllers/frontend/checkout.pre.php

The 'customer' area is for V2/V3.

Apologies for the late reply but I was stuck with some other projects. The path indeed now works without any errors but nothing is being added to the shopping cart either. I have double checked the productcode and it is correct. Any hints on how to troubleshoot this? Logging?

Sorry, it works properly every where else I have it installed.

Suggest you check your PHP error_log file and/or add logging to review values. Are you using the remote_add or remote_checkout mode? Suggest you use the remote_checkout mode to ensure it's not a session issue.

Sorry, it works properly every where else I have it installed.

Suggest you check your PHP error_log file and/or add logging to review values. Are you using the remote_add or remote_checkout mode? Suggest you use the remote_checkout mode to ensure it's not a session issue.

Enable PHP logging and I'm getting the error below. This is with either remote_add or remote_checkout.

Is it possible this is caused by a product with a variable price?

[27-Sep-2018 10:21:03 Europe/Berlin] PHP Warning: count(): Parameter must be an array or an object that implements Countable in D:\www\aluminiumplafond.nl\www\webshop\var\cache\templates\responsive\16ad86f458e189deeb9b3782eb6c178d5dceab06.tygh.index.tpl.php on line 80.

Given that the script doesn't have any template components/hooks, I'm not sure what actual template that is referring to.

By variable price do you mean option modifiers, promotions, quantity discounts? Price is determined by the system and does whatever it needs to in order to determine price based on promotions, pricing, options, etc. The public free-ware version does not accept price as a parameter.

If you want to send me that template file and the error message, you can do so via email at support@ez-ms.co and I'll take a look.