Direct buy button

Hi everyone,



Im trying to add a new button in the product page (A Direct Buy button), where customers press it and go directly to the checkout with that product added on the cart.



I’m using ajax for the rest of the buttons, so i need to keep ajax enabled



Hope you guys can help me out.



Thanks in advance

I’ve not tried it but, /include/customer/checkout.php, line 81:


fn_redirect("$index_script?$target_name=checkout&$mode_name=cart");


change to


fn_redirect("$index_script?$target_name=checkout&$mode_name=[COLOR="Red"]checkout[/COLOR]");

If you want a buy now button in addition to the add to cart button, here’s how:



[COLOR=Red]EDIT: Make sure AJAX “Add to cart” is disabled. It will not work with that.[/COLOR]


  1. In the languages page of the admin section, create a new variable:



    buy_now = Buy now


  2. In “/skins/CLIENT_SKIN/customer/buttons/”



    Create a new file called “buy_now.tpl”



{if $settings.General.allow_anonymous_shopping == 'Y' || $auth.user_id}
{include file="buttons/button.tpl" but_text=$but_text|default:$lang.buy_now but_type="" but_onclick=$but_onclick but_href=$but_href but_target=$but_target action_but="" but_style=$but_style}
{/if}




3. In “/include/customer/checkout.php”



AFTER



```php

// Add product to the cart

if ($mode == ‘add’) {

include CORE_DIR . ‘include_addons.php’;



$previous_state = md5(serialize(@$cart[‘products’]));

fn_add_product_to_cart($product_data, $auth);

fn_save_cart_content($cart, $auth[‘user_id’]);

if (defined(‘AJAX_REQUEST’)) {

fn_calculate_cart_content($cart, $auth, ‘S’, true, ‘F’, true);



include CORE_DIR . ‘include_addons.php’;

$smarty->assign_by_ref(‘cart_amount’, $cart[‘amount’]);

$smarty->assign_by_ref(‘cart_subtotal’, $cart[‘display_subtotal’]);



if (md5(serialize(@$cart[‘products’])) != $previous_state) { // FIXME!!! Only for ajax - add JS checking

$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;

}

fn_show_template(‘side_boxes/cart.tpl’, $smarty);

exit;

} else {

if (!empty($redirect_url)) {

fn_redirect($redirect_url);

} else {

fn_redirect(“$index_script?$target_name=checkout&$mode_name=cart”);

}

}

} ```ADD



```php

if ($mode == ‘add2’) {

include CORE_DIR . ‘include_addons.php’;



$previous_state = md5(serialize(@$cart[‘products’]));

fn_add_product_to_cart($product_data, $auth);

fn_save_cart_content($cart, $auth[‘user_id’]);

if (!empty($redirect_url)) {

fn_redirect($redirect_url);

} else {

fn_redirect(“$index_script?$target_name=checkout&$mode_name=checkout”);

}

} ```4. In “/skins/CLIENT_SKIN/customer/products_pages/product_details.tpl”



REPLACE



{include file="buttons/add_to_cart.tpl" but_onclick="fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}
WITH



{include file="buttons/add_to_cart.tpl" but_onclick="product_form.set_field_value(mode_name, 'add'); fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}
{if $settings.DHTML.ajax_add_to_cart != 'Y'} {include file="buttons/buy_now.tpl" but_onclick="product_form.set_field_value(mode_name, 'add2'); fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}{/if}

woops i missed this post :slight_smile:



Hi… baballuci, i was looking a button addition, thanks for your post man, im learning a lot!!



Cheers






[quote name=‘baballuci’]I’ve not tried it but, /include/customer/checkout.php, line 81:


fn_redirect("$index_script?$target_name=checkout&$mode_name=cart");


change to


fn_redirect("$index_script?$target_name=checkout&$mode_name=[COLOR="Red"]checkout[/COLOR]");
[/QUOTE]

Hey… i miss out this thread :slight_smile:



you did it again Matt :rolleyes:



thanks+ for your time/help !!








[quote name=‘mdekok3000’]If you want a buy now button in addition to the add to cart button, here’s how:



EDIT: Make sure AJAX “Add to cart” is disabled. It will not work with that.


  1. In the languages page of the admin section, create a new variable:



    buy_now = Buy now


  2. In “/skins/CLIENT_SKIN/customer/buttons/”



    Create a new file called “buy_now.tpl”



{if $settings.General.allow_anonymous_shopping == 'Y' || $auth.user_id}
{include file="buttons/button.tpl" but_text=$but_text|default:$lang.buy_now but_type="" but_onclick=$but_onclick but_href=$but_href but_target=$but_target action_but="" but_style=$but_style}
{/if}
3. In “/include/customer/checkout.php”



AFTER



```php

// Add product to the cart

if ($mode == ‘add’) {

include CORE_DIR . ‘include_addons.php’;



$previous_state = md5(serialize(@$cart[‘products’]));

fn_add_product_to_cart($product_data, $auth);

fn_save_cart_content($cart, $auth[‘user_id’]);

if (defined(‘AJAX_REQUEST’)) {

fn_calculate_cart_content($cart, $auth, ‘S’, true, ‘F’, true);



include CORE_DIR . ‘include_addons.php’;

$smarty->assign_by_ref(‘cart_amount’, $cart[‘amount’]);

$smarty->assign_by_ref(‘cart_subtotal’, $cart[‘display_subtotal’]);



if (md5(serialize(@$cart[‘products’])) != $previous_state) { // FIXME!!! Only for ajax - add JS checking

$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;

}

fn_show_template(‘side_boxes/cart.tpl’, $smarty);

exit;

} else {

if (!empty($redirect_url)) {

fn_redirect($redirect_url);

} else {

fn_redirect(“$index_script?$target_name=checkout&$mode_name=cart”);

}

}

}

```ADD



```php

if ($mode == ‘add2’) {

include CORE_DIR . ‘include_addons.php’;



$previous_state = md5(serialize(@$cart[‘products’]));

fn_add_product_to_cart($product_data, $auth);

fn_save_cart_content($cart, $auth[‘user_id’]);

if (!empty($redirect_url)) {

fn_redirect($redirect_url);

} else {

fn_redirect(“$index_script?$target_name=checkout&$mode_name=checkout”);

}

}

```4. In “/skins/CLIENT_SKIN/customer/products_pages/product_details.tpl”



REPLACE



{include file="buttons/add_to_cart.tpl" but_onclick="fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}
WITH



{include file="buttons/add_to_cart.tpl" but_onclick="product_form.set_field_value(mode_name, 'add'); fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}
{if $settings.DHTML.ajax_add_to_cart != 'Y'} {include file="buttons/buy_now.tpl" but_onclick="product_form.set_field_value(mode_name, 'add2'); fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}{/if}
[/QUOTE]

If you want a buy now button in addition to the add to cart button, here's how:

EDIT: Make sure AJAX "Add to cart" is disabled. It will not work with that.

1. In the languages page of the admin section, create a new variable:

buy_now = Buy now

2. In "/skins/CLIENT_SKIN/customer/buttons/"

Create a new file called "buy_now.tpl"

{if $settings.General.allow_anonymous_shopping == 'Y' || $auth.user_id}
    {include file="buttons/button.tpl" but_text=$but_text|default:$lang.buy_now but_type="" but_onclick=$but_onclick but_href=$but_href but_target=$but_target action_but="" but_style=$but_style}
{/if}
3. In "/include/customer/checkout.php"

AFTER

```php
// Add product to the cart
if ($mode == 'add') {
include CORE_DIR . 'include_addons.php';

$previous_state = md5(serialize(@$cart['products']));
fn_add_product_to_cart($product_data, $auth);
fn_save_cart_content($cart, $auth['user_id']);
if (defined('AJAX_REQUEST')) {
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);

include CORE_DIR . 'include_addons.php';
$smarty->assign_by_ref('cart_amount', $cart['amount']);
$smarty->assign_by_ref('cart_subtotal', $cart['display_subtotal']);

if (md5(serialize(@$cart['products'])) != $previous_state) { // FIXME!!! Only for ajax - add JS checking
$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;
}
fn_show_template('side_boxes/cart.tpl', $smarty);
exit;
} else {
if (!empty($redirect_url)) {
fn_redirect($redirect_url);
} else {
fn_redirect("$index_script?$target_name=checkout&$mode_name=cart");
}
}
} ```ADD

```php
if ($mode == 'add2') {
include CORE_DIR . 'include_addons.php';

$previous_state = md5(serialize(@$cart['products']));
fn_add_product_to_cart($product_data, $auth);
fn_save_cart_content($cart, $auth['user_id']);
if (!empty($redirect_url)) {
fn_redirect($redirect_url);
} else {
fn_redirect("$index_script?$target_name=checkout&$mode_name=checkout");
}
} ```4. In "/skins/CLIENT_SKIN/customer/products_pages/product_details.tpl"

REPLACE

{include file="buttons/add_to_cart.tpl" but_onclick="fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}
WITH

{include file="buttons/add_to_cart.tpl" but_onclick="product_form.set_field_value(mode_name, 'add'); fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}
{if $settings.DHTML.ajax_add_to_cart != 'Y'}   {include file="buttons/buy_now.tpl" but_onclick="product_form.set_field_value(mode_name, 'add2'); fn_add_to_cart(`$product.product_id`, `$smarty.capture.ajax_add_to_cart`);"}{/if}

i am looking for a similar addon or code for cscart- 4.3.4 ultimate

You can eanble the following tweaks in the config.local.php file:

    'disable_dhtml' => false, // Disable Ajax-based pagination and Ajax-based "Add to cart" button
...
    'redirect_to_cart' => false, // Redirect customer to the cart contents page. Used with the "disable_dhtml" setting. 

You can eanble the following tweaks in the config.local.php file:

    'disable_dhtml' => false, // Disable Ajax-based pagination and Ajax-based "Add to cart" button
...
    'redirect_to_cart' => false, // Redirect customer to the cart contents page. Used with the "disable_dhtml" setting. 

thnx, but i want the "Add to Cart" button to be there and add a new button "Buy Now".. when customer click add to cart, then it will act normal and add to cart with ajax. but when click "Buy Now" it will striaght go to checkout.checkout page with only that product in cart.

I think in this case you should create a button with another name value. Something like this in the submit input: name="dispatch[checkout.buy_now..12]"

Copy the whole '$_SERVER['REQUEST_METHOD'] == 'POST'' and '$mode == 'add' part of checkout.php file, change add to buy_now and remove all Registry::get('config.tweaks.disable_dhtml') && Registry::get('config.tweaks.redirect_to_cart') conditions. Considering that they are always set to true.

thnx, but i want the "Add to Cart" button to be there and add a new button "Buy Now".. when customer click add to cart, then it will act normal and add to cart with ajax. but when click "Buy Now" it will striaght go to checkout.checkout page with only that product in cart.

This task is easy. Please open the following file:

design/themes/responsive/templates/common/product_data.tpl

and add:

{include file="buttons/button.tpl" but_id="button_cart_`$obj_prefix``$obj_id`" but_name="dispatch[checkout.add.buy_now.`$obj_id`]" but_role=$but_role block_width=$block_width obj_id=$obj_id product=$product but_meta=$add_to_cart_meta but_text=__('buy_now')} 

after the line of code:

{include file="buttons/add_to_cart.tpl" but_id="button_cart_`$obj_prefix``$obj_id`" but_name="dispatch[checkout.add..`$obj_id`]" but_role=$but_role block_width=$block_width obj_id=$obj_id product=$product but_meta=$add_to_cart_meta}

Then open the following file

app/controllers/frontend/checkout.php

and add:

if ($action == 'buy_now') {
    Tygh::$app['ajax']->assign('force_redirection', fn_url('checkout.cart'));
}

before this code:

        return array(CONTROLLER_STATUS_OK, 'checkout.cart');
    }
//
// Update products quantity in the cart
//

Clear the cache and enjoy

thnx a lot.. helps quite a much.. just that i was for a bit different thing.

when user select add to cart. the product goes to the cart.

when user select buy now, then the product is not added to cart, its directly processed to checkout. means if user have a product a in cart, and he presses buy now for product b, then he will see checkout for product b only.. where as product a will still remain in his cart.

more example you can see this website. http://www.flipkart.com

here add any product (a) to cart. now go to some other product b and press buy now. it will then take u to checkout for that product b only.. after checkout you will see that product (a) is still in cart.

so,, means the cart thing remains as it is,, but buy-now functionality gives you to checkout a single product.

This task is easy. Please open the following file:

design/themes/responsive/templates/common/product_data.tpl

and add:

{include file="buttons/button.tpl" but_id="button_cart_`$obj_prefix``$obj_id`" but_name="dispatch[checkout.add.buy_now.`$obj_id`]" but_role=$but_role block_width=$block_width obj_id=$obj_id product=$product but_meta=$add_to_cart_meta but_text=__('buy_now')} 

after the line of code:

{include file="buttons/add_to_cart.tpl" but_id="button_cart_`$obj_prefix``$obj_id`" but_name="dispatch[checkout.add..`$obj_id`]" but_role=$but_role block_width=$block_width obj_id=$obj_id product=$product but_meta=$add_to_cart_meta}

Then open the following file

app/controllers/frontend/checkout.php

and add:

if ($action == 'buy_now') {
    Tygh::$app['ajax']->assign('force_redirection', fn_url('checkout.cart'));
}

before this code:

        return array(CONTROLLER_STATUS_OK, 'checkout.cart');
    }
//
// Update products quantity in the cart
//

Clear the cache and enjoy

sorry i click the red button by mistake

Careful ;)

thnx a lot.. helps quite a much.. just that i was for a bit different thing.

when user select add to cart. the product goes to the cart.

when user select buy now, then the product is not added to cart, its directly processed to checkout. means if user have a product a in cart, and he presses buy now for product b, then he will see checkout for product b only.. where as product a will still remain in his cart.

more example you can see this website. http://www.flipkart.com

here add any product (a) to cart. now go to some other product b and press buy now. it will then take u to checkout for that product b only.. after checkout you will see that product (a) is still in cart.

so,, means the cart thing remains as it is,, but buy-now functionality gives you to checkout a single product.

Ok. I understand what you mean. Additional code modifications are required in this case. If you are interested in this modification, feel free to contact us

Do not understand what is the use of it?

Buy now would be useful if it goes not to checkout but to paypal express checkout so you get instant payment but not order.

I am going to ask to develop a best offer button, that will create instant order with login credentials and price input feature.

thnx a lot.. helps quite a much.. just that i was for a bit different thing.

when user select add to cart. the product goes to the cart.

when user select buy now, then the product is not added to cart, its directly processed to checkout. means if user have a product a in cart, and he presses buy now for product b, then he will see checkout for product b only.. where as product a will still remain in his cart.

more example you can see this website. http://www.flipkart.com

here add any product (a) to cart. now go to some other product b and press buy now. it will then take u to checkout for that product b only.. after checkout you will see that product (a) is still in cart.

so,, means the cart thing remains as it is,, but buy-now functionality gives you to checkout a single product.

ecomlabs, you almost done what nonu_don wanted. Slight modification is required.

Solution:

1. Add the button. For example, you can add the following code:

{include file="buttons/button.tpl" but_id="button_cart_buy_now_`$obj_prefix``$obj_id`" but_name="dispatch[checkout.add.buy_now.`$obj_id`]" but_role=$but_role block_width=$block_width obj_id=$obj_id product=$product but_meta="cm-no-ajax" but_text=__('buy_now')}

just after the following line:

{include file="buttons/add_to_cart.tpl" but_id="button_cart_`$obj_prefix``$obj_id`" but_name="dispatch[checkout.add..`$obj_id`]" but_role=$but_role block_width=$block_width obj_id=$obj_id product=$product but_meta=$add_to_cart_meta}

in the design/themes/[YOUR THEME]/templates/common/product_data.tpl

(!) NOTE, the 'cm-no-ajax' class is VERY important for this button.

2. Add the following code:

        if ($action == 'buy_now') {
            fn_redirect('checkout.cart');
        }

just before the following line:

if (md5(serialize($cart['products'])) != $previous_state && empty($cart['skip_notification'])) {

in the app/controllers/frontend/checkout.php file (line 65 approximately).

3. Clear the cache just to be sure.

4. Enjoy.


But your new button will be displayed next to the 'Add to cart' anywhere the latest is displayed.

ecomlabs, you almost done what nonu_don wanted. Slight modification is required.

I am afraid, you misunderstood him. Our solution works correctly, but he wants to use 2 carts in parallel. It means that you should an an ability to checkout with one selected product as well as the whole cart at the same time. Unfortunately, it is not possible without significant code modifications

If so - yes.

nonu_don, simtechdev and we are at you service

You can still use our free my_changes checkout.pre.php module that will give you usage in the comments in the script.

You'd simply create your button to do a get request (redirect) to

your_domain.com?dispatch=checkout.remote_checkout.[product_code of the item].[optional quantity].

I.e.

foo.com?dispatch=checkout.remote_checkout.abc-123.3

To add 3 of 'abc-123' to the cart and go directly to checkout.

You can use this either from a button inside cs-cart or you can use it from a remote link (like someone's blog or email).

It's free and been in use since early V2.