commerce_discount.install in Commerce Discount 7
Install, update, and uninstall functions for the commerce discount module.
File
commerce_discount.installView source
<?php
/**
* @file
* Install, update, and uninstall functions for the commerce discount module.
*/
/**
* Implements hook_install().
*/
function commerce_discount_install() {
commerce_discount_install_helper();
}
/**
* Implements hook_schema().
*/
function commerce_discount_schema() {
$schema['commerce_discount'] = array(
'description' => 'The base table for discounts.',
'fields' => array(
'discount_id' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'The internal identifier for any discount.',
),
'name' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'The machine name of the discount.',
'default' => '',
),
'label' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The label of the discount.',
'default' => '',
),
'type' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The discount type (bundle).',
'default' => '',
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'Whether the discount is enabled.',
),
'export_status' => array(
'type' => 'int',
'not null' => TRUE,
// Set the default to ENTITY_CUSTOM without using the constant as it is
// not safe to use it at this point.
'default' => 0x1,
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
),
'module' => array(
'description' => 'The name of the providing module if the entity has been defined in code.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
'component_title' => array(
'description' => 'The component price title',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
'sort_order' => array(
'description' => 'The discount sort order.',
'type' => 'int',
'not null' => TRUE,
'default' => 10,
'size' => 'tiny',
),
),
'primary key' => array(
'discount_id',
),
'unique keys' => array(
'name' => array(
'name',
),
),
);
$schema['commerce_discount_offer'] = array(
'description' => 'The base table for discount offers.',
'fields' => array(
'discount_offer_id' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'The internal identifier for any discount offer.',
),
'type' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The discount offer type (bundle).',
'default' => '',
),
),
'primary key' => array(
'discount_offer_id',
),
);
$schema['commerce_discount_usage'] = array(
'fields' => array(
'discount' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Discount name.',
),
'mail' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The email of the customer that used this discount.',
),
'order_id' => array(
'type' => 'int',
'not null' => TRUE,
'description' => 'The order id that this discount was used with.',
),
),
'unique keys' => array(
'discount_mail_order_id' => array(
'discount',
'mail',
'order_id',
),
),
'foreign keys' => array(
'discount' => array(
'table' => 'commerce_discount',
'columns' => array(
'discount' => 'name',
),
),
'order_id' => array(
'table' => 'commerce_order',
'columns' => array(
'order_id' => 'order_id',
),
),
'mail' => array(
'table' => 'users',
'columns' => array(
'mail' => 'mail',
),
),
),
'indexes' => array(
'mail' => array(
'mail',
),
'discount' => array(
'discount',
),
'order_id' => array(
'order_id',
),
),
);
return $schema;
}
/**
* Implements hook_requirements().
*
* Ensure inline_conditions is enabled.
*/
function commerce_discount_requirements($phase) {
$requirements = array();
// Ensure translations don't break during installation.
$t = get_t();
if ($phase == 'runtime') {
if (!module_exists('inline_conditions')) {
$requirements['inline_conditions'] = array(
'title' => $t('Inline Conditions'),
'value' => $t('Inline Conditions has to be installed.'),
'severity' => REQUIREMENT_WARNING,
);
}
}
return $requirements;
}
/**
* Helper function to define and create the required fields & instances.
*
* Ensure creation of an entityreference field on commerce discount, referencing
* commerce discount offer.
* The instance will be added for every newly created bundle.
*/
function commerce_discount_install_helper() {
$fields = field_read_fields(array(), array(
'include_inactive' => TRUE,
));
$field_types = field_info_field_types();
// Clear field info cache, so entity reference, inline_conditions and
// commerce_product_reference field types can be used.
if (!isset($field_types['entityreference'], $field_types['inline_conditions'], $field_types['commerce_product_reference'], $field_types['datestamp'])) {
field_info_cache_clear();
// Overwrites variable, because during installation module after clears the
// field info cache the field type 'entityreference' not containing
// in variables. Under this the field, use this field type is not created.
$field_types = field_info_field_types();
}
// Create the discount offer entity reference field for use on all discounts.
if (empty($fields['commerce_discount_offer']) && isset($field_types['entityreference'])) {
$field = array(
'entity_types' => array(
'commerce_discount',
),
'settings' => array(
'handler' => 'base',
'target_type' => 'commerce_discount_offer',
'handler_settings' => array(
// Default to fixed_amount, and others will be selectable from the
// IEF widget.
'target_bundles' => array(
'fixed_amount',
),
),
),
'field_name' => 'commerce_discount_offer',
'type' => 'entityreference',
'locked' => TRUE,
);
field_create_field($field);
}
// Create the discount compatibility strategy field for use on all discounts.
if (empty($fields['commerce_compatibility_strategy'])) {
$field = array(
'type' => 'list_text',
'field_name' => 'commerce_compatibility_strategy',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_discount_compatibility_strategies',
),
);
field_create_field($field);
}
// Create the selected discounts field for use on all discounts.
if (empty($fields['commerce_compatibility_selection'])) {
$field = array(
'type' => 'entityreference',
'field_name' => 'commerce_compatibility_selection',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'locked' => TRUE,
'settings' => array(
'target_type' => 'commerce_discount',
'handler' => 'base',
'handler_settings' => array(
'sort' => array(
'type' => 'property',
'property' => 'label',
'direction' => 'ASC',
),
),
),
);
field_create_field($field);
}
if (empty($fields['commerce_fixed_amount'])) {
// Add price field to the commerce discount offer "fixed_amount" bundle.
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_fixed_amount',
'type' => 'commerce_price',
'locked' => TRUE,
);
field_create_field($field);
}
if (empty($fields['commerce_percentage'])) {
// Add decimal field to the commerce discount offer "percentage" bundle.
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_percentage',
'type' => 'number_decimal',
'locked' => TRUE,
);
field_create_field($field);
}
if (empty($fields['commerce_discounts'])) {
$field = array(
'entity_types' => array(
'commerce_order',
),
'settings' => array(
'handler' => 'base',
'target_type' => 'commerce_discount',
'handler_settings' => array(
'target_bundles' => array(),
),
),
'field_name' => 'commerce_discounts',
'type' => 'entityreference',
'locked' => FALSE,
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
}
if (empty($fields['inline_conditions']) && isset($field_types['inline_conditions'])) {
$field = array(
'entity_types' => array(
'commerce_discount',
),
'field_name' => 'inline_conditions',
'type' => 'inline_conditions',
'instance_settings' => array(
'entity_type' => 'commerce_order',
),
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
}
if (empty($fields['commerce_discount_date'])) {
$field = array(
'entity_types' => array(
'commerce_discount',
),
'settings' => array(
'granularity' => array(
'month' => 'month',
'day' => 'day',
'year' => 'year',
),
'tz_handling' => 'none',
'timezone_db' => '',
'todate' => 'optional',
'handler' => 'base',
'target_type' => 'commerce_discount',
'handler_settings' => array(
'target_bundles' => array(),
),
),
'field_name' => 'commerce_discount_date',
'type' => 'datestamp',
'locked' => TRUE,
);
field_create_field($field);
}
if (empty($fields['discount_usage_per_person'])) {
// Create entity reference field.
$field = array(
'entity_types' => array(
'commerce_discount',
),
'field_name' => 'discount_usage_per_person',
'type' => 'number_integer',
'locked' => TRUE,
);
field_create_field($field);
}
// Discount usage.
if (empty($fields['discount_usage_limit'])) {
// Create entity reference field.
$field = array(
'entity_types' => array(
'commerce_discount',
),
'field_name' => 'discount_usage_limit',
'type' => 'number_integer',
'locked' => TRUE,
);
field_create_field($field);
}
// Create fields for the shipping related offer types.
if (module_exists('commerce_shipping')) {
// Creating the shipping service text field and checkbox for the "Free
// shipping" offer.
if (empty($fields['commerce_free_shipping'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_free_shipping',
'type' => 'text',
'locked' => TRUE,
);
field_create_field($field);
}
if (empty($fields['commerce_free_shipping_strategy'])) {
$field = array(
'type' => 'list_text',
'field_name' => 'commerce_free_shipping_strategy',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_discount_free_shipping_strategies',
),
);
field_create_field($field);
}
// Create the percentage off and shipping service text fields for the "% off
// of shipping" offer.
if (empty($fields['commerce_percent_off_shipping'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_percent_off_shipping',
'type' => 'number_decimal',
'locked' => TRUE,
);
field_create_field($field);
}
if (empty($fields['commerce_percent_off_ship_serv'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_percent_off_ship_serv',
'type' => 'text',
'locked' => TRUE,
);
field_create_field($field);
}
// Create the target and source shipping service text fields for the
// "Shipping service upgrade" offer.
if (empty($fields['commerce_shipping_upgrade_target'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_shipping_upgrade_target',
'type' => 'list_text',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_shipping_service_options_list',
),
);
field_create_field($field);
}
if (empty($fields['commerce_shipping_upgrade_source'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_shipping_upgrade_source',
'type' => 'list_text',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_shipping_service_options_list',
),
);
field_create_field($field);
}
}
// Creating field for free bonus products offer type.
if (empty($fields['commerce_free_products']) && isset($field_types['commerce_product_reference'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_free_products',
'type' => 'commerce_product_reference',
'locked' => TRUE,
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
}
// Make any existing-but-inactive fields active, and clear the cache to get
// instances for those fields.
field_sync_field_status();
field_info_cache_clear();
// Create instances for above fields.
$instances = field_info_instances();
foreach (commerce_discount_types() as $type => $value) {
if (empty($instances['commerce_discount'][$type]['commerce_discount_offer'])) {
$instance = array(
'field_name' => 'commerce_discount_offer',
'entity_type' => 'commerce_discount',
'bundle' => $type,
'label' => t('Discount offer reference'),
'required' => TRUE,
'widget' => array(
'module' => 'inline_entity_form',
'type' => 'inline_entity_form_single',
'weight' => -12,
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount'][$type]['commerce_compatibility_strategy'])) {
$instance = array(
'field_name' => 'commerce_compatibility_strategy',
'label' => t('Compatibility with other discounts'),
'entity_type' => 'commerce_discount',
'bundle' => $type,
'required' => TRUE,
'widget' => array(
'weight' => -10,
'type' => 'options_buttons',
'module' => 'options',
),
'default_value' => array(
0 => array(
'value' => 'any',
),
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount'][$type]['commerce_compatibility_selection'])) {
$instance = array(
'field_name' => 'commerce_compatibility_selection',
'label' => t('Selected discounts'),
'entity_type' => 'commerce_discount',
'bundle' => $type,
'required' => FALSE,
'widget' => array(
'weight' => -9,
'type' => 'entityreference_autocomplete',
'module' => 'entityreference',
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount'][$type]['commerce_discount_date'])) {
$instance = array(
'field_name' => 'commerce_discount_date',
'entity_type' => 'commerce_discount',
'bundle' => $type,
'label' => t('Discount dates'),
'widget' => array(
'module' => 'date',
'type' => 'date_popup',
'weight' => -11,
'settings' => array(
'no_fieldset' => TRUE,
),
),
'settings' => array(
'default_value' => 'blank',
'default_value2' => 'blank',
),
);
field_create_instance($instance);
}
}
if (empty($instances['commerce_discount_offer']['fixed_amount']['commerce_fixed_amount'])) {
$instance = array(
'field_name' => 'commerce_fixed_amount',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'fixed_amount',
'label' => t('Fixed amount'),
'required' => TRUE,
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount_offer']['percentage']['commerce_percentage'])) {
$instance = array(
'field_name' => 'commerce_percentage',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'percentage',
'label' => t('Percentage'),
'settings' => array(
'suffix' => '%',
),
'required' => TRUE,
);
field_create_instance($instance);
}
// Add discount field to all commerce_order bundles.
$order_entity_info = entity_get_info('commerce_order');
foreach (array_keys($order_entity_info['bundles']) as $bundle) {
if (empty($instances['commerce_order'][$bundle]['commerce_discounts'])) {
$instance = array(
'field_name' => 'commerce_discounts',
'entity_type' => 'commerce_order',
'bundle' => $bundle,
'label' => t('Discount reference'),
);
field_create_instance($instance);
}
}
foreach (commerce_discount_types() as $type => $value) {
if (empty($instances['commerce_discount'][$type]['inline_conditions']) && isset($field_types['inline_conditions'])) {
$instance = array(
'field_name' => 'inline_conditions',
'entity_type' => 'commerce_discount',
'bundle' => $type,
'label' => t('!type_label conditions', array(
'!type_label' => $value['label'],
)),
'required' => FALSE,
'settings' => array(
'entity_type' => $value['entity type'],
),
'widget' => array(
'type' => 'inline_conditions',
'weight' => -13,
),
);
field_create_instance($instance);
}
}
foreach (commerce_discount_types() as $type => $info) {
if (empty($instances['commerce_discount'][$type]['discount_usage_per_person'])) {
$instance = array(
'field_name' => 'discount_usage_per_person',
'entity_type' => 'commerce_discount',
'bundle' => $type,
'label' => t('Maximum usage per customer'),
'description' => t('Enter the maximum number of times a specific person (as identified by email) may use this discount. Leave blank for unlimited.'),
'required' => FALSE,
'widget' => array(
'weight' => 100,
),
'settings' => array(
'min' => 0,
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount'][$type]['discount_usage_limit'])) {
$instance = array(
'field_name' => 'discount_usage_limit',
'entity_type' => 'commerce_discount',
'bundle' => $type,
'label' => t('Maximum overall usage'),
'description' => t('Enter the maximum number of times this discount may be used on the site, by anyone. Leave blank for unlimited.'),
'required' => FALSE,
'widget' => array(
'weight' => 100,
),
'settings' => array(
'min' => 0,
),
);
field_create_instance($instance);
}
}
// Create field instances for the shipping related offer types.
if (module_exists('commerce_shipping')) {
// Add the shipping service text field to the "Free shipping" offer.
if (empty($instances['commerce_discount_offer']['free_shipping']['commerce_free_shipping'])) {
$instance = array(
'field_name' => 'commerce_free_shipping',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'free_shipping',
'label' => t('Shipping service'),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount_offer']['free_shipping']['commerce_free_shipping_strategy'])) {
$instance = array(
'field_name' => 'commerce_free_shipping_strategy',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'free_shipping',
'label' => t('Free shipping discount strategy'),
'required' => TRUE,
'widget' => array(
'type' => 'options_buttons',
'module' => 'options',
),
'default_value' => array(
0 => array(
'value' => 'only_selected',
),
),
);
field_create_instance($instance);
}
// Add the percentage off and shipping service text fields to the "% off of
// shipping" offer.
if (empty($instances['commerce_discount_offer']['percent_off_shipping']['commerce_percent_off_shipping'])) {
$instance = array(
'field_name' => 'commerce_percent_off_shipping',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'percent_off_shipping',
'label' => t('Percentage off of shipping'),
'settings' => array(
'suffix' => '%',
),
'required' => TRUE,
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount_offer']['percent_off_shipping']['commerce_percent_off_ship_serv'])) {
$instance = array(
'field_name' => 'commerce_percent_off_ship_serv',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'percent_off_shipping',
'label' => t('Shipping service to take % off shipping'),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
// Add the target and source shipping service text fields to the "Shipping
// service upgrade" offer.
if (empty($instances['commerce_discount_offer']['shipping_upgrade']['commerce_shipping_upgrade_target'])) {
$instance = array(
'field_name' => 'commerce_shipping_upgrade_target',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'shipping_upgrade',
'label' => t('Let customers select this service'),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount_offer']['shipping_upgrade']['commerce_shipping_upgrade_source'])) {
$instance = array(
'field_name' => 'commerce_shipping_upgrade_source',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'shipping_upgrade',
'label' => t('For the same price as this service'),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
if (empty($instances['commerce_discount_offer']['free_products']['commerce_free_products'])) {
$instance = array(
'field_name' => 'commerce_free_products',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'free_products',
'label' => t('Select bonus products'),
'description' => t('Enter a comma-separated list of SKUs. Each product is added to customer order with a quantity of 1.'),
'required' => TRUE,
'widget' => array(
'type' => 'commerce_product_reference_autocomplete',
),
);
field_create_instance($instance);
}
// Update properties of product_discount bundle.
commerce_line_item_configure_line_item_fields(array(
'commerce_discount',
));
}
/**
* Implements hook_uninstall().
*
* Each implementing module adding a bundle needs to take care of deleting
* it, upon uninstall.
*
* @see commerce_discount_type_info()
* @see commerce_discount_offer_type_info()
*/
function commerce_discount_uninstall() {
module_load_include('module', 'commerce');
// Drop all bundles attached on commerce_discount and commerce_discount_offer
// entity types.
field_attach_delete_bundle('commerce_discount', 'order_discount');
field_attach_delete_bundle('commerce_discount', 'product_discount');
field_attach_delete_bundle('commerce_discount_offer', 'fixed_amount');
field_attach_delete_bundle('commerce_discount_offer', 'percentage');
field_attach_delete_bundle('commerce_discount_offer', 'free_shipping');
field_attach_delete_bundle('commerce_discount_offer', 'commerce_percent_off_shipping');
field_attach_delete_bundle('commerce_discount_offer', 'commerce_percent_off_ship_serv');
field_attach_delete_bundle('commerce_discount_offer', 'free_products');
// Delete custom line items types.
field_attach_delete_bundle('commerce_line_item', 'commerce_discount');
field_attach_delete_bundle('commerce_line_item', 'product_discount');
// Then delete the related fields.
commerce_delete_field('commerce_discount_offer');
commerce_delete_field('commerce_fixed_amount');
commerce_delete_field('commerce_percentage');
commerce_delete_field('commerce_discounts');
commerce_delete_field('inline_conditions');
commerce_delete_field('commerce_free_shipping');
commerce_delete_field('commerce_percent_off_shipping');
commerce_delete_field('commerce_percent_off_ship_serv');
commerce_delete_field('commerce_free_products');
commerce_delete_field('commerce_discount_date');
commerce_delete_field('discount_usage_per_person');
commerce_delete_field('discount_usage_limit');
// Delete all rules added by commerce_discount module.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'rules_config')
->propertyCondition('module', 'commerce_discount')
->execute();
if (!empty($result)) {
rules_config_delete(array_keys(reset($result)));
}
// Delete variable settings.
variable_del('commerce_discount_line_item_types');
}
/**
* Update the widget type of field commerce_discount_offer.
*
* @see https://drupal.org/node/1875524
*/
function commerce_discount_update_7101() {
$instances = field_info_instances('commerce_discount');
foreach ($instances as $bundle => $bundle_instances) {
foreach ($bundle_instances as $field_name => $instance) {
if ($field_name == 'commerce_discount_offer') {
$instance['widget']['type'] = 'inline_entity_form_single';
field_update_instance($instance);
}
}
}
}
/**
* Discount component price names.
*
* @link https://drupal.org/node/2034685
*
* Add a new column in commerce_discount table in order to display customized
* component price title for a discount.
*/
function commerce_discount_update_7102() {
$field_schema = array(
'description' => 'The component price title',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
db_add_field('commerce_discount', 'component_title', $field_schema);
}
/**
* Update properties of product_discount bundle.
*/
function commerce_discount_update_7103() {
commerce_line_item_configure_line_item_fields(array(
'commerce_discount',
));
}
/**
* Changes to commerce_discount schema.
*
* @see http://drupal.org/node/1854108
*/
function commerce_discount_update_7104() {
// Rename "enabled" to "status" and "status" to "export_status".
db_change_field('commerce_discount', 'status', 'export_status', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0x1,
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
));
db_change_field('commerce_discount', 'enabled', 'status', array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'Whether the discount is active.',
));
// Rebuild the schema.
drupal_get_complete_schema(TRUE);
}
/**
* Add a sort order column to the Commerce Discount table.
*/
function commerce_discount_update_7105() {
$field_schema = array(
'description' => 'The discount sort order.',
'type' => 'int',
'not null' => TRUE,
'default' => 10,
'size' => 'tiny',
);
db_add_field('commerce_discount', 'sort_order', $field_schema);
return t('Sort order added to the Commerce Discount table.');
}
/**
* Add discount compatibility fields to all discount types.
*/
function commerce_discount_update_7106() {
$fields = field_read_fields(array(), array(
'include_inactive' => TRUE,
));
// Create the discount compatibility strategy field for use on all discounts.
if (empty($fields['commerce_compatibility_strategy'])) {
$field = array(
'type' => 'list_text',
'field_name' => 'commerce_compatibility_strategy',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_discount_compatibility_strategies',
),
);
field_create_field($field);
}
// Create the selected discounts field for use on all discounts.
if (empty($fields['commerce_compatibility_selection'])) {
$field = array(
'type' => 'entityreference',
'field_name' => 'commerce_compatibility_selection',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'locked' => TRUE,
'settings' => array(
'target_type' => 'commerce_discount',
'handler' => 'base',
'handler_settings' => array(
'sort' => array(
'type' => 'property',
'property' => 'label',
'direction' => 'ASC',
),
),
),
);
field_create_field($field);
}
field_sync_field_status();
field_info_cache_clear();
$instances = field_info_instances();
foreach (commerce_discount_types() as $type => $value) {
if (empty($instances['commerce_discount'][$type]['commerce_compatibility_strategy'])) {
$instance = array(
'field_name' => 'commerce_compatibility_strategy',
'label' => t('Compatibility with other discounts'),
'entity_type' => 'commerce_discount',
'bundle' => $type,
'required' => TRUE,
'widget' => array(
'weight' => -10,
'type' => 'options_buttons',
'module' => 'options',
),
'default_value' => array(
0 => array(
'value' => 'any',
),
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount'][$type]['commerce_compatibility_selection'])) {
$instance = array(
'field_name' => 'commerce_compatibility_selection',
'label' => t('Selected discounts'),
'entity_type' => 'commerce_discount',
'bundle' => $type,
'required' => FALSE,
'widget' => array(
'weight' => -9,
'type' => 'entityreference_autocomplete',
'module' => 'entityreference',
),
);
field_create_instance($instance);
}
}
return t('Discount compatibility fields added to all discount types.');
}
/**
* Add fields to support a new "Shipping service upgrade" offer type.
*/
function commerce_discount_update_7107() {
if (module_exists('commerce_shipping')) {
$fields = field_read_fields(array(), array(
'include_inactive' => TRUE,
));
if (empty($fields['commerce_shipping_upgrade_target'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_shipping_upgrade_target',
'type' => 'list_text',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_shipping_service_options_list',
),
);
field_create_field($field);
}
if (empty($fields['commerce_shipping_upgrade_source'])) {
$field = array(
'entity_types' => array(
'commerce_discount_offer',
),
'field_name' => 'commerce_shipping_upgrade_source',
'type' => 'list_text',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_shipping_service_options_list',
),
);
field_create_field($field);
}
field_sync_field_status();
field_info_cache_clear();
$instances = field_info_instances();
if (empty($instances['commerce_discount_offer']['shipping_upgrade']['commerce_shipping_upgrade_target'])) {
$instance = array(
'field_name' => 'commerce_shipping_upgrade_target',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'shipping_upgrade',
'label' => t('Let customers select this service'),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
if (empty($instances['commerce_discount_offer']['shipping_upgrade']['commerce_shipping_upgrade_source'])) {
$instance = array(
'field_name' => 'commerce_shipping_upgrade_source',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'shipping_upgrade',
'label' => t('For the same price as this service'),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
return t('Fields added to support the new "Shipping service upgrade" offer type.');
}
/**
* Add a field to support the concept of "Free or reduced shipping" offers.
*/
function commerce_discount_update_7108() {
if (module_exists('commerce_shipping')) {
$fields = field_read_fields(array(), array(
'include_inactive' => TRUE,
));
if (empty($fields['commerce_free_shipping_strategy'])) {
$field = array(
'type' => 'list_text',
'field_name' => 'commerce_free_shipping_strategy',
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'commerce_discount_free_shipping_strategies',
),
);
field_create_field($field);
}
field_sync_field_status();
field_info_cache_clear();
$instances = field_info_instances();
if (empty($instances['commerce_discount_offer']['free_shipping']['commerce_free_shipping_strategy'])) {
$instance = array(
'field_name' => 'commerce_free_shipping_strategy',
'entity_type' => 'commerce_discount_offer',
'bundle' => 'free_shipping',
'label' => t('Free shipping discount strategy'),
'required' => TRUE,
'widget' => array(
'type' => 'options_buttons',
'module' => 'options',
),
'default_value' => array(
0 => array(
'value' => 'only_selected',
),
),
);
field_create_instance($instance);
}
}
return t('Field added to support the concept of "Free or reduced shipping" offers.');
}
/**
* Check for wrong discount values, before form validation was introduced.
*
* @see https://www.drupal.org/node/2468159#comment-10100064
*/
function commerce_discount_update_7109() {
// Every value below and including '1' should be recalculated.
$query = db_select('field_data_commerce_percentage', 'percent');
$query
->condition('percent.commerce_percentage_value', 1, '<=')
->fields('percent', array(
'commerce_percentage_value',
'entity_id',
));
$result = $query
->execute();
if ($result) {
foreach ($result as $record) {
// We got results, perform the updates.
db_update('field_data_commerce_percentage')
->fields(array(
'commerce_percentage_value' => abs($record->commerce_percentage_value * 100),
))
->condition('entity_id', $record->entity_id, '=')
->execute();
}
}
$text = t('Update 7109 finished. Please check all your percentage based discount settings for correct values after this update!');
$text .= t('Number of percentage values in table "field_data_commerce_percentage" who were updated as a result: @count', array(
'@count' => $result
->rowCount(),
));
return $text;
}
/**
* Enable and update usage and date sub-modules into discount core.
*/
function commerce_discount_update_7110() {
// Fail early if the Date module is not present.
if (!module_exists('date')) {
throw new DrupalUpdateException('The Date module is required.');
}
commerce_discount_install_helper();
$fields = field_read_fields(array(), array(
'include_inactive' => TRUE,
));
if (!db_table_exists('commerce_discount_usage')) {
db_create_table('commerce_discount_usage', drupal_get_schema_unprocessed('commerce_discount', 'commerce_discount_usage'));
}
// Migrate from commerce_discount_max_uses to discount_usage_limit.
foreach (entity_load('commerce_discount') as $discount) {
$wrapper = entity_metadata_wrapper('commerce_discount', $discount);
if ((!isset($wrapper->discount_usage_limit) || $wrapper->discount_usage_limit
->value() == FALSE) && isset($wrapper->commerce_discount_max_uses) && $wrapper->commerce_discount_max_uses
->value()) {
$wrapper->discount_usage_limit = $wrapper->commerce_discount_max_uses
->value();
entity_save('commerce_discount', $discount);
}
}
if (!empty($fields['commerce_discount_max_uses'])) {
field_delete_field('commerce_discount_max_uses');
}
$disable_modules = array(
'commerce_discount_usage',
'commerce_discount_date',
);
db_update('system')
->fields(array(
'status' => 0,
))
->condition('name', $disable_modules)
->condition('type', 'module')
->execute();
field_info_cache_clear();
return t('Discount date and usage sub-modules are now in core.');
}
Functions
Name | Description |
---|---|
commerce_discount_install | Implements hook_install(). |
commerce_discount_install_helper | Helper function to define and create the required fields & instances. |
commerce_discount_requirements | Implements hook_requirements(). |
commerce_discount_schema | Implements hook_schema(). |
commerce_discount_uninstall | Implements hook_uninstall(). |
commerce_discount_update_7101 | Update the widget type of field commerce_discount_offer. |
commerce_discount_update_7102 | Discount component price names. |
commerce_discount_update_7103 | Update properties of product_discount bundle. |
commerce_discount_update_7104 | Changes to commerce_discount schema. |
commerce_discount_update_7105 | Add a sort order column to the Commerce Discount table. |
commerce_discount_update_7106 | Add discount compatibility fields to all discount types. |
commerce_discount_update_7107 | Add fields to support a new "Shipping service upgrade" offer type. |
commerce_discount_update_7108 | Add a field to support the concept of "Free or reduced shipping" offers. |
commerce_discount_update_7109 | Check for wrong discount values, before form validation was introduced. |
commerce_discount_update_7110 | Enable and update usage and date sub-modules into discount core. |