In this page we’d like to show how to make a plugin, for TheCartPress, to add States of a country.
A plugin for TheCartPress is a plugin for WordPress. They are stored in the same folder (WordPress plugins folder) and are activated fro the same admin panel.
TheCartPress provides a filter to display only the plugins of TheCartPress.
To add States is only necessary to use one action, “tcp_states_loading” (Since 1.0.9).
The next example is the plugin to add the Australian States
<?php /* Plugin Name: TheCartPress, Australian States Plugin URI: http://thecartpress.com Description: Adds the Australian States to TheCartPress Version: 1.0.0 Author: TheCartPress team Author URI: http://thecartpress.com License: GPL Parent: thecartpress */ /** * This file is part of TheCartPress-Australian-State. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ class TCPAustralianStates { function __construct() { add_action( 'tcp_states_loading', array( $this, 'tcp_states_loading' ) ); } function tcp_states_loading() { ?> ,'AU': { //Australia 'NSW' : 'New South Wales', 'QLD' : 'Queensland', 'SA' : 'South Australia', 'TAS' : 'Tasmania', 'VIC' : 'Victoria', 'WA' : 'Western Australia', 'ACT' : 'Australian Capital Territory', 'NT' : 'Northern Territory' } <?php } } new TCPAustralianStates(); ?>
NOTICE: The rest of the tutorial has only a didactic purpose. Since 1.0.9 version, the issue of states/region/taxes has been modified.
In the community, an user made an question about how to show the States of the USA in the address forms (a few weeks ago another user made the same question but with the Canadian States). So here is a tutorial about How to add the States functionality in the eCommerce platform.
/* Plugin Name: TheCartPress, USA states Plugin URI: http://thecartpress.com Description: Shows USA States in TheCartPress Version: 1.0 Author: TheCartPress team Author URI: http://thecartpress.com License: GPL */ /** * This file is part of TheCartPress-usastates. * * TheCartPress-usastates is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TheCartPress-usastates is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TheCartPress-usastates. If not, see . */ class USAStates { function __construct() { if ( is_admin() ) { add_filter( 'tcp_address_editor_load_regions', array( $this, 'load_states' ) ); } else { add_filter( 'tcp_load_regions_for_billing', array( $this, 'load_states' ) ); add_filter( 'tcp_load_regions_for_shipping', array( $this, 'load_states' ) ); } } function load_states( $regions = false ) { $states = array( '' => array( 'name' => 'No state', ), 'AL' => array( 'name' => 'Alabama', ), 'AK' => array( 'name' => 'Alaska', ), 'AZ' => array( 'name' => 'Arizona', ), 'AR' => array( 'name' => 'Arkansas', ), 'CA' => array( 'name' => 'California', ), 'CO' => array( 'name' => 'Colorado', ), 'CT' => array( 'name' => 'Connecticut', ), 'DE' => array( 'name' => 'Delaware', ), 'DC' => array( 'name' => 'District of Columbia', ), 'FL' => array( 'name' => 'Florida', ), 'GA' => array( 'name' => 'Georgia', ), 'HI' => array( 'name' => 'Hawaii', ), 'ID' => array( 'name' => 'Idaho', ), 'IL' => array( 'name' => 'Illinois', ), 'IN' => array( 'name' => 'Indiana', ), 'IA' => array( 'name' => 'Iowa', ), 'KS' => array( 'name' => 'Kansas', ), 'KY' => array( 'name' => 'Kentucky', ), 'LA' => array( 'name' => 'Louisiana', ), 'ME' => array( 'name' => 'Maine', ), 'MD' => array( 'name' => 'Maryland', ), 'MA' => array( 'name' => 'Massachusetts', ), 'MI' => array( 'name' => 'Michigan', ), 'MN' => array( 'name' => 'Minnesota', ), 'MS' => array( 'name' => 'Mississippi', ), 'MO' => array( 'name' => 'Missouri', ), 'MT' => array( 'name' => 'Montana', ), 'NE' => array( 'name' => 'Nebraska', ), 'NV' => array( 'name' => 'Nevada', ), 'NH' => array( 'name' => 'New Hampshire', ), 'NJ' => array( 'name' => 'New Jersey', ), 'NM' => array( 'name' => 'New Mexico', ), 'NY' => array( 'name' => 'New York', ), 'NC' => array( 'name' => 'North Carolina', ), 'ND' => array( 'name' => 'North Dakota', ), 'OH' => array( 'name' => 'Ohio', ), 'OK' => array( 'name' => 'Oklahoma', ), 'OR' => array( 'name' => 'Oregon', ), 'PA' => array( 'name' => 'Pennsylvania', ), 'RI' => array( 'name' => 'Rhode Island', ), 'SC' => array( 'name' => 'South Carolina', ), 'SD' => array( 'name' => 'South Dakota', ), 'TN' => array( 'name' => 'Tennessee', ), 'TX' => array( 'name' => 'Texas', ), 'UT' => array( 'name' => 'Utah', ), 'VT' => array( 'name' => 'Vermont', ), 'VA' => array( 'name' => 'Virginia', ), 'WA' => array( 'name' => 'Washington', ), 'WV' => array( 'name' => 'West Virginia', ), 'WI' => array( 'name' => 'Wisconsin', ), 'WY' => array( 'name' => 'Wyoming', ), // 'AS' => array( // 'name' => 'American Samoa', // ), // 'GU' => array( // 'name' => 'Guam', // ), // 'MP' => array( // 'name' => 'Northern Mariana Islands', // ), // 'PR' => array( // 'name' => 'Puerto Rico', // ), // 'VI' => array( // 'name' => 'Virgin Islands', // ), // 'UM' => array( // 'name' => 'U.S. Minor Outlying Islands', // ), // 'FM' => array( // 'name' => 'Federated States of Micronesia', // ), // 'MH' => array( // 'name' => 'Marshall Islands', // ), // 'PW' => array( // 'name' => 'Palau', // ), ); return $states; } } new USAStates(); ?>
This code should be saved in a file called ‘USAStates.class.php’ into the folder ‘thecartpress-usastates’.
We must pay special attention to these lines:
add_filter( 'tcp_address_editor_load_regions', array( $this, 'load_states' ) ); add_filter( 'tcp_load_regions_for_billing', array( $this, 'load_states' ) ); add_filter( 'tcp_load_regions_for_shipping', array( $this, 'load_states' ) );
This lines use three filters (hooks) to change the way that the Checkout and the address editor shows address. What this plugin does is load an array of USA States. But we could to go very far and we could change the way of calculating taxes, because is typical to have different tax in the different states.
So take a look to the Canadian States plugin.
/* Plugin Name: TheCartPress, Canadian Tax Plugin URI: http://thecartpress.com Description: Add international tax into TheCartPress Version: 1.0 Author: TheCartPress team Author URI: http://thecartpress.com License: GPL */ /** * This file is part of TheCartPress. * * TheCartPress is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TheCartPress is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TheCartPress. If not, see . */ class CanadianTax { function __construct() { if ( is_admin() ) { add_filter( 'tcp_address_editor_load_regions', array( $this, 'tcp_load_regions_for_billing' ) ); } else { add_action( 'tcp_checkout_calculate_other_costs', array( $this, 'tcp_checkout_calculate_other_costs' ) ); add_filter( 'tcp_load_regions_for_billing', array( $this, 'tcp_load_regions_for_billing' ) ); add_filter( 'tcp_load_regions_for_shipping', array( $this, 'tcp_load_regions_for_billing' ) ); } } function tcp_load_regions_for_billing( $regions = false ) { $regions = array( '' => array( 'name' => 'No Canadian state', 'tax' => 0, ), 'AL' => array( 'name' => 'Alberta', 'tax' => 5, ), 'BC' => array( 'name' => 'British Columbia', 'tax' => 12, ), 'MA' => array( 'name' => 'Manitoba', 'tax' => 5, ), 'NB' => array( 'name' => 'New Brunswick', 'tax' => 13, ), 'NL' => array( 'name' => 'Newfoundland and Labrador', 'tax' => 13, ), 'NT' => array( 'name' => 'Northwest Territories', 'tax' => 5, ), 'NS' => array( 'name' => 'Nova Scotia', 'tax' => 15, ), 'NU' => array( 'name' => 'Nunavut', 'tax' => 5, ), 'ON' => array( 'name' => 'Ontario', 'tax' => 13, ), 'PE' => array( 'name' => 'Prince Edward Island', 'tax' => 5, ), 'QU' => array( 'name' => 'Québec', 'tax' => 5, ), 'SA' => array( 'name' => 'Saskatchewan', 'tax' => 5, ), 'YU' => array( 'name' => 'Yukon', 'tax' => 5, ), ); return $regions; } function tcp_checkout_calculate_other_costs() { $selected_billing_address = isset( $_REQUEST['selected_billing_address'] ) ? $_REQUEST['selected_billing_address'] == 'Y' : false; if ( $selected_billing_address ) { $address = Addresses::get( $_REQUEST['selected_billing_id'] ); $billing_region_id = $address->region_id; $billing_country_id = $address->country_id; } else { $billing_region_id = $_REQUEST['billing_region_id']; $billing_country_id = $_REQUEST['billing_country_id']; } $shoppingCart = TheCartPress::getShoppingCart(); $shoppingCart->deleteOtherCost( 'CA_TAX' ); if ( $billing_country_id == 'CA' ) { $regions = $this->tcp_load_regions_for_billing(); $tax = isset( $regions[$billing_region_id] ) ? $regions[$billing_region_id] : 0; $cost = $shoppingCart->getTotal( true ) * $tax['tax'] / 100; $shoppingCart->addOtherCost( 'CA_TAX', $cost, 'tax (' . $tax['name'] . ')', '9999' ); } } } new CanadianTax(); ?>
This code should be saved in a file called ‘CanadianTax.class.php’ into the folder ‘thecartpress-canadian-tax’.
In this new code there are two new issues: the action ‘tcp_checkout_calculate_other_costs’ and the ‘tax’ field in the array of states.
add_action( 'tcp_checkout_calculate_other_costs', array( $this, 'tcp_checkout_calculate_other_costs' ) );
In the checkout page, before calculating the cost, this action is called. So we have an opportunity to change the result, in this case adding the tax of the billing state.
if ( $billing_country_id == ‘CA’ ) This function is only executed if the shipping country is Canada.
Please, you could make any question or comment in our great community.
And that’s all.