[Drupal] How to change a products shippable property during the Ubercart checkout process?

| | 2 min read

We may face a situation in our Drupal site utilizing Ubercart where we need to change the product shippable property during the checkout process. If a product can be shipped freely to certain locations we need to set the Shippable property to false programmatically in the Ubercart checkout process. Read on to know how to change a products shippable property during the Ubercart checkout process.

  • This can be done by using a rules condition using the Drupal Rules module
  • First we need to write a custom module with a rule file (that is modulename.rules.inc).
  • Next we need to implement a hook_rules_condition_info in it and set whether the product is shippable or not in the function that we used to evaluate that condition.
  • The following code will implement the hook_rules_condition_info, which will define a new rules condition in the rules admin page.
    /**
     * Implements hook_rules_condition_info().
     */
    function modulename_rules_condition_info() {
      $conditions['modulename_product_shippable'] = array(
        'label' => t("Check an order's product is shippable"),
        'group' => t('Modulename: group name'),
        'base' => 'modulename_product_shippable',
        'parameter' => array(
          'order' => array(
            'type' => 'uc_order',
            'label' => t('Order'),
          ),
        ),
      );
      return $conditions;
    }
  • The following code will set whether the product is shippable or not
    /**
     * Checks wether this product is shippable or not.
     */
    function modulename_product_shippable($order) {
      $required_check = true;
      foreach ($order->products as $product) {
        if (check the condition whether this product need to be shippable or not) {
          //$required_check = false;
          $product->data['shippable'] = '0';
        }
        // To reset the pointer as per calling of end()
        reset($product->data['attributes']['Format']);
      }
      return $required_check;
    }
    

Hope that was helpful