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.