You are here

function basic_cart_update_7202 in Basic cart 7.3

Adding the latest fields, in case they aren't already there.

File

./basic_cart.install, line 238
Basic cart install file

Code

function basic_cart_update_7202() {

  // Adding the add to cart and/or price field.
  foreach (basic_cart_get_fields() as $field_name => $field_) {

    // Check to see if the field field already exists.
    $field = field_info_field($field_name);

    // If the field does not exist then create it.
    if (empty($field)) {
      $field = array(
        'field_name' => $field_name,
        'type' => $field_['type'],
        'entity_types' => array(
          'node',
        ),
      );
      field_create_field($field);
    }
  }

  // Getting the available content types.
  $content_types = basic_cart_product_types();

  // Creating the field instances.
  if (!empty($content_types) && is_array($content_types)) {
    foreach ($content_types as $type => $checked) {

      // If a node type is checked, then create the price field.
      if ($checked) {

        // Save content_type as a product.
        $product_types[$type] = $type;
        foreach (basic_cart_get_fields() as $field_name => $field_) {

          // Foreach checked content type, we must assign the price field to the content type.
          $instance = field_info_instance('node', $field_name, $type);
          if (empty($instance)) {
            $instance = array(
              'field_name' => $field_name,
              'label' => $field_['title'],
              'description' => $field_['description'],
              'entity_type' => 'node',
              'bundle' => $type,
            );

            // It doesn't exist. Create it.
            field_create_instance($instance);
          }
        }
      }
      else {
        foreach (basic_cart_get_fields() as $field_name => $field_) {
          $instance = field_info_instance('node', $field_name, $type);
          if (!empty($instance)) {
            field_delete_instance($instance);
          }
        }
      }
    }
  }
}