Révision b858700c
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/commerce/modules/cart/commerce_cart.module | ||
---|---|---|
8 | 8 |
* special considerations to associate it with a user and |
9 | 9 |
*/ |
10 | 10 |
|
11 |
// Define constants for the shopping cart refresh modes. |
|
12 |
define('COMMERCE_CART_REFRESH_ALWAYS', 'always'); |
|
13 |
define('COMMERCE_CART_REFRESH_OWNER_ONLY', 'owner_only'); |
|
14 |
define('COMMERCE_CART_REFRESH_ACTIVE_CART_ONLY', 'active_cart_only'); |
|
11 | 15 |
|
12 | 16 |
/** |
13 | 17 |
* Implements hook_menu(). |
... | ... | |
39 | 43 |
'file' => 'includes/commerce_cart.pages.inc', |
40 | 44 |
); |
41 | 45 |
|
46 |
// If the Order UI module is installed, add a local action to it that lets an |
|
47 |
// administrator execute a cart order refresh on the order. Modules that |
|
48 |
// define their own order edit menu item are also responsible for defining |
|
49 |
// their own local action menu items if needed. |
|
50 |
if (module_exists('commerce_order_ui')) { |
|
51 |
$items['admin/commerce/orders/%commerce_order/edit/refresh'] = array( |
|
52 |
'title' => 'Apply pricing rules', |
|
53 |
'description' => 'Executes the cart order refresh used to apply all current pricing rules on the front end.', |
|
54 |
'page callback' => 'drupal_get_form', |
|
55 |
'page arguments' => array('commerce_cart_order_refresh_form', 3), |
|
56 |
'access callback' => 'commerce_cart_order_refresh_form_access', |
|
57 |
'access arguments' => array(3), |
|
58 |
'type' => MENU_LOCAL_ACTION, |
|
59 |
'file' => 'includes/commerce_cart.admin.inc', |
|
60 |
); |
|
61 |
} |
|
62 |
|
|
42 | 63 |
return $items; |
43 | 64 |
} |
44 | 65 |
|
... | ... | |
74 | 95 |
drupal_goto('cart'); |
75 | 96 |
} |
76 | 97 |
|
98 |
/** |
|
99 |
* Access callback: determines access to the "Apply pricing rules" local action. |
|
100 |
*/ |
|
101 |
function commerce_cart_order_refresh_form_access($order) { |
|
102 |
// Do not show the link for cart orders as they're refreshed automatically. |
|
103 |
if (commerce_cart_order_is_cart($order)) { |
|
104 |
return FALSE; |
|
105 |
} |
|
106 |
|
|
107 |
// Returns TRUE if the link is enabled via the order settings form and the |
|
108 |
// user has access to update the order. |
|
109 |
return variable_get('commerce_order_apply_pricing_rules_link', TRUE) && commerce_order_access('update', 3); |
|
110 |
} |
|
111 |
|
|
77 | 112 |
/** |
78 | 113 |
* Implements hook_hook_info(). |
79 | 114 |
*/ |
... | ... | |
308 | 343 |
drupal_set_message(t('%title removed from your cart.', array('%title' => $title))); |
309 | 344 |
} |
310 | 345 |
|
346 |
/** |
|
347 |
* Implements hook_form_FORM_ID_alter(). |
|
348 |
* |
|
349 |
* Adds a checkbox to the order settings form to enable the local action on |
|
350 |
* order edit forms to apply pricing rules. |
|
351 |
*/ |
|
352 |
function commerce_cart_form_commerce_order_settings_form_alter(&$form, &$form_state) { |
|
353 |
$form['commerce_order_apply_pricing_rules_link'] = array( |
|
354 |
'#type' => 'checkbox', |
|
355 |
'#title' => t('Enable the local action link on order edit forms to apply pricing rules.'), |
|
356 |
'#description' => t('Even if enabled the link will not appear on shopping cart order edit forms.'), |
|
357 |
'#default_value' => variable_get('commerce_order_apply_pricing_rules_link', TRUE), |
|
358 |
'#weight' => 10, |
|
359 |
); |
|
360 |
|
|
361 |
// Add a fieldset for settings pertaining to the shopping cart refresh. |
|
362 |
$form['cart_refresh'] = array( |
|
363 |
'#type' => 'fieldset', |
|
364 |
'#title' => t('Shopping cart refresh'), |
|
365 |
'#description' => t('Shopping cart orders comprise orders in shopping cart and some checkout related order statuses. These settings let you control the shopping cart orders are refreshed, the process during which product prices are recalculated, to improve site performance in the case of excessive refreshes on sites with less dynamic pricing needs.'), |
|
366 |
'#weight' => 40, |
|
367 |
); |
|
368 |
$form['cart_refresh']['commerce_cart_refresh_mode'] = array( |
|
369 |
'#type' => 'radios', |
|
370 |
'#title' => t('Shopping cart refresh mode'), |
|
371 |
'#options' => array( |
|
372 |
COMMERCE_CART_REFRESH_ALWAYS => t('Refresh a shopping cart when it is loaded regardless of who it belongs to.'), |
|
373 |
COMMERCE_CART_REFRESH_OWNER_ONLY => t('Only refresh a shopping cart when it is loaded if it belongs to the current user.'), |
|
374 |
COMMERCE_CART_REFRESH_ACTIVE_CART_ONLY => t("Only refresh a shopping cart when it is loaded if it is the current user's active shopping cart."), |
|
375 |
), |
|
376 |
'#default_value' => variable_get('commerce_cart_refresh_mode', COMMERCE_CART_REFRESH_OWNER_ONLY), |
|
377 |
); |
|
378 |
$form['cart_refresh']['commerce_cart_refresh_frequency'] = array( |
|
379 |
'#type' => 'textfield', |
|
380 |
'#title' => t('Shopping cart refresh frequency'), |
|
381 |
'#description' => t('Shopping carts will only be refreshed if more than the specified number of seconds have passed since they were last refreshed.'), |
|
382 |
'#default_value' => variable_get('commerce_cart_refresh_frequency', 30), |
|
383 |
'#required' => TRUE, |
|
384 |
'#size' => 32, |
|
385 |
'#field_suffix' => t('seconds'), |
|
386 |
'#element_validate' => array('commerce_cart_validate_refresh_frequency'), |
|
387 |
); |
|
388 |
$form['cart_refresh']['commerce_cart_refresh_force'] = array( |
|
389 |
'#type' => 'checkbox', |
|
390 |
'#title' => t('Always refresh shopping cart orders on shopping cart and checkout form pages regardless of other settings.'), |
|
391 |
'#description' => t('Note: this option only applies to the core /cart and /checkout/* paths.'), |
|
392 |
'#default_value' => variable_get('commerce_cart_refresh_force', TRUE), |
|
393 |
); |
|
394 |
} |
|
395 |
|
|
396 |
/** |
|
397 |
* Form element validation handler for the cart refresh frequency value. |
|
398 |
*/ |
|
399 |
function commerce_cart_validate_refresh_frequency($element, &$form_state) { |
|
400 |
$value = $element['#value']; |
|
401 |
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value < 0)) { |
|
402 |
form_error($element, t('%name must be 0 or a positive integer.', array('%name' => $element['#title']))); |
|
403 |
} |
|
404 |
} |
|
405 |
|
|
311 | 406 |
/** |
312 | 407 |
* Implements hook_form_FORM_ID_alter(). |
313 | 408 |
* |
... | ... | |
508 | 603 |
*/ |
509 | 604 |
function commerce_cart_user_update(&$edit, $account, $category) { |
510 | 605 |
// If the e-mail address was changed... |
511 |
if ($account->mail != $edit['original']->mail) { |
|
606 |
if (!empty($edit['original']->mail) && $account->mail != $edit['original']->mail) {
|
|
512 | 607 |
// Load the user's shopping cart orders. |
513 | 608 |
$query = new EntityFieldQuery(); |
514 | 609 |
|
... | ... | |
579 | 674 |
} |
580 | 675 |
} |
581 | 676 |
|
677 |
/** |
|
678 |
* Checks if a cart order should be refreshed based on the shopping cart refresh |
|
679 |
* settings on the order settings form. |
|
680 |
* |
|
681 |
* @param $order |
|
682 |
* The cart order to check. |
|
683 |
* |
|
684 |
* @return |
|
685 |
* Boolean indicating whether or not the cart order can be refreshed. |
|
686 |
*/ |
|
687 |
function commerce_cart_order_can_refresh($order) { |
|
688 |
global $user; |
|
689 |
|
|
690 |
// Force the shopping cart refresh on /cart and /checkout/* paths if enabled. |
|
691 |
if (variable_get('commerce_cart_refresh_force', TRUE) && |
|
692 |
(current_path() == 'cart' || strpos(current_path(), 'checkout/') === 0)) { |
|
693 |
return TRUE; |
|
694 |
} |
|
695 |
|
|
696 |
// Prevent refresh for orders that don't match the current refresh mode. |
|
697 |
switch (variable_get('commerce_cart_refresh_mode', COMMERCE_CART_REFRESH_OWNER_ONLY)) { |
|
698 |
case COMMERCE_CART_REFRESH_OWNER_ONLY: |
|
699 |
// If the order is anonymous, check the session to see if the order |
|
700 |
// belongs to the current user. Otherwise just check that the order uid |
|
701 |
// matches the current user. |
|
702 |
if ($order->uid == 0 && !commerce_cart_order_session_exists($order->order_id)) { |
|
703 |
return FALSE; |
|
704 |
} |
|
705 |
elseif ($order->uid != $user->uid) { |
|
706 |
return FALSE; |
|
707 |
} |
|
708 |
break; |
|
709 |
|
|
710 |
case COMMERCE_CART_REFRESH_ACTIVE_CART_ONLY: |
|
711 |
// Check to see if the order ID matches the current user's cart order ID. |
|
712 |
if (commerce_cart_order_id($user->uid) != $order->order_id) { |
|
713 |
return FALSE; |
|
714 |
} |
|
715 |
break; |
|
716 |
|
|
717 |
case COMMERCE_CART_REFRESH_ALWAYS: |
|
718 |
default: |
|
719 |
// Continue on if shopping cart orders should always refresh. |
|
720 |
break; |
|
721 |
} |
|
722 |
|
|
723 |
// Check to see if the last cart refresh happened long enough ago. |
|
724 |
$seconds = variable_get('commerce_cart_refresh_frequency', 15); |
|
725 |
|
|
726 |
if (!empty($seconds) && !empty($order->data['last_cart_refresh']) && |
|
727 |
REQUEST_TIME - $order->data['last_cart_refresh'] < $seconds) { |
|
728 |
return FALSE; |
|
729 |
} |
|
730 |
|
|
731 |
return TRUE; |
|
732 |
} |
|
733 |
|
|
582 | 734 |
/** |
583 | 735 |
* Implements hook_commerce_order_load(). |
584 | 736 |
* |
... | ... | |
592 | 744 |
|
593 | 745 |
foreach ($orders as $order) { |
594 | 746 |
// Refresh only if this order object represents the latest revision of a |
595 |
// shopping cart order and it hasn't been refreshed already. |
|
747 |
// shopping cart order, it hasn't been refreshed already in this request |
|
748 |
// and it meets the criteria in the shopping cart refresh settings. |
|
596 | 749 |
if (!isset($refreshed[$order->order_id]) && |
597 | 750 |
commerce_cart_order_is_cart($order) && |
598 |
commerce_order_is_latest_revision($order)) { |
|
751 |
commerce_order_is_latest_revision($order) && |
|
752 |
commerce_cart_order_can_refresh($order)) { |
|
753 |
// Update the last cart refresh timestamp and record the order's current |
|
754 |
// changed timestamp to detect if the order is actually updated. |
|
755 |
$order->data['last_cart_refresh'] = REQUEST_TIME; |
|
756 |
|
|
757 |
$unchanged_data = $order->data; |
|
758 |
$last_changed = $order->changed; |
|
759 |
|
|
599 | 760 |
// Refresh the order and add its ID to the refreshed array. |
600 | 761 |
$refreshed[$order->order_id] = TRUE; |
601 | 762 |
commerce_cart_order_refresh($order); |
763 |
|
|
764 |
// If order wasn't updated during the refresh, we need to manually update |
|
765 |
// the last cart refresh timestamp in the database. |
|
766 |
if ($order->changed == $last_changed) { |
|
767 |
db_update('commerce_order') |
|
768 |
->fields(array('data' => serialize($unchanged_data))) |
|
769 |
->condition('order_id', $order->order_id) |
|
770 |
->execute(); |
|
771 |
|
|
772 |
db_update('commerce_order_revision') |
|
773 |
->fields(array('data' => serialize($unchanged_data))) |
|
774 |
->condition('order_id', $order->order_id) |
|
775 |
->condition('revision_id', $order->revision_id) |
|
776 |
->execute(); |
|
777 |
} |
|
602 | 778 |
} |
603 | 779 |
} |
604 | 780 |
} |
... | ... | |
877 | 1053 |
|
878 | 1054 |
/** |
879 | 1055 |
* Refreshes the contents of a shopping cart by finding the most current prices |
880 |
* for any product line items on the order.
|
|
1056 |
* for any product line items on the order. |
|
881 | 1057 |
* |
882 | 1058 |
* @param $order |
883 | 1059 |
* The order object whose line items should be refreshed. |
... | ... | |
925 | 1101 |
rules_invoke_event('commerce_product_calculate_sell_price', $cloned_line_item); |
926 | 1102 |
} |
927 | 1103 |
|
928 |
// Allow other modules alter line items on a shopping cart refresh. |
|
1104 |
// Allow other modules to alter line items on a shopping cart refresh.
|
|
929 | 1105 |
module_invoke_all('commerce_cart_line_item_refresh', $cloned_line_item, $order_wrapper); |
930 | 1106 |
|
931 | 1107 |
// Delete this line item if it no longer has a valid price. |
... | ... | |
1126 | 1302 |
// If no order existed, create one now. |
1127 | 1303 |
if (empty($order)) { |
1128 | 1304 |
$order = commerce_cart_order_new($uid); |
1305 |
$order->data['last_cart_refresh'] = REQUEST_TIME; |
|
1129 | 1306 |
} |
1130 | 1307 |
|
1131 | 1308 |
// Set the incoming line item's order_id. |
... | ... | |
1528 | 1705 |
* A fully formed product line item whose data will be used in the following |
1529 | 1706 |
* ways by the form: |
1530 | 1707 |
* - $line_item->data['context']['product_ids']: an array of product IDs to |
1531 |
* include on the form. |
|
1708 |
* include on the form or the string 'entity' if the context array includes |
|
1709 |
* an entity array with information for accessing the product IDs from an |
|
1710 |
* entity's product reference field. |
|
1711 |
* - $line_item->data['context']['entity']: if the product_ids value is the |
|
1712 |
* string 'entity', an associative array with the keys 'entity_type', |
|
1713 |
* 'entity_id', and 'product_reference_field_name' that points to the |
|
1714 |
* location of the product IDs used to build the form. |
|
1532 | 1715 |
* - $line_item->data['context']['add_to_cart_combine']: a boolean indicating |
1533 | 1716 |
* whether or not to attempt to combine the product added to the cart with |
1534 | 1717 |
* existing line items of matching fields. |
... | ... | |
1574 | 1757 |
$default_quantity = $line_item->quantity; |
1575 | 1758 |
|
1576 | 1759 |
// Retrieve the array of product IDs from the line item's context data array. |
1577 |
$product_ids = array(); |
|
1578 |
|
|
1579 |
// If the product IDs setting tells us to use entity values... |
|
1580 |
if ($line_item->data['context']['product_ids'] == 'entity' && |
|
1581 |
is_array($line_item->data['context']['entity'])) { |
|
1582 |
$entity_data = $line_item->data['context']['entity']; |
|
1583 |
|
|
1584 |
// Load the specified entity. |
|
1585 |
$entity = entity_load_single($entity_data['entity_type'], $entity_data['entity_id']); |
|
1586 |
|
|
1587 |
// Extract the product IDs from the specified product reference field. |
|
1588 |
if (!empty($entity->{$entity_data['product_reference_field_name']})) { |
|
1589 |
$product_ids = entity_metadata_wrapper($entity_data['entity_type'], $entity)->{$entity_data['product_reference_field_name']}->raw(); |
|
1590 |
} |
|
1591 |
} |
|
1592 |
elseif (is_array($line_item->data['context']['product_ids'])) { |
|
1593 |
$product_ids = $line_item->data['context']['product_ids']; |
|
1594 |
} |
|
1760 |
$product_ids = commerce_cart_add_to_cart_form_product_ids($line_item); |
|
1595 | 1761 |
|
1596 | 1762 |
// If we don't have a list of products to load, just bail out early. |
1597 | 1763 |
// There is nothing we can or have to do in that case. |
... | ... | |
2142 | 2308 |
$classes[] = 'commerce-product-field-empty'; |
2143 | 2309 |
} |
2144 | 2310 |
|
2145 |
$element += array(
|
|
2146 |
'#prefix' => '<div class="' . implode(' ', $classes) . '">',
|
|
2147 |
'#suffix' => '</div>',
|
|
2148 |
);
|
|
2311 |
// Append the prefix and suffix around existing values if necessary.
|
|
2312 |
$element += array('#prefix' => '', '#suffix' => '');
|
|
2313 |
$element['#prefix'] = '<div class="' . implode(' ', $classes) . '">' . $element['#prefix'];
|
|
2314 |
$element['#suffix'] .= '</div>';
|
|
2149 | 2315 |
|
2150 | 2316 |
$commands[] = ajax_command_replace('.' . $replacement_class, drupal_render($element)); |
2151 | 2317 |
} |
... | ... | |
2273 | 2439 |
'commerce_cart_add_to_cart_form' => array( |
2274 | 2440 |
'label' => t('Add to Cart form'), |
2275 | 2441 |
'description' => t('Display an Add to Cart form for the referenced product.'), |
2276 |
'field types' => array('commerce_product_reference'), |
|
2442 |
'field types' => array('commerce_product_reference', 'entityreference'),
|
|
2277 | 2443 |
'settings' => array( |
2278 | 2444 |
'show_quantity' => FALSE, |
2279 | 2445 |
'default_quantity' => 1, |
... | ... | |
2337 | 2503 |
else { |
2338 | 2504 |
$element['line_item_type'] = array( |
2339 | 2505 |
'#type' => 'hidden', |
2340 |
'#value' => key($types),
|
|
2506 |
'#value' => reset($types),
|
|
2341 | 2507 |
); |
2342 | 2508 |
} |
2343 | 2509 |
} |
... | ... | |
2390 | 2556 |
$product_ids = array(); |
2391 | 2557 |
|
2392 | 2558 |
foreach ($items as $delta => $item) { |
2393 |
$product_ids[] = $item['product_id']; |
|
2559 |
if (isset($item['product_id'])) { |
|
2560 |
$product_ids[] = $item['product_id']; |
|
2561 |
} |
|
2562 |
elseif (module_exists('entityreference') && isset($item['target_id'])) { |
|
2563 |
$product_ids[] = $item['target_id']; |
|
2564 |
} |
|
2394 | 2565 |
} |
2395 | 2566 |
|
2396 | 2567 |
if ($display['type'] == 'commerce_cart_add_to_cart_form') { |
... | ... | |
2442 | 2613 |
$cart_context['entity_id'] = $entity_id; |
2443 | 2614 |
unset($cart_context['entity']); |
2444 | 2615 |
|
2616 |
// Remove any Views data added to the context by views_handler_field_field. |
|
2617 |
// It unnecessarily increases the size of rows in the cache_form table for |
|
2618 |
// Add to Cart form state data. |
|
2619 |
if (!empty($cart_context['display']) && is_array($cart_context['display'])) { |
|
2620 |
unset($cart_context['display']['views_view']); |
|
2621 |
unset($cart_context['display']['views_field']); |
|
2622 |
unset($cart_context['display']['views_row_id']); |
|
2623 |
} |
|
2624 |
|
|
2445 | 2625 |
// Add the context for displaying product fields in the context of an entity |
2446 | 2626 |
// that references the product by looking at the entity this product |
2447 | 2627 |
// reference field is attached to. |
... | ... | |
2480 | 2660 |
} |
2481 | 2661 |
} |
2482 | 2662 |
|
2663 |
/** |
|
2664 |
* Returns an array of product IDs used for building an Add to Cart form from |
|
2665 |
* the context information in a line item's data array. |
|
2666 |
* |
|
2667 |
* @param $line_item |
|
2668 |
* The line item whose data array includes a context array used for building |
|
2669 |
* an Add to Cart form. |
|
2670 |
* |
|
2671 |
* @return |
|
2672 |
* The array of product IDs extracted from the line item. |
|
2673 |
* |
|
2674 |
* @see commerce_cart_add_to_cart_form() |
|
2675 |
*/ |
|
2676 |
function commerce_cart_add_to_cart_form_product_ids($line_item) { |
|
2677 |
$product_ids = array(); |
|
2678 |
|
|
2679 |
if (empty($line_item->data['context']) || |
|
2680 |
empty($line_item->data['context']['product_ids']) || |
|
2681 |
($line_item->data['context']['product_ids'] == 'entity' && empty($line_item->data['context']['entity']))) { |
|
2682 |
return $product_ids; |
|
2683 |
} |
|
2684 |
|
|
2685 |
// If the product IDs setting tells us to use entity values... |
|
2686 |
if ($line_item->data['context']['product_ids'] == 'entity' && |
|
2687 |
is_array($line_item->data['context']['entity'])) { |
|
2688 |
$entity_data = $line_item->data['context']['entity']; |
|
2689 |
|
|
2690 |
// Load the specified entity. |
|
2691 |
$entity = entity_load_single($entity_data['entity_type'], $entity_data['entity_id']); |
|
2692 |
|
|
2693 |
// Extract the product IDs from the specified product reference field. |
|
2694 |
if (!empty($entity->{$entity_data['product_reference_field_name']})) { |
|
2695 |
$product_ids = entity_metadata_wrapper($entity_data['entity_type'], $entity)->{$entity_data['product_reference_field_name']}->raw(); |
|
2696 |
} |
|
2697 |
} |
|
2698 |
elseif (is_array($line_item->data['context']['product_ids'])) { |
|
2699 |
$product_ids = $line_item->data['context']['product_ids']; |
|
2700 |
} |
|
2701 |
|
|
2702 |
return $product_ids; |
|
2703 |
} |
|
2704 |
|
|
2483 | 2705 |
/** |
2484 | 2706 |
* Implements hook_preprocess_views_view(). |
2485 | 2707 |
*/ |
... | ... | |
2488 | 2710 |
|
2489 | 2711 |
// Add the shopping cart stylesheet to the cart or form if they are not empty. |
2490 | 2712 |
if ($view->name == 'commerce_cart_block' || $view->name == 'commerce_cart_form') { |
2491 |
if (!empty($view->result)) { |
|
2492 |
drupal_add_css(drupal_get_path('module', 'commerce_cart') . '/theme/commerce_cart.theme.css'); |
|
2493 |
} |
|
2713 |
drupal_add_css(drupal_get_path('module', 'commerce_cart') . '/theme/commerce_cart.theme.css'); |
|
2494 | 2714 |
} |
2495 | 2715 |
} |
2496 | 2716 |
|
Formats disponibles : Unified diff
Weekly update of contrib modules