Drupal Web Design 

Delaware Web Design & Drupal eCommerce Websites

Commerce Kickstart Temporary Free Shipping Flat Rate Service

How to create a Commerce Kickstart free shipping flat rate service for a certain time period

How to create a custom Commerce Kickstart free shipping flat rate service for a certain designated time period

 

Concept:

To create a "Free Shipping for the Holidays" service for a limited time and offer free shipping from one time period to another like Thanksgiving to New Years Eve. We start by adding a new flat Rate shipping service and add rules to apply to all orders and for a select time period. Of course, you can adjust these parameters to fit your needs.

 

Work flow:

  • Add a new Flat Rate service.
  • Configure component by adding conditions set to commerce_order and Data comparison for time and date.
  • Hack commerce_kickstart_product.features.inc to force new shipping rule to be selected and appear on top of shipping services lists.
  • Implement hooks in theme template.
  • Spread joy with free shipping!

 

Let's get started... Updated: 11/22/2015!

 

Add a new Flat Rate service:Add a new Flat Rate service

admin/commerce/config/shipping/services/flat-rate/add

Settings:

Title*:
Free Shipping Holidays (Machine name: free_shipping_holidays)

Display title:
Free Shipping for the Holidays!

Description: (Note: The HTML styles are not needed, just for fun! Use plain text if you prefer.)
<span style="color:green; font-weight:bold;">Free Shipping for the Holidays! Ends January 1st, 2015.</span>

Base rate*:
0

Save flat rate

 

Configure component: Conditions ElementsConfiguring component

1. On the Shipping services admin page: (admin/commerce/config/shipping/services/flat-rate)
Click the "configure component" link for your new shipping service.

2. Click the "Add condition" button and add settings as follows.
2a. Click down arrow and select: "Order amount comparison" More info...
2b. For Operator- Value*, Click down arrow and select: "greater than"
2c. For Order amount, type: 0.00

Save

 

Settings overview: (your settings should look like this)

The order.
Data selector*
commerce_order More info...

Operator
Value*
greater than

0.00

 

Setting a time limit by date: Conditions Elements

1. Click the "+ Add and" button and leave selection with the default: "Data comparison" selected and then click "Continue".

2. For the newly added "And" row, for "Operations" : Click the "Add condition" button and leave selection with the default: "Data comparison" selected and click "Continue".

3. Click inside the data selector field box and select the top: "commerce-order:" and wait for the list to load or, click inside the field again.

4. Select "commerce-order:created (Date created)" and click "Continue".

commerce-order:created(Date created) More info...

 

6. On the next page, for Operator Value, select: "is lower than".

7. Under "Data value" Click the "switch to direct input mode" button More info... and enter the end date in GMT PHP format. (Note: You can set the "Date value: Value" to the time that fits your needs.)

Example: 2016-01-01 00:01:01 = January 1st, 20016 at 1 minute after 12 midnight.

 

Settings example:

Data value
The value to compare the data with.
Value *
2016-01-01 00:01:01

Save

 

Hacking the Commerce Kickstart product module features.inc to force shipping services weights/listing.

 

Now, unfortunately, you will have to hack the Commerce Kickstart product module features.inc to force your new Free shipping flat rate service to be on top of the shipping list and selected by default when a customer reaches the shipping part of the checkout process. If you do not want it on top of your shipping services list and selected by default then you can skip the rest of this tutorial and you are done. Flush all caches to see your results.

 

Hacking the Commerce Kickstart product module features.inc

(Note: you will have to do this every time you update/upgrade Commerce Kickstart)

(Note: Add code blocks without PHP start and end tags... <?php ?>)

Note: While you are there hacking the Commerce Kickstart product module features.inc to re-order your shipping services, you might want to adjust the weights of the other shipping services.

 

Setting weights of Express and Standard Shipping

Open Commerce Kickstart product module features.inc file:

C:\PATH TO YOUR LOCAL DRUPAL INSTALL\profiles\commerce_kickstart\profiles\commerce_kickstart\modules\commerce_kickstart\commerce_kickstart_product\commerce_kickstart_product.features.inc

1. Change "Express shipping" at line 20 to: 2

'price_component' => 'flat_rate_express_shipping',
'weight' => 2,

2. Change "Standard shipping" at line 70 to: -10

'price_component' => 'flat_rate_standard_shipping',
'weight' => -10,

 

Then add this to line 64:

(Note: Add code blocks without PHP start and end tags... <?php ?>)

array(
'name' => 'free_shipping_holidays',
'base' => 'free_shipping_holidays',
'display_title' => 'Free Shipping Holidays',
'description' => 'Free shipping for the Holidays!',
'shipping_method' => 'flat_rate',
'rules_component' => TRUE,
'price_component' => 'free_shipping_holidays',
'weight' => -20,
'callbacks' => array(
'rate' => 'commerce_flat_rate_service_rate_order',
'details_form' => 'standard_shipping_details_form',
'details_form_validate' => 'standard_shipping_details_form_validate',
'details_form_submit' => 'standard_shipping_details_form_submit',
),
'module' => 'commerce_flat_rate',
'title' => 'Free Shipping Holidays',
'base_rate' => array(
'amount' => '0',
'currency_code' => 'USD',
'data' => array(),
),
'data' => array(),
'admin_list' => TRUE,
),
?>

If you are following this tutorial and have added a "Free Shipping Holidays" shipping service then, your code should look like this:

(Note: Add code blocks without PHP start and end tags... <?php ?>)

* Implements hook_commerce_flat_rate_default_services().
*/
function commerce_kickstart_product_commerce_flat_rate_default_services() {
$items = array(
'express_shipping' =?> array(
'name' => 'express_shipping',
'base' => 'express_shipping',
'display_title' => 'Express shipping: 1 business day',
'description' => 'An express shipping service with additional fee.',
'shipping_method' => 'flat_rate',
'rules_component' => TRUE,
'price_component' => 'flat_rate_express_shipping',
'weight' => 20,
'callbacks' => array(
'rate' => 'commerce_flat_rate_service_rate_order',
'details_form' => 'express_shipping_details_form',
'details_form_validate' => 'express_shipping_details_form_validate',
'details_form_submit' => 'express_shipping_details_form_submit',
),
'module' => 'commerce_flat_rate',
'title' => 'Express Shipping',
'base_rate' => array(
'amount' => '1500',
'currency_code' => 'USD',
'data' => array(),
),
'data' => array(),
'admin_list' => TRUE,
),
'standard_shipping' => array(
'name' => 'standard_shipping',
'base' => 'standard_shipping',
'display_title' => 'Standard shipping: 3 - 5 business days',
'description' => 'A standard shipping service.',
'shipping_method' => 'flat_rate',
'rules_component' => TRUE,
'price_component' => 'flat_rate_standard_shipping',
'weight' => -10,
'callbacks' => array(
'rate' => 'commerce_flat_rate_service_rate_order',
'details_form' => 'standard_shipping_details_form',
'details_form_validate' => 'standard_shipping_details_form_validate',
'details_form_submit' => 'standard_shipping_details_form_submit',
),
'module' => 'commerce_flat_rate',
'title' => 'Standard Shipping',
'base_rate' => array(
'amount' => '800',
'currency_code' => 'USD',
'data' => array(),
),
'data' => array(),
'admin_list' => TRUE,
),
'free_shipping_holidays' => array(
'name' => 'free_shipping_holidays',
'base' => 'free_shipping_holidays',
'display_title' => 'Free Shipping Holidays',
'description' => 'Free shipping for the Holidays!',
'shipping_method' => 'flat_rate',
'rules_component' => TRUE,
'price_component' => 'free_shipping_holidays',
'weight' => -20,
'callbacks' => array(
'rate' => 'commerce_flat_rate_service_rate_order',
'details_form' => 'standard_shipping_details_form',
'details_form_validate' => 'standard_shipping_details_form_validate',
'details_form_submit' => 'standard_shipping_details_form_submit',
),
'module' => 'commerce_flat_rate',
'title' => 'Free Shipping Holidays',
'base_rate' => array(
'amount' => '0',
'currency_code' => 'USD',
'data' => array(),
),
'data' => array(),
'admin_list' => TRUE,
),
);
return $items;
}
?>

 

Adding hooks to the Commerce Kickstart theme template.php (or, your custom theme)

You will find the Commerce Kickstart theme template.php in:
profiles\commerce_kickstart\profiles\commerce_kickstart\themes\commerce_kickstart_theme\template.php

Add the following to the theme template.php (Note: Add code blocks without PHP start and end tags... <?php ?>)

* Implements hook_commerce_shipping_service_info_alter().
*/
function commerce_shipping_commerce_shipping_service_info_alter(&$shipping_services) {
if (isset($shipping_services['standard_shipping'])) {
$shipping_services['standard_shipping']['weight'] = -10;
}
if (isset($shipping_services['free_shipping_holidays'])) {
$shipping_services['free_shipping_holidays']['weight'] = -20;
}
}
??>

 

Finally, upload the edited files commerce_kickstart_product.features.inc and template.php. Flush all caches and add something to your cart. When you get to the shipping options page your new shipping service should be on top of the list and selected by default.

 

Please feel free to contact me anytime if you have any questions and...
Have fun! 1 (302) 396-8558 | Email

Ralph Manis - Infinitee Drupal Web Design