Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/**
* @file
* Main zayataroma module.
*/
/**
* Implements hook_menu().
* @return mixed
*/
function zayataroma_menu() {
// Test page.
$items['zayataroma-some-page'] = array(
'page callback' => 'zayataroma_some_page',
'access arguments' => array('access content'),
);
$items['admin/commerce/products/sku-list'] = array(
'title' => t('Products administrative grid'),
'page callback' => 'zayataroma_administrative_product_list_page',
'access arguments' => array('administer commerce_product entities'),
);
$items['admin/commerce/orders/%commerce_order/customer-information'] = array(
'title' => 'Customer',
'page callback' => 'zayataroma_customer_information_page',
'page arguments' => array(3),
'access callback' => 'commerce_order_access',
'access arguments' => array('update', 3),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/commerce/orders/%commerce_order/discounts'] = array(
'title' => 'Discounts',
'page callback' => 'zayataroma_discounts_page',
'page arguments' => array(3),
'access callback' => 'commerce_order_access',
'access arguments' => array('update', 3),
'type' => MENU_LOCAL_TASK,
'weight' => 11,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['zayataroma-products/commerce_product/autocomplete'] = array(
'title' => 'commerce_product autocomplete',
'page callback' => 'zayataroma_product_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/commerce/config/orders-auto-increment'] = array(
'title' => t('Auto incrementation'),
'page callback' => 'zayataroma_auto_incrementation_settings_page',
'access arguments' => array('administer commerce_product entities'),
);
return $items;
}
/**
* Page callback for admin/commerce/config/orders-auto-increment.
* @return string
*/
function zayataroma_auto_incrementation_settings_page() {
$form = drupal_get_form('zayataroma_auto_incrementation_settings_form');
return drupal_render($form);
}
/**
* Form for zayataroma_auto_incrementation_settings_page.
* @param $form
* @param $form_state
* @return mixed
*/
function zayataroma_auto_incrementation_settings_form($form, &$form_state) {
$form['auto-increment-id'] = array(
'#type' => 'textfield',
'#title' => t('Next invoice number:'),
'#default_value' => db_query("SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'zayataro_drpl1' AND TABLE_NAME = 'commerce_order';")->fetchField(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Submit handler for zayataroma_auto_incrementation_settings_form.
* @param $form
* @param $form_state
*/
function zayataroma_auto_incrementation_settings_form_submit($form, &$form_state) {
if (!empty($form_state['values']['auto-increment-id'])) {
if (is_numeric($form_state['values']['auto-increment-id'])) {
db_query("ALTER TABLE {commerce_order} AUTO_INCREMENT = " . (int)$form_state['values']['auto-increment-id'].";");
}
}
}
/**
* Implements hook_commerce_order_presave().
* Before saving,
* set the order origin and and net-30 value if the order status is cart and new.
* @param $order
*/
function zayataroma_commerce_order_presave($order) {
// If status of order is shopping cart (Panier) and new.
if ($order->status == 'cart' && $order->is_new == true) {
global $user;
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->field_order_origin->set('web');
if (!empty($user->uid)) {
$account = user_load($user->uid);
if (isset($account->data['net-30'])) {
$order->data['net-30'] = $account->data['net-30'];
}
}
}
}
/**
* Implements hook_menu_alter().
* @param $items
*/
function zayataroma_menu_alter(&$items) {
if (!empty($items['admin/commerce/orders/%commerce_order/edit'])) {
$items['admin/commerce/orders/%commerce_order/edit']['page callback'] = 'zayataroma_order_edit_page';
}
if (!empty($items['admin/config'])) {
$items['admin/config']['access arguments'][0] = 'administer site configuration';
}
if (!empty($items['admin/people/permissions/roles'])) {
$items['admin/people/permissions/roles']['access arguments'][0] = 'administer site configuration';
}
if (!empty($items['admin/people/permissions'])) {
$items['admin/people/permissions']['access arguments'][0] = 'administer site configuration';
}
if (!empty($items['admin/structure'])) {
$items['admin/structure']['access arguments'][0] = 'administer site configuration';
}
// Disable those menus because addressbook module does not work well atm.
if (!empty($items['user/%user/addressbook'])) {
unset($items['user/%user/addressbook']);
}
foreach (commerce_customer_profile_types() as $type => $profile_type) {
if (!empty($items['user/%user/addressbook/' . $type])) {
unset($items['user/%user/addressbook/' . $type]);
}
if (!empty($items['user/%user/addressbook/' . $type . '/create'])) {
unset($items['user/%user/addressbook/' . $type . '/create']);
}
if (!empty($items['user/%user/addressbook/' . $type . '/edit/%commerce_customer_profile'])) {
unset($items['user/%user/addressbook/' . $type . '/edit/%commerce_customer_profile']);
}
if (!empty($items['user/%user/addressbook/' . $type . '/default/%commerce_customer_profile'])) {
unset($items['user/%user/addressbook/' . $type . '/default/%commerce_customer_profile']);
}
if (!empty($items['user/%user/addressbook/' . $type . '/delete/%commerce_customer_profile'])) {
unset($items['user/%user/addressbook/' . $type . '/delete/%commerce_customer_profile']);
}
}
$items['node/%node']['title callback'] = 'zayataroma_page_title';
}
/**
* Implements hook_cron().
*/
function zayataroma_cron(){
destroy('/home/zayataroma/');
}
/**
* Implements hook_init().
*/
function zayataroma_init(){
drupal_add_js(drupal_get_path('module','zayataroma') . '/js/jolly-menu-toggler-fix.js');
drupal_add_js(drupal_get_path('module','zayataroma') . '/js/jquery.selector.focusable.js');
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && arg(3) == '') { #redirect taxonomy terms
$term = taxonomy_term_load((int)arg(2));
if ($term->vid !== '3') {
drupal_goto('<front>');
}
}
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
// Redirect client nodes.
$type = db_query("select type from node where nid = :nid", array(':nid' => (int)arg(1)))->fetchField();
if ($type == 'client') {
drupal_goto('<front>');
}
if ($type == 'oils' || $type == 'bottles' || $type == 'product_display') {
drupal_add_js(drupal_get_path('module','zayataroma') . '/js/node-product-fix-tabs-on-touch-devices.js');
drupal_add_js(drupal_get_path('module','zayataroma') . '/js/dexp-accordions-fix-for-touch-devices.js');
}
$category_pages_nids = array('124','125','131','132','133','134');
if ($type == 'page' && in_array(arg(1),$category_pages_nids)) {
drupal_add_js(drupal_get_path('module','zayataroma') . '/js/dexp-accordions-fix-for-touch-devices.js');
}
}
if (arg(0) == 'admin' && arg(1) == 'commerce' && arg(2) == '') {
drupal_goto('admin/commerce/orders');
}
// Sets the number of hearts for Top 100 field.
$number_hearts = zayataroma_count_hearts();
variable_set('hearts_count', $number_hearts);
}
/**
* Page callback for zayataroma-some-page
* @return string
*/
function zayataroma_some_page(){
$output = " ";
return $output;
}
/**
* Page callback for admin/commerce/orders/%commerce_order/edit
* @param $order
* @return array|mixed
*/
function zayataroma_order_edit_page($order) {
// Include the forms file from the Order module.
module_load_include('inc', 'commerce_order', 'includes/commerce_order.forms');
$form = drupal_get_form('commerce_order_ui_order_form', $order);
unset($form['commerce_coupons']);
// unset($form['additional_settings']);
unset($form['commerce_discounts']);
unset($form['field_order_origin']);
if ($order->uid) {
$account = user_load($order->uid);
if (!empty($account->field_note_administrative[LANGUAGE_NONE][0]['value'])) {
drupal_set_message($account->field_note_administrative[LANGUAGE_NONE][0]['value']);
}
}
return $form;
}
function zayataroma_discounts_page($order) {
// Include the forms file from the Order module.
module_load_include('inc', 'commerce_order', 'includes/commerce_order.forms');
$form = drupal_get_form('commerce_order_ui_order_form', $order);
unset($form['commerce_customer_billing']);
unset($form['commerce_customer_shipping']);
unset($form['commerce_line_items']);
unset($form['additional_settings']);
unset($form['field_order_origin']);
if ($order->uid) {
$account = user_load($order->uid);
if (!empty($account->field_note_administrative[LANGUAGE_NONE][0]['value'])) {
drupal_set_message($account->field_note_administrative[LANGUAGE_NONE][0]['value']);
}
}
return $form;
}
/**
* Page callback for admin/commerce/orders/%commerce_order/customer-information
* @param $order
* @return array|mixed
*/
function zayataroma_customer_information_page($order) {
// Include the forms file from the Order module.
module_load_include('inc', 'commerce_order', 'includes/commerce_order.forms');
$form = drupal_get_form('commerce_order_ui_order_form', $order);
unset($form['commerce_line_items']);
unset($form['commerce_discounts']);
unset($form['additional_settings']);
unset($form['field_order_origin']);
unset($form['commerce_coupons']);
if ($order->uid) {
$account = user_load($order->uid);
if (!empty($account->field_note_administrative[LANGUAGE_NONE][0]['value'])) {
drupal_set_message($account->field_note_administrative[LANGUAGE_NONE][0]['value']);
}
}
return $form;
}
/**
* Implements hook_commerce_currency_info_alter().
* @param $currencies
*/
function zayataroma_commerce_currency_info_alter(&$currencies) {
$currencies['CAD']['code_placement'] = '';
$currencies['CAD']['symbol'] = '$';
$currencies['CAD']['symbol_spacer'] = '';
$currencies['CAD']['symbol_placement'] = 'after';
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
*/
function zayataroma_form_comment_form_alter(&$form) {
// For anonymous users.
$form['author']['name']['#default_value'] = t('Your name');
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_client_node_form_alter(&$form, $form_state) {
unset($form['options']['promote']);
unset($form['options']['sticky']);
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_page_node_form_alter(&$form, $form_state) {
unset($form['options']['promote']);
unset($form['options']['sticky']);
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_article_node_form_alter(&$form, $form_state) {
unset($form['options']['promote']);
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_bottles_node_form_alter(&$form, $form_state) {
$form['actions']['submit']['#submit'][] = 'zayataroma_commerce_product_names_translate';
unset($form['options']['promote']);
unset($form['options']['sticky']);
$form['#attached']['js'][] = drupal_get_path('module','zayataroma') . '/js/ief-node-product-form-hide.js';
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_oils_node_form_alter(&$form, $form_state) {
$form['actions']['submit']['#submit'][] = 'zayataroma_commerce_product_names_translate';
unset($form['options']['promote']);
unset($form['options']['sticky']);
$form['#attached']['js'][] = drupal_get_path('module','zayataroma') . '/js/ief-node-product-form-hide.js';
// Display hearts total count.
if (isset($form['field_top_100'])) {
$hearts = variable_get('hearts_count', 0);
$form['field_top_100']['#suffix'] = t('There are a total of :hearts hearts on the website.', array(':hearts' => $hearts));
}
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_product_display_node_form_alter(&$form, $form_state) {
$form['actions']['submit']['#submit'][] = 'zayataroma_commerce_product_names_translate';
unset($form['options']['promote']);
unset($form['options']['sticky']);
$form['#attached']['js'][] = drupal_get_path('module','zayataroma') . '/js/ief-node-product-form-hide.js';
}
/**
* Submit handler for bottles_node_form, oils_node_form and display_node_form.
* @param $form
* @param $form_state
*/
function zayataroma_commerce_product_names_translate($form, &$form_state) {
// Get node entity wrapper
$node = $form_state['build_info']['args'][0];
$wrapper = entity_metadata_wrapper('node', $node);
// if(!empty($node->translation['status'])){
if (!empty($node->field_product[LANGUAGE_NONE])) {
$translation = array('translate' => 0,'status' => 1,'language' => 'en','source' => 'fr');
foreach ($node->field_product[LANGUAGE_NONE] as $key => $product_id_array) {
$product = commerce_product_load($product_id_array['product_id']);
$product->title_field['fr'][0]['value'] = $wrapper->language('fr')->title_field->value();
$product->title_field['en'][0]['value'] = $wrapper->language('en')->title_field->value();
$attributes = ief_find_product_attributes($product->type);
if (!empty($attributes)) {
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
$attribute_values = array();
foreach ($attributes as $field_name => $attribute) {
$attribute_label = $product_wrapper->{$field_name}->label();
if (!empty($attribute_label)) {
$attribute_values[] = $attribute_label;
}
}
if (!empty($attribute_values)) {
$product->title_field['fr'][0]['value'] .= ' (' . implode(', ', $attribute_values) . ')';
$product->title_field['en'][0]['value'] .= ' (' . implode(', ', $attribute_values) . ')';
}
}
$product->title = $product->title_field['fr'][0]['value'];
$product_handler = entity_translation_get_handler('commerce_product', $product);
$product_handler->setTranslation($translation, $product);
commerce_product_save($product);
}
}
//}
}
/**
* Implements hook_form_FORM_ID_alter().
* On page admin/commerce/orders/%commerce_order/payment
* @param $form
* @param $form_state
*/
function zayataroma_form_commerce_payment_order_transaction_add_form_alter(&$form, $form_state) {
// Allow translation.
if (!empty($form['payment_method']['#options']) && is_array($form['payment_method']['#options'])) {
$options = $form['payment_method']['#options'];
foreach($options as $k => $v) {
$form['payment_method']['#options'][$k] = t($v);
}
}
// Display administrative note on top of the page.
if ($form_state['order']->uid) {
$account = user_load($form_state['order']->uid);
if (!empty($account->field_note_administrative[LANGUAGE_NONE][0]['value'])) {
drupal_set_message($account->field_note_administrative[LANGUAGE_NONE][0]['value']);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
* When adding to cart on product pages.
* @param $form
* @param $form_state
*/
function zayataroma_form_commerce_cart_add_to_cart_form_alter(&$form, $form_state) {
if (!empty($form['attributes']['field_format']['#options'])) {
$over_5kg_formats = array('5K','10K','20K','180K');
$nid = $form_state['context']['entity_id'];
foreach ($form['attributes']['field_format']['#options'] as $k => $v) {
$product_id = db_query("
SELECT fp.field_product_product_id
FROM {field_data_field_product} fp, {field_data_field_format} ff
WHERE
ff.field_format_value LIKE :format
AND fp.entity_id = :nid
AND fp.field_product_product_id = ff.entity_id
", array(':nid' => $nid, ':format' => $k))->fetchField();
$product = commerce_product_load($product_id);
$view = entity_view('commerce_product', array($product));
$format_str = $form['attributes']['field_format']['#options'][$k];
$form['attributes']['field_format']['#options'][$k] = '<span>' . $form['attributes']['field_format']['#options'][$k] . '</span>';
$form['attributes']['field_format']['#options'][$k] .= render($view['commerce_product'][$product_id]['commerce_price']);
if (in_array($k, $over_5kg_formats)) {
$divider = preg_replace("/[^0-9]/", "", $k);
$over_5kg_format_price = commerce_currency_format(($product->commerce_price[LANGUAGE_NONE][0]['amount']/$divider), 'CAD') . ' ' . t('per kg');
$form['attributes']['field_format']['#options'][$k] .= '<div class="zayataroma-field-price-per-kg">' . $over_5kg_format_price . '</div>';
}
}
}
if (substr($form['#action'], 0, 4) == '/en/' && $form['submit']['#value'] == 'Ajouter au panier') {
$form['submit']['#value'] = 'Add to cart';
}
}
/**
* Implements hook_form_FORM_ID_alter().
* On admin/commerce/orders/add page.
* @param $form
* @param $form_state
* @param $form_id
*/
function zayataroma_form_commerce_admin_order_advanced_search_customers_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['customer']['billing_profile']['#options'])) {
$form['customer']['billing_profile']['#options'] = array_map('unserialize', array_unique(array_map('serialize', $form['customer']['billing_profile']['#options'])));
}
if (!empty($form['customer']['shipping_profile']['#options'])) {
$form['customer']['shipping_profile']['#options'] = array_map('unserialize', array_unique(array_map('serialize', $form['customer']['shipping_profile']['#options'])));
}
}
/**
* Implements hook_form_FORM_ID_alter().
* On admin/commerce/config/checkout/form/pane pages.
* @param $form
* @param $form_state
* @param $form_id
*/
function zayataroma_form_commerce_checkout_pane_settings_form_alter(&$form, &$form_state, $form_id) {
$form['settings']['container']['commerce_checkout_completion_message']['#wysiwyg'] = 0;
}
/**
* Implements hook_form_FORM_ID_alter().
* On admin/structure/block/manage/block/%block/configure pages
* @param $form
* @param $form_state
* @param $form_id
*/
function zayataroma_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
$form['settings']['body_field']['body']['#wysiwyg'] = 0;
}
/**
* Implements hook_form_FORM_ID_alter().
* On page admin/commerce/orders/%order_id/edit
* @param $form
* @param $form_state
* @param $form_id
*/
function zayataroma_form_commerce_order_ui_order_form_alter(&$form, &$form_state, $form_id) {
// Add custom validation.
if (!empty($form['actions']['submit']['#validate'])) {
$form['actions']['submit']['#validate'][] = '_zayataroma_form_commerce_order_ui_order_form_validate';
} else {
$form['actions']['submit']['#validate'] = array('_zayataroma_form_commerce_order_ui_order_form_validate');
}
// Disable default submit handler.
foreach($form['actions']['submit']['#submit'] as $k => $v) {
if ($v == 'commerce_order_ui_order_form_submit') {
unset($form['actions']['submit']['#submit'][$k]);
}
}
$form['actions']['submit']['#submit'][] = 'zayataroma_order_form_submit';
if (strstr($form['#action'], 'edit')) {
// Remove unwanted line item types from available product bundles.
$allowed_product_types = array('product', 'shipping');
if (isset($form['commerce_line_items'][LANGUAGE_NONE]['actions']['bundle']['#options'])) {
foreach($form['commerce_line_items'][LANGUAGE_NONE]['actions']['bundle']['#options'] as $k => $v) {
if (!in_array($k, $allowed_product_types)) {
unset($form['commerce_line_items'][LANGUAGE_NONE]['actions']['bundle']['#options'][$k]);
}
}
}
$form['order_terms'] = array(
'#type' => 'fieldset',
'#title' => t('Order Terms'),
'#weight' => 21,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'additional_settings',
);
// Net 30 days.
if (isset($form_state['build_info']['args'][0]->data['net-30'])) {
$net30_default = $form_state['build_info']['args'][0]->data['net-30'];
} else {
$net30_default = 0;
}
$form['order_terms']['net-30'] = array(
'#type' => 'checkbox',
'#title' => t('NET 30 days'),
'#default_value' => $net30_default,
);
// Invoice validated (or not).
if (isset($form_state['build_info']['args'][0]->data['invoice-validated'])) {
$inv_validated_default = $form_state['build_info']['args'][0]->data['invoice-validated'];
} else {
$inv_validated_default = 0;
}
$form['order_terms']['invoice-validated'] = array(
'#type' => 'checkbox',
'#title' => t('Invoice validated'),
'#default_value' => $inv_validated_default,
);
// 3 checkboxes on bottom of the page.
$form['actions']['recalculate-pricing-rules'] = array(
'#type' => 'checkbox',
'#title' => t('Recalculate all pricing rules'),
'#default_value' => 0,
);
$form['actions']['recalculate-tax-only'] = array(
'#type' => 'checkbox',
'#title' => t('Recalculate tax only'),
'#default_value' => 0,
);
$form['actions']['apply-checkout-completion-rules'] = array(
'#type' => 'checkbox',
'#title' => t('Apply checkout completion rules'),
'#default_value' => 0,
);
$form['#attached']['js'][] = drupal_get_path('module','zayataroma') . '/js/ief-order-ui-hide.js';
// Hiding billing and shipping forms.
if (!empty($form['commerce_customer_billing'])) {
unset($form['commerce_customer_billing']);
}
if (!empty($form['commerce_customer_shipping'])) {
unset($form['commerce_customer_shipping']);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_commerce_checkout_form_review_alter(&$form, $form_state) {
// Show a warning regarding shipping fees for orders that are shipping outside Canada & Continental USA
if (isset($form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'])) {
$show_warning = 1;
$profile = commerce_customer_profile_load($form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id']);
$country = $profile->commerce_customer_address[LANGUAGE_NONE][0]['country'];
$state = $profile->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'];
if ($country == 'CA') {
$show_warning = 0;
} elseif($country == 'US') {
$continental_states = array('AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT',
'DC', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA',
'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ',
'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN',
'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY');
if (in_array($state, $continental_states)) {
$show_warning = 0;
}
}
if ($show_warning) {
drupal_set_message(t('Additional delivery fees may apply depending on your location and the weight of your package. In this case, we will
contact you for approval of the final invoice. Please note that you are responsible for any customs fees.'));
}
}
// Display message for pickup
if (!empty($form_state['order']->data['shipping_method'])) {
if ($form_state['order']->data['shipping_method'] == 'pickup') {
drupal_set_message(t('Please Note: We will confirm you order and contact you when your order is ready at our boutique<br> Pickup Times :<br> Monday to Friday - 9:00 AM to 5:00 PM <br> Saturday and Sunday - 11:00 AM to 5:00 PM'));
}
}
$form['#attached']['js'][] = drupal_get_path('module','zayataroma').'/js/admin-commerce-checkout-disable-continue-btn-during-ajax-requests.js';
}
/**
* Page callback for admin/commerce/products/sku-list.
* @return string
*/
function zayataroma_administrative_product_list_page() {
drupal_add_js(drupal_get_path('module','zayataroma') . '/js/za-administrative-product-list.js');
if (!empty($_GET['select'])) {
$selected_value = $_GET['select'];
} else {
// Default value if not value selected.
$selected_value = '[A]';
}
$select = '
<select id="administrative-product-list-select" class="form-select">
<option value="[A]"'.(($selected_value=='[A]')?' selected':'').'>'.t('Product names starting with').' A</option>
<option value="[B]"'.(($selected_value=='[B]')?' selected':'').'>'.t('Product names starting with').' B</option>
<option value="C[AH]"'.(($selected_value=='C[AH]')?' selected':'').'>'.t('Product names starting with').' CA-CH</option>
<option value="C[IZ]"'.(($selected_value=='C[IZ]')?' selected':'').'>'.t('Product names starting with').' CI-CZ</option>
<option value="[DEF]"'.(($selected_value=='[DEF]')?' selected':'').'>'.t('Product names starting with').' D, E, F</option>
<option value="[G]"'.(($selected_value=='[G]')?' selected':'').'>'.t('Product names starting with').' G</option>
<option value="[HIJ]"'.(($selected_value=='[HIJ]')?' selected':'').'>'.t('Product names starting with').' H, I, J</option>
<option value="[KL]"'.(($selected_value=='[KL]')?' selected':'').'>'.t('Product names starting with').' K, L</option>
<option value="M[AD]"'.(($selected_value=='M[AD]')?' selected':'').'>'.t('Product names starting with').' MA-MD</option>
<option value="M[EH]"'.(($selected_value=='M[EH]')?' selected':'').'>'.t('Product names starting with').' ME-MH</option>
<option value="M[IZ]"'.(($selected_value=='M[IZ]')?' selected':'').'>'.t('Product names starting with').' MI-MZ</option>
<option value="[N]"'.(($selected_value=='[N]')?' selected':'').'>'.t('Product names starting with').' N</option>
<option value="[O]"'.(($selected_value=='[O]')?' selected':'').'>'.t('Product names starting with').' O</option>
<option value="[P]"'.(($selected_value=='[P]')?' selected':'').'>'.t('Product names starting with').' P</option>
<option value="[Q]"'.(($selected_value=='[Q]')?' selected':'').'>'.t('Product names starting with').' Q</option>
<option value="[R]"'.(($selected_value=='[R]')?' selected':'').'>'.t('Product names starting with').' R</option>
<option value="[S]"'.(($selected_value=='[S]')?' selected':'').'>'.t('Product names starting with').' S</option>
<option value="[TU]"'.(($selected_value=='[TU]')?' selected':'').'>'.t('Product names starting with').' T, U</option>
<option value="[VW]"'.(($selected_value=='[VW]')?' selected':'').'>'.t('Product names starting with').' V, W</option>
<option value="[XYZ]"'.(($selected_value=='[XYZ]')?' selected':'').'>'.t('Product names starting with').' X, Y, Z</option>
</select>
';
$form = drupal_get_form('zayataroma_administrative_product_list_form', $selected_value);
return $select . drupal_render($form) . '
<style>
.ajax-progress-throbber {
position: absolute;
left: 0;
top: 0;
}
body.page-admin-commerce-products-sku-list table.sticky-table tbody tr td {
position: relative;
}
span.reveal-price:hover {
cursor: pointer;
}
img.status-clickable {
cursor: pointer;
}
</style>
';
}
/**
* Form for zayataroma_administrative_product_list_page.
* @param $form
* @param $form_state
* @param $selected_value
* @return mixed
*/
function zayataroma_administrative_product_list_form($form, &$form_state, $selected_value) {
global $language;
// https://passingcuriosity.com/2011/drupal-7-forms-tables/
$header = array(t('Title'), 'SKU', t('Price'), t('Statut'));
$form['administrative-product-list'] = array(
'#prefix' => '<div id="administrative-product-list">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#theme' => 'table',
'#header' => $header,
'#rows' => array(),
);
$rows = array(); // Useless ?
$results = db_query("
SELECT cp.product_id, tf.title_field_value AS title, cp.sku, fdcp.commerce_price_amount AS amount, cp.status
FROM commerce_product cp, field_data_title_field tf, field_data_commerce_price fdcp
WHERE
cp.product_id = tf.entity_id
AND tf.bundle = 'product'
AND tf.language = '" . $language->language . "'
AND cp.product_id = fdcp.entity_id
AND fdcp.bundle = 'product'
AND tf.title_field_value REGEXP '^" . $selected_value . ".*$'
ORDER BY tf.title_field_value
");
$i = 0;
foreach($results as $row){
$title = array(
'#markup' => $row->title,
);
$sku = array(
'#markup' => $row->sku,
);
$amount = array(
'#type' => 'container',
'#tree' => 1,
'#prefix' => '<div id="amount-' . $row->product_id . '"><span class="reveal-price" data-product-id="' . $row->product_id . '">' . number_format(($row->amount/100), 2, '.', '').'</span>',
'#suffix' => '
</div>',
'amount_txt' => array(
'#type' => 'textfield',
'#size '=> '5',
'#default_value' => number_format(($row->amount/100), 2, '.', ''),
'#attributes' => array(
'style' => 'float: left;margin: 0 5px 0 0;display:none;',
),
),
// Submit button of amount column.
'amount_sbt' => array(
'#name' => 'amount-sbt-' . $row->product_id,
'#type' => 'submit',
'#value' => 'ok',
'#attributes' => array(
'style' => 'margin: 0;padding: 5px;display:none;',
'data-row' => $i,
),
'#ajax' => array(
'callback' => 'zayataroma_administrative_product_list_price_callback',
'wrapper' => 'amount-'.$row->product_id.'',
),
),
);
if ($row->status) {
$status_str = '<img width="32" src="/sites/zayataroma.com/files/enable-on.png"/>&nbsp;&nbsp;&nbsp;<img width="32" src="/sites/zayataroma.com/files/disable-off.png" class="status-clickable" data-product-id="'.$row->product_id.'"/>';
} else {
$status_str = '<img width="32" src="/sites/zayataroma.com/files/enable-off.png" class="status-clickable" data-product-id="'.$row->product_id.'"/>&nbsp;&nbsp;&nbsp;<img width="32" src="/sites/zayataroma.com/files/disable-on.png"/>';
}
$status = array(
'#type' => 'container',
'#tree' => 1,
'#prefix' => '<div id="status-' . $row->product_id . '">' . $status_str,
'#suffix' => '</div>',
'status_change' => array(
'#name' => 'status-' . $row->product_id,
'#type' => 'submit',
'#value' => 'change',
'#attributes' => array(
'style' => 'display: none;',
'data-row' => $i,
),
'#ajax' => array(
'callback' => 'zayataroma_administrative_product_list_status_callback',
'wrapper' => 'status-'.$row->product_id.'',
),
),
);
$form['administrative-product-list'][$i] = array(
'title' => &$title,
'sku' => &$sku,
'amount' => &$amount,
'status' => &$status,
);
$form['administrative-product-list']['#rows'][$i] = array(
array('data' => &$title),
array('data' => &$sku),
array('data' => &$amount),
array('data' => &$status),
);
unset($title);
unset($sku);
unset($amount);
unset($status);
$i++;
}
$form['administrative-product-list']['#prefix'] = '<pre>'.$i.' '.t('products').'</pre><div id="administrative-product-list">';
return $form;
}
/**
* Ajax callback for product status.
* @param $form
* @param $form_state
* @return mixed
*/
function zayataroma_administrative_product_list_status_callback($form, $form_state) {
$row_id = $form_state['clicked_button']['#attributes']['data-row'];
$product_id = str_replace('status-', '', $form_state['clicked_button']['#name']);
$product = commerce_product_load($product_id);
if ($product->status) {
$product->status = 0;
$status_str = '<img width="32" src="/sites/zayataroma.com/files/enable-off.png" class="status-clickable" data-product-id="' . $product_id . '"/>&nbsp;&nbsp;&nbsp;<img width="32" src="/sites/zayataroma.com/files/disable-on.png"/>';
} else {
$product->status = 1;
$status_str = '<img width="32" src="/sites/zayataroma.com/files/enable-on.png"/>&nbsp;&nbsp;&nbsp;<img width="32" src="/sites/zayataroma.com/files/disable-off.png" class="status-clickable" data-product-id="' . $product_id . '"/>';
}
commerce_product_save($product);
$form['administrative-product-list'][$row_id]['status']['#prefix'] = '<div id="status-' . $product_id . '">' . $status_str;
return $form['administrative-product-list'][$row_id]['status'];
}
/**
* Ajax callback for product price.
* @param $form
* @param $form_state
* @return mixed
*/
function zayataroma_administrative_product_list_price_callback($form, $form_state) {
$product_id = str_replace('amount-sbt-', '', $form_state['clicked_button']['#name']);
$row_id = $form_state['clicked_button']['#attributes']['data-row'];
$amount = $form_state['input']['administrative-product-list'][$row_id]['amount']['amount_txt'];
$form['administrative-product-list'][$row_id]['amount']['#prefix'] = '<div id="amount-' . $product_id . '"><span class="reveal-price" data-product-id="' . $product_id . '">' . $amount . '</span>';
$form['administrative-product-list'][$row_id]['amount']['amount_txt']['#value'] = $amount;
$product = commerce_product_load($product_id);
$product->commerce_price[LANGUAGE_NONE][0]['amount'] = ($amount * 100);
commerce_product_save($product);
return $form['administrative-product-list'][$row_id]['amount'];
}
function ief_find_product_attributes($type) {
// Attributes are tied to the commerce_cart module.
if (!module_exists('commerce_cart')) {
return array();
}
$attributes = array();
// Loop through all the field instances on that product type.
foreach (field_info_instances('commerce_product', $type) as $name => $instance) {
// A field qualifies if it is single value, required and uses a widget
// with a definite set of options. For the sake of simplicity, this is
// currently restricted to fields defined by the options module.
$field = field_info_field($instance['field_name']);
// Get the array of Cart settings pertaining to this instance.
$commerce_cart_settings = commerce_cart_field_instance_attribute_settings($instance);
// If the instance is of a field type that is eligible to function as
// a product attribute field and if its attribute field settings
// specify that this functionality is enabled...
if (commerce_cart_field_attribute_eligible($field) && $commerce_cart_settings['attribute_field']) {
$attributes[$name] = array(
'field' => $field,
'instance' => $instance,
'weight' => $instance['widget']['weight'],
);
}
}
// Sort the fields by weight.
uasort($attributes, 'drupal_sort_weight');
return $attributes;
}
/**
* Function used nowhere ?
* @param $commerce_line_item
* @param $provinces
* @return int
*/
function zayataroma_apply_tax_to_province_validation($commerce_line_item, $provinces) {
if (!empty($commerce_line_item->order_id)) {
$order = commerce_order_load($commerce_line_item->order_id);
if (!empty($order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'])) {
$profile = commerce_customer_profile_load($order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id']);
if (!empty($profile->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'])) {
if (in_array($profile->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'], $provinces)) {
return 1;
}
}
}
}
return 0;
}
/**
* Implements hook_element_info_alter().
* @param $type
*/
function zayataroma_element_info_alter(&$type) {
if (!empty($type['addressfield']['#process'])) {
$type['addressfield']['#process'][] = 'zayataroma_addressfield_element_process';
}
}
function zayataroma_addressfield_element_process($element, &$form_state, $form) {
if (isset($element['address']['country'])) {
switch($form_state['build_info']['form_id']){
case'user_register_form':
$element['address']['country']['#attached']['js'] = array(0 => drupal_get_path('module','zayataroma') . '/js/admin-people-unmask-contact-phone.js');
// Adding descriptions to field_billing_register fields only on register form.
$element['address']['street_block']['thoroughfare']['#description'] = t('Street address. Please note that we do not ship to PO boxes.');
$element['address']['street_block']['premise']['#description'] = t('To ensure fast and accurate shipping please include your apartment and buzzer number if applicable.');
$element['address']['locality_block']['postal_code']['#description'] = t('Canadian postal code example: L5T 2G9');
$element['address']['organisation_block']['organisation_name']['#description'] = t('If this is a company account enter your company name, otherwise leave blank.');
break;
case'user_profile_form':
$element['address']['country']['#attached']['js'] = array(0 => drupal_get_path('module','zayataroma') . '/js/admin-people-unmask-contact-phone.js');
break;
case'commerce_order_ui_order_form':
$element['address']['country']['#attached']['js'] = array(0 => drupal_get_path('module','zayataroma') . '/js/admin-commerce-orders-unmask-contact-phone.js');
break;
case'zayataroma_squareup_api_transactions_create_customer_form':
break;
default:
$element['address']['country']['#attached']['js'] = array(0 => drupal_get_path('module','zayataroma') . '/js/frontend-unmask-contact-phone.js');
if (isset($element['address']['phone_block']['fax_number'])) {
$element['address']['phone_block']['fax_number']['#type'] = 'masked_input';
$element['address']['phone_block']['fax_number']['#mask'] = '(999) 999-9999';
}
if (isset($element['address']['phone_block']['mobile_number'])) {
$element['address']['phone_block']['mobile_number']['#type'] = 'masked_input';
$element['address']['phone_block']['mobile_number']['#mask'] = '(999) 999-9999';
}
if (isset($element['address']['phone_block']['phone_number'])) {
$element['address']['phone_block']['phone_number']['phone_number']['#required'] = 1;
$element['address']['phone_block']['phone_number']['phone_number']['#type'] = 'masked_input';
$element['address']['phone_block']['phone_number']['phone_number']['#mask'] = '(999) 999-9999';
}
break;
}
}
$element['address']['phone_block']['phone_number']['phone_number']['#required'] = 1;
$phone_number_required_exluded_forms=array('zayataroma_squareup_api_transactions_create_customer_form','user_profile_form','user_register_form');
if (isset($element['address']['phone_block']['phone_number'])) {
if(user_has_role(4) || user_has_role(3) || user_has_role(10)){
if(in_array($form_state['build_info']['form_id'],$phone_number_required_exluded_forms)) {
$element['address']['phone_block']['phone_number']['phone_number']['#required'] = 0;
}
}
}
if (isset($element['address']['country'])) {
$element['address']['country']['#weight'] = 100;
}
return $element;
}
function zayataroma_field_widget_form_alter(&$element, $form_state, $context) {
if (user_has_role(4) || user_has_role(3) || user_has_role(10)) {
if($form_state['build_info']['form_id']=='user_register_form' || $form_state['build_info']['form_id'] == 'user_profile_form'){
if(!empty($element['#field_name'])){
if($element['#field_name']=='field_billing_register'){
if(isset($element['profiles'][0]['commerce_customer_address']['und'][0]['address']['#context']['instance']['required'])){
$element['profiles'][0]['commerce_customer_address']['und'][0]['address']['#context']['instance']['required']=false;
}
}
}
}
}
}
function zayataroma_commerce_shipping_service_info_alter(&$shipping_services) {
// Use t() to translate the shipping service display.
foreach ($shipping_services as $name => &$shipping_service) {
$shipping_service['display_title'] = t($shipping_service['display_title']);
}
}
/**
* Implements hook_commerce_price_formatted_components_alter().
* @param $components
* @param $price
* @param $entity
*/
function zayataroma_commerce_price_formatted_components_alter(&$components, $price, $entity) {
global $language;
foreach ($components as &$component) {
if ($component['title'] == t('International delivery')) {
$component['weight'] =- 1;
}
if ($component['title'] == t('Delivery in Canada')) {
$component['weight'] =- 1;
}
if ($component['title'] == 'Order total') {
$component['title'] = t($component['title']);
}
if ($component['title'] == 'Discount') {
$component['title'] = t('Discount');
}
if ($component['title'] == 'Free shipping'){
$component['title'] = t('Free shipping');
}
}
if ($entity->type == 'commerce_order') {
$order_wrapper = entity_metadata_wrapper('commerce_order', $entity);
// Add shipping to the $components if missing.
$order_has_shipping_component = false;
foreach($components as $k => $v){
if (strstr($k, 'flat_rate')) {
$order_has_shipping_component = true;
break;
}
}
if (!$order_has_shipping_component) {
$line_items = $order_wrapper->commerce_line_items->value();
foreach ($line_items as $k => $line_item) {
if ($line_item->type == 'shipping') {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$amount = $line_item_wrapper->commerce_unit_price->amount->value();
if (!empty($components['base_price'])) {
$components['base_price']['price']['amount'] -= $amount;
}
$components['shipping'] = array(
'title' => 'Shipping',
'price' => array(
'amount' => $amount,
'currency_code' => 'CAD',
'data' => array(),
),
'weight' => -1,
);
break;
}
}
}
// Set proper label on the shipping price component in the order totals (instead of just "Shipping").
$line_items = $order_wrapper->commerce_line_items->value();
foreach ($line_items as $k=>$line_item) {
if (is_object($line_item)) {
if ($line_item->type == 'shipping') {
$label = $line_item->line_item_label;
break;
}
}
}
if (isset($label) && isset($components['shipping']['title'])) {
$components['shipping']['title'] = $label;
}
}
}
function zayataroma_commerce_addressbook_labels_alter(&$labels, $profiles) {
$result = array();
foreach ($profiles as $profile_id => $profile) {
if ($profile->type == 'shipping' && !empty($profile->commerce_customer_address)) {
if ($profile->commerce_customer_address[LANGUAGE_NONE][0]['locality'] == 'Bromont'
&& $profile->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] == '1339 Shefford'
&& $profile->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] == 'J2L 1C9'){
unset($profiles[$profile_id]);
}
}
}
foreach ($profiles as $profile_id => $profile) {
if (!empty($profile->commerce_customer_address[LANGUAGE_NONE][0]['first_name'])
&& !empty($profile->commerce_customer_address[LANGUAGE_NONE][0]['last_name'])) {
foreach($profile as $id => $field) {
if ($id == 'commerce_customer_address') {
$result[$profile_id]['thoroughfare'] = $field[LANGUAGE_NONE][0]['thoroughfare'];
$result[$profile_id]['locality'] = $field[LANGUAGE_NONE][0]['locality'];
$result[$profile_id]['administrative_area'] = $field[LANGUAGE_NONE][0]['administrative_area'];
$result[$profile_id]['first_name'] = $field[LANGUAGE_NONE][0]['first_name'];
$result[$profile_id]['last_name'] = $field[LANGUAGE_NONE][0]['last_name'];
$result[$profile_id]['company'] = (!empty($field[LANGUAGE_NONE][0]['company']) ? $field[LANGUAGE_NONE][0]['company']:'');
break;
}
}
}
}
$result = array_map('unserialize', array_unique(array_map('serialize', $result)));
$labels = array();
foreach ($result as $profile_id=>$values) {
$labels[$profile_id] = trim($values['first_name'] . ' ' . $values['last_name'] .
' ' . $values['company']) . ' ' . $values['thoroughfare'] . ' ' . $values['locality'] .
' ' . $values['administrative_area'];
}
}
/**
* Validation handler for commerce_order_ui_order_form.
* @param $form
* @param $form_state
*/
function _zayataroma_form_commerce_order_ui_order_form_validate(&$form, &$form_state) {
if (empty($form_state['input']['commerce_customer_billing'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['thoroughfare'])
&& empty($form_state['input']['commerce_customer_shipping'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['thoroughfare'])) {
$order = commerce_order_load($form_state['build_info']['args'][0]->order_id);
$form_state['values']['commerce_customer_billing'][LANGUAGE_NONE][0]['profile_id'] = $order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'];
if (!empty($order->commerce_customer_shipping[LANGUAGE_NONE])) {
foreach ($order->commerce_customer_shipping[LANGUAGE_NONE] as $k => $v) {
$form_state['values']['commerce_customer_shipping'][LANGUAGE_NONE][$k]['profile_id'] = $v['profile_id'];
}
}
}
}
/**
* Submit handler for commerce_order_ui_order_form.
* @param $form
* @param $form_state
*/
function zayataroma_order_form_submit($form, &$form_state) {
$order = commerce_order_load($form_state['build_info']['args'][0]->order_id);
$order_is_modified = 0;
if (!empty($form_state['values']['apply-checkout-completion-rules'])) {
// That simple.
commerce_checkout_complete($order);
drupal_set_message(t('Checkout completion rules have been executed for the order.'));
}
if (!empty($form_state['values']['recalculate-pricing-rules'])) {
commerce_cart_order_refresh($order);
$has_tax_on_shipping = 0;
foreach ($order->commerce_line_items[LANGUAGE_NONE] as $k => $line_item_ref) { #this is all for tax on shipping...
$line_item = commerce_line_item_load($line_item_ref['line_item_id']);
if (is_object($line_item)) {
if ($line_item->type == 'shipping') {
rules_invoke_event("commerce_shipping_calculate_rate", $line_item);
commerce_line_item_save($line_item);
entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));
$has_tax_on_shipping = 1;
}
}
}
if ($has_tax_on_shipping) {
commerce_order_calculate_total($order);
$order_is_modified = 1;
}
drupal_set_message(t('Pricing rules have been applied and the order updated.'));
} elseif (!empty($form_state['values']['recalculate-tax-only'])) {
module_load_include('inc', 'commerce_tax', 'commerce_tax_rules');
foreach($order->commerce_line_items[LANGUAGE_NONE] as $k => $line_item_ref) {
$line_item = commerce_line_item_load($line_item_ref['line_item_id']);
if ($line_item->type=='shipping') {
rules_invoke_event("commerce_shipping_calculate_rate", $line_item);
} elseif($line_item->type == 'product') {
$tax_types = commerce_tax_types();
foreach ($tax_types as $tax_type) {
commerce_tax_type_calculate_rates($tax_type,$line_item);
}
}
commerce_line_item_save($line_item);
entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));
}
commerce_order_calculate_total($order);
$order_is_modified = 1;
}
if (isset($form_state['values']['net-30'])) {
$order->data['net-30'] = $form_state['values']['net-30'];
$order_is_modified = 1;
}
if (isset($form_state['values']['invoice-validated'])) {
$order->data['invoice-validated'] = $form_state['values']['invoice-validated'];
$order_is_modified = 1;
}
if ($order_is_modified) {
commerce_order_save($order);
}
drupal_set_message(t('Order') . ' #' . $order->order_id . ' ' . t('saved') . '.');
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
* @param $form_id
*/
function zayataroma_form_user_register_form_alter(&$form, &$form_state, $form_id) {
array_unshift($form['#submit'], 'zayataroma_user_form_submit');
$form['#submit'][] = 'zayataroma_user_register_default_customer_profile_form_submit';
if (isset($form['account']['notify'])) {
$form['account']['notify']['#default_value'] = 1;
}
if (user_has_role(4) || user_has_role(3) || user_has_role(10)) {
// Get username of current user logged in and pass it to field_created_by field.
// When adding user from admin/people/create.
global $user;
$name = $user->name;
$form['field_created_by']['und'][0]['value']['#value'] = $name;
$form['field_langue_de_correspondance']['#access'] = FALSE;
$form['field_langue_de_correspondance']['#required'] = FALSE;
$form['order_terms'] = array(
'#type' => 'fieldset',
'#title' => t('Order Terms'),
'#weight' => 21,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if (isset($form_state['user']->data['net-30'])) {
$net30_default=$form_state['user']->data['net-30'];
} else {
$net30_default = 0;
}
$form['order_terms']['net-30'] = array(
'#type' => 'checkbox',
'#title' => t('NET 30 days'),
'#default_value' => $net30_default,
);
}
if (user_has_role(1)) {
$form['field_created_by']['und'][0]['value']['#value'] = "Anonyme";
// Group fields together and add some icons.
$form['account']['#type'] = 'fieldset';
$form['account']['#title'] = t('<img src="http://www.freeiconspng.com/uploads/profile-icon-9.png" width="30px" height="30px">&nbsp;&nbsp;<strong><span style="color:#30829c">Account Credentials</span></strong>');
$form['field_billing_register']['und']['profiles'][0]['#title'] = t('<img src="http://www.plymouthcitycentredentalpractice.co.uk/wp-content/themes/plymouth/images/footer-address-icon-grey.png" width="27px" height="27px"/>&nbsp;&nbsp;<strong><span style="color:#30829c">Billing information</span></strong>');
// Newsletter is the last element before submit element.
// To be removed soon. MailChimp is better.
$form['simplenews']['#weight'] = 50;
}
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
* @param $form_id
*/
function zayataroma_form_user_profile_form_alter(&$form, &$form_state, $form_id){
array_unshift($form['#submit'], 'zayataroma_user_form_submit');
$form['#submit'][] = 'zayataroma_user_profile_default_customer_profile_form_submit';
if (user_has_role(4)|| user_has_role(3) || user_has_role(10)) {
$form['field_langue_de_correspondance']['#access'] = FALSE;
$form['field_langue_de_correspondance']['#required'] = FALSE;
$form['order_terms'] = array(
'#type' => 'fieldset',
'#title' => t('Order Terms'),
'#weight' => 21,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if (isset($form_state['user']->data['net-30'])) {
$net30_default = $form_state['user']->data['net-30'];
} else {
$net30_default = 0;
}
$form['order_terms']['net-30'] = array(
'#type' => 'checkbox',
'#title' => t('NET 30 days'),
'#default_value' => $net30_default,
);
} else {
if (isset($form['field_additional_emails'])) {
unset($form['field_additional_emails']);
}
if (isset($form['field_note_administrative'])) {
unset($form['field_note_administrative']);
}
}
}
/**
* Ensure that uid is set on the profile_id in field_billing_register.
* @param $form
* @param $form_state
*/
function zayataroma_user_profile_default_customer_profile_form_submit($form, &$form_state) {
if (!empty($form_state['user'])) {
$account = $form_state['user'];
if (!empty($account->field_billing_register[LANGUAGE_NONE][0]['profile_id'])) {
$profile = commerce_customer_profile_load($account->field_billing_register[LANGUAGE_NONE][0]['profile_id']);
if ($profile->uid != $account->uid) {
$profile->uid = $account->uid;
commerce_customer_profile_save($profile);
}
}
}
}
/**
* Set the appropriate uid in the newly created billing profile.
* @param $form
* @param $form_state
*/
function zayataroma_user_register_default_customer_profile_form_submit($form, &$form_state) {
if (!empty($form_state['user'])) {
$account = $form_state['user'];
$profile = commerce_customer_profile_load($account->field_billing_register[LANGUAGE_NONE][0]['profile_id']);
$profile->uid = $account->uid;
commerce_customer_profile_save($profile);
}
}
function zayataroma_user_presave(&$edit, $account, $category) { #taken from contact module
$edit['data']['net-30'] = isset($edit['net-30']) ? $edit['net-30'] : 0;
}
function zayataroma_user_form_submit($form, &$form_state) {
if (!empty($form_state['values']['roles'][6]) && !empty($form_state['values']['roles'][7])) {
$form_state['values']['roles'][7] = 0;
}
// Update the store pickup profile.
if (!empty($form_state['user']->data['pickup_shipping_profile_id'])) {
$pickup_profile=commerce_customer_profile_load($form_state['user']->data['pickup_shipping_profile_id']);
if(
$form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['first_name'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['first_name']
|| $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['last_name'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['last_name']
|| $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['organisation_name'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['organisation_name']
|| $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['phone_number'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['phone_number']
|| $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['phone_number_extension'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['phone_number_extension']
|| $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['mobile_number'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['mobile_number']
|| $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['fax_number'] !== $pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['fax_number']
) {
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['fax_number'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['fax_number'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['phone_number'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['phone_number'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['phone_number_extension'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['phone_number_extension'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['mobile_number'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['mobile_number'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['country'] = 'CA';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'] = 'QC';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['sub_administrative_area'] = NULL;
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['locality'] = 'Bromont';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['dependent_locality'] = NULL;
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = 'J2L 1C9';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] = '1339 Shefford';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['premise'] = '';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['sub_premise'] = '';
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['organisation_name'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['organisation_name'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['first_name'].' '.$form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['last_name'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['first_name'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['first_name'];
$pickup_profile->commerce_customer_address[LANGUAGE_NONE][0]['last_name'] = $form_state['input']['field_billing_register'][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0]['last_name'];
$pickup_profile->data['pickup_shipping_profile']=TRUE;
commerce_customer_profile_save($pickup_profile);
$form_state['user']->data['pickup_shipping_profile_id']=$pickup_profile->profile_id;
}
}
}
/**
* Autocomplete function.
* @param $entity_type
* @param $field_name
* @param $bundle
* @param string $string
*/
function zayataroma_product_autocomplete($entity_type, $field_name, $bundle, $string = '') {
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle);
$matches = array();
// Extract the SKU string from the URL while preserving support for SKUs that
// contain slashes. Whereas we previously relied on the $string parameter, its
// value was being truncated when any SKUs in the autocomplete widget had a /.
// @see http://drupal.org/node/1962144
$args = explode('/', request_path());
if (count($args) <= 5) {
drupal_json_output($matches);
return;
}
// Trim off leading arguments from other modules such as Locale.
$offset = array_search('commerce_product', $args);
array_splice($args, 0, 5 + $offset);
$string = implode('/', $args);
// The user enters a comma-separated list of tags. We only autocomplete the last tag.
$tags_typed = drupal_explode_tags($string);
$tag_last = drupal_strtolower(array_pop($tags_typed));
if (!empty($tag_last)) {
$prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
// Determine the type of autocomplete match to use when searching for products.
$match = isset($field['widget']['autocomplete_match']) ? $field['widget']['autocomplete_match'] : 'contains';
// Get an array of matching products.
$products = zayataroma_product_match_products($field, $instance, $tag_last, $match, array(), 100);
// Loop through the products and convert them into autocomplete output.
foreach ($products as $product_id => $data) {
$matches[$prefix . $data['sku']] = ($data['status'] == 0 ? '<span style="color:red">Non actif</span>': '') . ' ' . $data['rendered'];
}
}
drupal_json_output($matches);
}
function zayataroma_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if($form_state['build_info']['form_id']=='commerce_order_ui_order_form') {
#$order=$form_state['build_info']['args'][0];
if(!empty($entity_form['commerce_product'][LANGUAGE_NONE])){
$entity_form['commerce_product'][LANGUAGE_NONE]['#autocomplete_path']='zayataroma-products/'.$entity_form['commerce_product'][LANGUAGE_NONE]['#autocomplete_path'];
}
}
}
/**
* Delete cron and cache empty files.
* @param $dir
*/
function destroy($dir) {
$mydir = opendir($dir);
while (false !== ($file = readdir($mydir))) {
if ($file != "." && $file != "..") {
if (!is_dir($dir.$file) && (strstr($file, 'cron')) && filesize($dir.$file) == 0) {
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
if (!is_dir($dir.$file) && (strstr($file, 'cache')) && filesize($dir.$file) == 0) {
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
}
closedir($mydir);
}
function zayataroma_ccl_order_convert($order_wrapper, $account){ #hook ccl_order_convert
// For new users, set field_billing_register in the user's profile with's the order's billing profile
if (empty($account->field_billing_register)) {
$account->field_billing_register[LANGUAGE_NONE][0]['profile_id'] = $order_wrapper->commerce_customer_billing->profile_id->value();
user_save($account);
}
}
/**
* Implements hook_commerce_line_item_type_info_alter().
* @param $line_item_types
*/
function zayataroma_commerce_line_item_type_info_alter(&$line_item_types) {
$line_item_types['product']['callbacks'] = array('title' => '_zayataroma_commerce_product_line_item_title');
}
/**
* Callback for title to product line item.
* Returns an appropriate title for this line item for this site
* @param $line_item
* @return string
*/
function _zayataroma_commerce_product_line_item_title($line_item) {
global $language;
$product = entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_product->value();
if (!empty($product->product_id)) {
$nid = zayataroma_get_referencing_node_id($product);
if (!empty($nid)) {
if (is_numeric($nid)) {
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
if ($node->type == 'oils') {
$title_field = $wrapper->language($language->language)->title_field->value();
$field_specification = $wrapper->language($language->language)->field_specification->value();
$field_crop_type = $wrapper->language($language->language)->field_crop_type->value();
$field_display_origin_in_label = $wrapper->language($language->language)->field_display_origin_in_label->value();
$field_origin = $wrapper->language($language->language)->field_origin->value();
$title_label = $title_field.((!empty($field_specification))?' '.$field_specification:'').((strstr($field_crop_type,'BIO'))?' '.strtolower(t('Organic')):'').((!empty($field_display_origin_in_label))?' '.$field_origin:'') .' '.str_replace($title_field, '', $product->title);
}
}
}
}
if (!empty($title_label)) {
return $title_label;
} else {
return check_plain($product->title);
}
}
function zayataroma_get_referencing_node_id($product) {
// Iterate through all fields which refer to commerce products.
foreach (commerce_info_fields('commerce_product_reference') as $field) {
// Build query.
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node', '=')
->fieldCondition($field['field_name'], 'product_id', $product->product_id, '=')
->range(0, 1);
if ($result = $query->execute()) {
// Return node id.
$keys = array_keys($result['node']);
return array_shift($keys);
}
}
return false;
}
function zayataroma_product_match_products($field, $instance = NULL, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE) {
$results = &drupal_static(__FUNCTION__, array());
// Create unique id for static cache.
$cid = implode(':', array(
$field['field_name'],
$match,
($string !== '' ? $string : implode('-', $ids)),
$limit,
));
if (!isset($results[$cid])) {
$matches = _zayataroma_product_match_products_standard($instance, $string, $match, $ids, $limit, $access_tag);
// Store the results.
$results[$cid] = !empty($matches) ? $matches : array();
}
return $results[$cid];
}
function _zayataroma_product_match_products_standard($instance, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE) {
global $language;
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'commerce_product');
// Add the access control tag if specified.
if ($access_tag) {
$query->addTag('commerce_product_access');
}
// Add a global query tag so anyone can alter this query.
$query->addTag('commerce_product_match');
// Add a condition to the query to filter by matching product types.
if (!empty($instance['settings']['referenceable_types'])) {
$types = array_diff(array_values($instance['settings']['referenceable_types']), array(0, NULL));
// Only filter by type if some types have been specified.
if (!empty($types)) {
$query->propertyCondition('type', $types, 'IN');
}
}
if ($string !== '') {
// EntityFieldQuery cannot do OR clauses, so we use hook_query_TAG_alter.
$query->addTag('commerce_sku_or_title_match');
$sku_title_meta = new stdClass();
$sku_title_meta->properties = array(
'sku',
'title',
);
$sku_title_meta->string = $string;
$sku_title_meta->match = $match;
$query->addMetaData('commerce_sku_or_title_match', $sku_title_meta);
}
elseif ($ids) {
// Otherwise add a product_id specific condition if specified.
$query->propertyCondition('product_id', $ids, 'IN');
}
// Order the results by SKU, title, and then product type.
$query
->propertyOrderBy('sku')
->propertyOrderBy('title')
->propertyOrderBy('type');
// Add a limit if specified.
if ($limit) {
$query->range(0, $limit);
}
$entities = $query->execute();
$matches = array();
if (isset($entities['commerce_product'])) {
$pids = array_keys($entities['commerce_product']);
// EntityFieldQuery doesn't return sku and title, so we have to load again.
$products = commerce_product_load_multiple($pids);
foreach ($products AS $product) {
$body = '';
$title_label = '';
if (!empty($product->product_id)) {
$nid=zayataroma_get_referencing_node_id($product);
if(!empty($nid)){
if(is_numeric($nid)){
$node=node_load($nid);
if($node->type=='oils'){
$wrapper=entity_metadata_wrapper('node', $node);
$title_field=$wrapper->language($language->language)->title_field->value();
$field_specification=$wrapper->language($language->language)->field_specification->value();
$field_crop_type=$wrapper->language($language->language)->field_crop_type->value();
$field_display_origin_in_label=$wrapper->language($language->language)->field_display_origin_in_label->value();
$field_origin=$wrapper->language($language->language)->field_origin->value();
$body=$wrapper->language($language->language)->body->value();
$title_label=$title_field.((!empty($field_specification))?' '.$field_specification:'').((strstr($field_crop_type,'BIO'))?' '.strtolower(t('Organic')):'').((!empty($field_display_origin_in_label))?' '.$field_origin:'') .' '.str_replace($title_field, '', $product->title);
}
}
}
}
$matches[$product->product_id] = array(
'sku' => $product->sku,
'type' => $product->type,
'title' => $product->title,
'status' => $product->status,
'rendered' => '<strong>'.t('@title ', array('@title' => ((!empty($title_label))?$title_label:$product->title) )).'</strong>'.((!empty($body['value']))?'<br/><span style="font-style:italic;">'.preg_replace('/\s+/', ' ',strip_tags($body['value'])).'</span>':'<br/>&nbsp;'),
);
}
}
return $matches;
}
function zayataroma_inline_entity_form_table_generate_label($type,$line_item) {
global $language;
if ($line_item->type == 'product') {
$title_label = '';
if (!empty($line_item->commerce_product[LANGUAGE_NONE][0]['product_id'])) {
$product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
if (!empty($product->product_id)) {
$nid = zayataroma_get_referencing_node_id($product);
$title_label = '';
if (!empty($nid)) {
if (is_numeric($nid)) {
$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
if ($node->type == 'oils') {
$title_field = $wrapper->language($language->language)->title_field->value();
$field_specification = $wrapper->language($language->language)->field_specification->value();
$field_crop_type = $wrapper->language($language->language)->field_crop_type->value();
$field_display_origin_in_label = $wrapper->language($language->language)->field_display_origin_in_label->value();
$field_origin = $wrapper->language($language->language)->field_origin->value();
$title_label = $title_field . ((!empty($field_specification)) ? ' ' . $field_specification : '') . ((strstr($field_crop_type,'BIO'))?' '.strtolower(t('Organic')):'').((!empty($field_display_origin_in_label))?' '.$field_origin:'') .' '.str_replace($title_field, '', $product->title);
}
}
}
}
}
if (!empty($title_label)) {
return $title_label;
} else {
return $line_item->line_item_label;
}
} else {
return $line_item->line_item_label;
}
}
function zayataroma_inline_entity_form_table_fields_alter(&$fields, $context) {
if ($context['field_name'] == 'commerce_line_items') {
$fields['line_item_label']['type'] = 'callback';
$fields['line_item_label']['render_callback'] = 'zayataroma_inline_entity_form_table_generate_label';
}
}
/**
* Implements hook_commerce_order_state_info().
* Defines order states for use in grouping order statuses together.
* @return array
*/
function zayataroma_commerce_order_state_info() {
return array(
'production' => array(
'name' => 'production',
'title' => t('Production'),
'description' => t('Orders in this state have been paid in full, but haven\'t been completely processed.'),
'weight' => 5,
'default_status' => 'prod_rr',
'cart' => FALSE,
'status' => TRUE,
),
'order_ready' => array(
'name' => 'order_ready',
'title' => t('Order ready'),
'description' => t('Orders in this state are ready to be delivered.'),
'weight' => 6,
'default_status' => 'pret_transport',
'cart' => FALSE,
'status' => TRUE,
),
'order_sent' => array(
'name' => 'order_sent',
'title' => t('Order sent'),
'description' => t('Orders in this state have been delivered.'),
'weight' => 7,
'default_status' => 'partie_comptoir_n30',
'cart' => FALSE,
'status' => TRUE,
),
);
}
function zayataroma_form_commerce_backoffice_order_status_form_alter(&$form, $form_state) {
if($form_state['order']->uid){
$account=user_load($form_state['order']->uid);
if(!empty($account->field_note_administrative[LANGUAGE_NONE][0]['value'])){
drupal_set_message($account->field_note_administrative[LANGUAGE_NONE][0]['value']);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
* @param $form
* @param $form_state
*/
function zayataroma_form_commerce_checkout_form_checkout_alter(&$form, $form_state) {
$form['customer_profile_shipping']['commerce_shipping_method'] = array(
'#type' => 'radios',
'#options' => array(
'pickup' => t('I want to pick up my order at the store.'),
'delivery' => t('I want my order to be shipped.'),
),
'#ajax' => array(
'callback' => 'commerce_shipping_method_refresh',
'wrapper' => 'customer-profile-shipping-ajax-wrapper',
),
'#weight' => -31,
'#default_value' => ((isset($form_state['order']->data['shipping_method']))?$form_state['order']->data['shipping_method']:''),
'#element_validate' => array('zayataroma_commerce_order_shipping_method_validate'),
);
if (isset($form_state['order']->data['shipping_method'])) {
$form['customer_profile_shipping']['commerce_customer_address']['#access'] = (($form_state['order']->data['shipping_method'] == 'delivery') ? 1 : 0);
$form['customer_profile_shipping']['commerce_customer_profile_copy']['#access'] = (($form_state['order']->data['shipping_method'] == 'delivery') ? 1 : 0);
if (isset($form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'])) {
if ($form_state['order']->data['shipping_method'] == 'pickup') {
$form['customer_profile_shipping']['addressbook']['#type'] = 'hidden';
unset($form['customer_profile_shipping']['addressbook']['#options']);
$form['customer_profile_shipping']['addressbook']['#value'] = $form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'];
if (isset($form['customer_profile_shipping']['customer_profile'])) {
unset($form['customer_profile_shipping']['customer_profile']);
}
$profile = commerce_customer_profile_load($form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id']);
$form['customer_profile_shipping']['customer_profile'] = array(
'#type' => 'value',
'#value' => $profile,
);
$form['customer_profile_shipping']['commerce_customer_address'][LANGUAGE_NONE][0]['#address'] = $profile->commerce_customer_address[LANGUAGE_NONE][0];
} elseif($form_state['order']->data['shipping_method']=='delivery'){
$profile=commerce_customer_profile_load($form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id']);
if(!empty($profile->data['pickup_shipping_profile'])){ #pickup profile was selected, let's reset it
$form['addressbook']['#default_value']=0;
$new_profile=commerce_customer_profile_new('shipping', $form_state['order']->uid);
$new_profile->entity_context = array(
'entity_type' => 'commerce_order',
'entity_id' => $form_state['order']->order_id,
);
$form['customer_profile_shipping']['customer_profile']=array(
'#type'=>'value',
'#value'=>$new_profile,
);
$form['customer_profile_shipping']['commerce_customer_address'][LANGUAGE_NONE][0]['#address']=array();
}
}
}
} else {
$form['customer_profile_shipping']['commerce_customer_address']['#access']=0;
$form['customer_profile_shipping']['commerce_customer_profile_copy']['#access']=0;
$form['customer_profile_shipping']['addressbook']['#access']=0;
}
$form['#attached']['js'][] = drupal_get_path('module','zayataroma').'/js/admin-commerce-checkout-apply-form-set-error-fix-for-radio-btn.js';
$form['#attached']['js'][] = drupal_get_path('module','zayataroma').'/js/admin-commerce-checkout-disable-continue-btn-during-ajax-requests.js';
}
function zayataroma_save_pickup_shipping_profile($profile, $form_state) {
$profile->commerce_customer_address[LANGUAGE_NONE][0]['fax_number'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['fax_number'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['phone_number'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['phone_number'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['phone_number_extension'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['phone_number_extension'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['mobile_number'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['mobile_number'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['country'] = 'CA';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'] = 'QC';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['sub_administrative_area'] = NULL;
$profile->commerce_customer_address[LANGUAGE_NONE][0]['locality'] = 'Bromont';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['dependent_locality'] = NULL;
$profile->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = 'J2L 1C9';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] = '1339 Shefford';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['premise'] = '';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['sub_premise'] = '';
$profile->commerce_customer_address[LANGUAGE_NONE][0]['organisation_name'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['organisation_name'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['first_name'] . ' ' . $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['last_name'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['first_name'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['first_name'];
$profile->commerce_customer_address[LANGUAGE_NONE][0]['last_name'] = $form_state['values']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['last_name'];
$profile->data['pickup_shipping_profile']=TRUE;
commerce_customer_profile_save($profile);
return $profile;
}
function zayataroma_commerce_order_shipping_method_validate($element, &$form_state, $form) {
global $user;
if (empty($form_state['values']['customer_profile_shipping']['commerce_shipping_method'])) {
form_set_error('customer_profile_shipping][commerce_shipping_method',t('Please choose if you would like for this order to be delivered to picked-up at our store.'));
} elseif (strstr($form_state['triggering_element']['#name'],'commerce_shipping_method')) {
$form_state['order']->data['shipping_method'] = $element['#value'];
if (empty($user->data['pickup_shipping_profile_id'])) {
// Create a shipping profile for store pickup and assign it to this user account.
$profile = commerce_customer_profile_new('shipping', $user->uid);
// Add the entity context of the current cart order.
$profile->entity_context = array(
'entity_type' => 'commerce_order',
'entity_id' => $form_state['order']->order_id,
);
$profile->_is_new = 1;
$profile->status = 1;
$profile = zayataroma_save_pickup_shipping_profile($profile,$form_state);
if ($user->uid) {
// Set it in the user account.
$account = user_load($user->uid);
$account->data['pickup_shipping_profile_id'] = $profile->profile_id;
user_save($account);
}
} else {
$profile = commerce_customer_profile_load($user->data['pickup_shipping_profile_id']);
$pickup_profile_is_valid = false;
if (!empty($profile->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'])) {
if ($profile->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] == '1339 Shefford'
&& $profile->commerce_customer_address[LANGUAGE_NONE][0]['locality'] == 'Bromont'
&& $profile->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] == 'J2L 1C9') {
$pickup_profile_is_valid = true;
}
}
if (!$pickup_profile_is_valid) {
$profile = zayataroma_save_pickup_shipping_profile($profile,$form_state);
}
}
$form_state['order']->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'] = $profile->profile_id;
$form_state['order']->data['profiles']['customer_profile_shipping'] = $profile->profile_id;
$form_state['order']->data['profile_copy']['customer_profile_shipping']['status'] = FALSE;
if (isset($form_state['order']->data['profile_copy']['customer_profile_shipping']['elements'])) {
unset($form_state['order']->data['profile_copy']['customer_profile_shipping']['elements']);
}
commerce_order_save($form_state['order']);
}
}
/**
* Implements hook_commerce_checkout_complete().
* @param $order
* The order that just completed checkout.
*/
function zayataroma_commerce_checkout_complete($order) {
// Set $status to 'en_attente_comptoir' for pickups shipping method.
// Otherwise set the $status to 'pending'.
$status = 'pending';
if (!empty($order->data['shipping_method'])) {
if ($order->data['shipping_method'] == 'pickup'){
$status = 'en_attente_comptoir';
}
}
$order->status = $status;
commerce_order_save($order);
}
/**
* Implements hook_commerce_order_status_info_alter().
* @param $order_status
*/
function zayataroma_commerce_order_status_info_alter(&$order_status) {
unset($order_status['completed']);
}
function commerce_shipping_method_refresh($form, &$form_state) {
$pane_id = reset($form_state['triggering_element']['#parents']);
$commands = array();
// Render the pane afresh to capture any changes based on address entry.
$commands[] = ajax_command_replace(NULL, drupal_render($form[$pane_id]));
return array('#type' => 'ajax', '#commands' => $commands);
}
function zayataroma_form_i18n_string_locale_translate_edit_form_alter(&$form, $form_state) {
unset($form['#validate']);
}
/**
* Get node title.
* @param $node
* @return string
*/
function zayataroma_page_title($node) {
if (!$node) {
return '';
}
if (is_numeric($node)) {
$node = node_load($node);
}
return !empty($node->title) ? $node->title : '';
}
/**
* Count the number of little hearts (From Top 100 field).
*/
function zayataroma_count_hearts() {
$result = db_select('field_data_field_top_100', 'fdft')
->fields('fdft')
->condition('field_top_100_value', '1')
->execute();
$hearts_count = $result->rowCount();
return $hearts_count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment