Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save facine/7014017 to your computer and use it in GitHub Desktop.
Save facine/7014017 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_menu().
*/
function MODULENAME_menu() {
$items['node/%node/edit-line-item/%commerce_line_item'] = array(
'title' => 'Edit line item in the cart',
'page callback' => 'node_page_view',
'page arguments' => array(1),
'access callback' => 'MODULE_edit_line_item_edit_access',
'access arguments' => array(1, 3, 'access checkout'),
);
return $items;
}
/**
* Implements hook_views_data_alter().
*
* Overrides cart_line_item_link_edit handler.
*/
function MODULENAME_views_data_alter(&$data) {
$data['commerce_line_item']['cart_line_item_link_edit']['field']['handler'] = 'MODULENAME_handler_field_cart_line_item_link_edit';
}
/**
* Access callback for node/%node/edit-line-item/%commerce_line_item.
*/
function MODULENAME_edit_line_item_edit_access($node, $line_item, $permission) {
$context = $line_item->data['context'];
if (($context['entity']['entity_type'] != 'node') || ($context['entity']['entity_id'] != $node->nid)) {
return false;
}
return commerce_cart_line_item_form_menu_item_access($line_item, $permission);
}
/**
* Implements hook_module_implements_alter().
*
* Invoke our hook_field_attach_view_alter() before commerce_cart's one.
*/
function MODULENAME_module_implements_alter(&$implementations, $hook) {
if ($hook == 'field_attach_view_alter') {
$key = 'MODULENAME';
$group = $implementations[$key];
unset($implementations[$key]);
$implementations = array($key => $group) + $implementations;
}
}
/**
* Implements hook_field_attach_view_alter().
*
* Replace the line item argument to the add to cart form with the one being edited.
*
* @see commerce_cart_field_attach_view_alter().
* @see MODULENAME_module_implements_alter().
*/
function MODULENAME_field_attach_view_alter(&$output, $context) {
if (arg(2) == 'edit-line-item') {
$line_item = menu_get_object('commerce_line_item', 3);
if ($line_item) {
$output[DISCOVER_PLAN_FIELD_PRODUCT_REFERENCE][0]['#arguments']['line_item'] = $line_item;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment