You are here

commerce_pado.module in Commerce Product Add-on 7

Same filename and directory in other branches
  1. 8 commerce_pado.module

Commerce Product Add On adds an option to entityreference fields that allows selected products to act as "add-ons" for the parent product.

File

commerce_pado.module
View source
<?php

/**
 * @file
 * Commerce Product Add On adds an option to entityreference fields
 * that allows selected products to act as "add-ons" for the parent
 * product.
 */

/*
 * Implements hook_form_alter().
 */
function commerce_pado_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'field_ui_field_edit_form') {

    /*
     * Only add the option if this instance is attached to a Commerce Product
     * entity, and the field is of type 'entityreference.'
     */
    if (isset($form['#instance']) && $form['#instance']['entity_type'] == 'commerce_product' && ($form['#field']['type'] == 'entityreference' || $form['#field']['type'] == 'commerce_product_reference')) {
      $form['commerce_pado_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Commerce Product Add On Settings'),
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
        '#weight' => -1,
      );
      $commerce_pado_settings = variable_get('commerce_pado_settings', array());
      $instance_id = $form['#instance']['id'];
      $form['commerce_pado_settings']['commerce_pado_on'] = array(
        '#type' => 'checkbox',
        '#title' => t('Offer products referenced with this field as add-on products on the Add to Cart form.'),
        '#description' => t('Each product referenced will be displayed next to a
                          checkbox on the Add to Cart form. You can control how
                          a product is displayed by the editing Add-on view in
                          the display settings of the product entity. If selected,
                          the target type must be set to <strong>Commerce Product</strong>.'),
        '#default_value' => isset($commerce_pado_settings[$instance_id]) ? $commerce_pado_settings[$instance_id] : 0,
      );
      $form['#submit'][] = 'commerce_pado_field_settings_submit';
      $form['#validate'][] = 'commerce_pado_field_settings_validate';
    }
  }
}

/*
 * Validation handler for extra settings added to the field settings form
 * Ensure that the entity type is set to Commerce Product.
 */
function commerce_pado_field_settings_validate($form, &$form_state) {
  if ($form_state['values']['commerce_pado_on'] == 1 && $form['#field']['type'] == 'entityreference' && $form_state['values']['field']['settings']['target_type'] != 'commerce_product') {
    form_set_error('commerce_pado_settings', 'Since you have enabled <em>Product Add On Settings</em>, the target entity type must be <em>Commerce Product</em>.');
  }
}

/*
 * Submit handler for extra settings added to the field settings form
 */
function commerce_pado_field_settings_submit($form, &$form_state) {
  $commerce_pado_settings = variable_get('commerce_pado_settings', array());
  $commerce_pado_on = $form_state['values']['commerce_pado_on'];
  $instance = isset($form_state['entityreference']['instance']) ? $form_state['entityreference']['instance'] : $form['#instance'];

  // We save the settings keyed by field instance id.
  $commerce_pado_settings[$instance['id']] = $commerce_pado_on;
  variable_set('commerce_pado_settings', $commerce_pado_settings);
}

/*
 * Implements hook_entity_info_alter().
 */
function commerce_pado_entity_info_alter(&$info) {
  $info['commerce_product']['view modes']['commerce_pado'] = array(
    'label' => t('Add On'),
    'custom settings' => TRUE,
  );
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
function commerce_pado_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {

  // If there are no available products currently, do not make any changes to the form.
  if (!empty($form_state['default_product'])) {

    // Get the correct product. If its the first form load, use the default product.
    if (empty($form_state['input']['product_id'])) {
      $product = $form_state['default_product'];
    }
    else {
      $product = commerce_product_load($form_state['input']['product_id']);
    }
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

    // Does the product have an entityreference field with the Add On settings checked?
    $instances = field_info_instances('commerce_product', $product->type);
    $commerce_pado_settings = variable_get('commerce_pado_settings', array());
    $commerce_pado_products = array();

    // Loop through all fields on the product, and check the instance ID to see if
    // it is an entityreference field checked to be used as a product Add On.
    foreach ($instances as $field_name => $field_info) {
      if (!empty($commerce_pado_settings[$field_info['id']]) && $commerce_pado_settings[$field_info['id']] == 1 && !empty($product->{$field_name})) {
        $all_field_info = field_info_field($field_name);

        // If cardinality of the field does not equal one, loop through the values of the field and put the products in the array.
        if ($all_field_info['cardinality'] != 1) {
          foreach ($product_wrapper->{$field_name} as $field_value_wrapper) {
            $product_id = $field_value_wrapper->product_id
              ->value();
            $commerce_pado_products[$product_id] = $field_value_wrapper
              ->value();
          }
        }
        else {
          $product_id = $product_wrapper->{$field_name}->product_id
            ->value();
          $commerce_pado_products[$product_id] = $product_wrapper->{$field_name}
            ->value();
        }
        $form_state['commerce_pado_products'] = $commerce_pado_products;

        // Add an additional submit handler to execute
        if (!in_array('commerce_pado_add_to_cart_submit', $form['#submit'])) {
          $form['#submit'][] = 'commerce_pado_add_to_cart_submit';
        }
      }
    }
    global $language;
    foreach ($commerce_pado_products as $product_add_on) {

      // Get the renderable array for the product
      if ($product_add_on->status == 1) {
        $renderable_product = entity_view('commerce_product', array(
          $product_add_on->product_id => $product_add_on,
        ), 'commerce_pado', $language->language, TRUE);
        $form[$product_add_on->sku] = array(
          '#type' => 'checkbox',
          '#title' => drupal_render($renderable_product),
        );
      }
    }
  }
}

/*
 * Submit handler added to the add to cart form for commerce_pado.
 */
function commerce_pado_add_to_cart_submit($form, &$form_state) {
  $commerce_pado_products = $form_state['commerce_pado_products'];
  $quantity = $form_state['values']['quantity'];
  foreach ($commerce_pado_products as $product_add_on) {

    // If the product was checked in the add to cart form...
    if ($form_state['values'][$product_add_on->sku] == 1) {

      // Trigger the rule event
      rules_invoke_event('commerce_pado_add_to_cart', $product_add_on, $quantity);
    }
  }
}