How to create a Checkout step (or box)

TheCartPress provides an API to create, easily, steps or box into the checkout process.

The next code shows how to integrate a new checkout step into your ecommerce. The new step allow to select one Additional service (called Additional service 1) and its cost depends of the shopping cart amount.

To try this example you need to create a folder called additionalservicebox in /wp-content/plugins. Into this new folder you have to create the next two files:

File TCPAdditionalServiceBoxPlugin.class.php

<?php
/*
Plugin Name: Additional Service Box
Plugin URI: http://thecartpress.com
Description: Addiontal checkout box
Version: 1.0
Author: TheCartPress team
Author URI: http://thecartpress.com
License: GPL
parent: thecartpress
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! class_exists( 'TCPAdditionalServiceBoxPlugin' ) ) :

class TCPAdditionalServiceBoxPlugin {
function __construct() {
add_action( 'tcp_init', array( $this, 'register_checkout_box' ) );
}

function register_checkout_box() {
tcp_register_checkout_box( 'additionalservicebox/TCPAdditionalServicesBox.class.php', 'TCPAdditionalServiceBox' );
}

}

new TCPAdditionalServiceBoxPlugin();
endif; // class_exists check

First file registers the plugin.

File TCPAdditionalServiceBox.class.php

<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! class_exists( 'TCPAdditionalServiceBox' ) ) :

define( 'TCP_ADD_SER_1', 'tcp_additional_services_1' );

require_once( WP_PLUGIN_DIR . '/thecartpress/checkout/TCPCheckoutBox.class.php' );

class TCPAdditionalServiceBox extends TCPCheckoutBox {

function get_title() {
return 'Additional Services';
}

function get_class() {
return 'additional_services_layer';
}

/**
* Execute when continue button is pressed
*
* @return true if after actions is right. If any error it should return false
*/
function after_action() {
$shoppingCart = TheCartPress::getShoppingCart();
if ( isset( $_REQUEST['tcp_additional_services'] ) ) {
$cost = $this->get_cost();
$shoppingCart->addOtherCost( TCP_ADD_SER_1, $cost, 'Additional service 1' );
} else {
$shoppingCart->deleteOtherCost( TCP_ADD_SER_1 );
}
return true;
}

/**
* Outputs the Box, or Step, content
*/
function show() {
$shoppingCart = TheCartPress::getShoppingCart();
$tcp_additional_services = $shoppingCart->getOtherCostById( TCP_ADD_SER_1 ) !== false;
$cost = $this->get_cost(); ?>
<div id="additional_services_layer_info">
<ul>
<li>
<label>
<input type="checkbox" name="tcp_additional_services" value="yes" <?php checked( $tcp_additional_services ); ?>/> Additional services 1: <?php echo tcp_format_the_price( $cost ); ?>
</label>
</li>
</ul>
</div>
<?php return true;
}

private function get_cost() {
$shoppingCart = TheCartPress::getShoppingCart();
if ( $shoppingCart->getTotal() <= 1250 ) {
return 30;
} else {
return 50;
}
}
}
endif; // class_exists check

The second file defines the box behaviour. The most important function would be “after_action()”. This function is executed when customers click in the “continue” button. If the function returns true, the checkout will display the next step. If false, the box will be displayed again.

The plugin shows how to use the shopping Cart functions to manage Other Costs: addOtherCost(), getOtherCostById() and deleteOtherCost().

You could see more example in thecartpress/checkout folder.