You are here

function uc_product_actions_action_executor in Ubercart Product Actions 7

Same name and namespace in other branches
  1. 6 uc_product_actions.module \uc_product_actions_action_executor()

Execute the action.

5 calls to uc_product_actions_action_executor()
uc_product_actions_update_cost_action in ./uc_product_actions.module
uc_product_actions_update_default_qty_action in ./uc_product_actions.module
uc_product_actions_update_list_price_action in ./uc_product_actions.module
uc_product_actions_update_sell_price_action in ./uc_product_actions.module
uc_product_actions_update_weight_action in ./uc_product_actions.module

File

./uc_product_actions.module, line 358

Code

function uc_product_actions_action_executor($action, $node, $context) {
  $current_value = $node->{$action};
  if (!is_null($current_value)) {
    switch ($context[$action . '_method']) {
      case 'percentage':
        if ($current_value == 0) {
          $new_value = 0;
        }
        else {
          $change = $current_value * $context[$action . '_change'] / 100;
          $current_value > 0 ? $new_value = $current_value + $change : ($new_value = $current_value - $change);
        }
        break;
      case 'difference':
        $new_value = $current_value + $context[$action . '_change'];
        break;
      case 'absolute':
        $new_value = $context[$action . '_change'];
        break;
    }
  }

  // Make necessary adjustments to value.
  if ($action == 'weight') {

    // When modifying by a percentage and old value contains a decimal, round new value to the same precision.
    if ($context['weight_method'] == 'percentage' && floor($current_value) != $current_value) {
      $precision = strlen(substr(strrchr($current_value, '.'), 1));
      $new_value = round($new_value, $precision);
    }
    if ($new_value < 0) {
      $new_value = 0;
    }

    // Product weight cannot be negative.
  }
  else {
    $precision = variable_get('uc_currency_prec', 2);
    $new_value = round($new_value, $precision);
  }

  // Save modified product.
  if (isset($new_value)) {
    $node->{$action} = $new_value;
    node_save($node);

    // Load modified node to check if operation was processed correctly.
    $new_node = node_load($node->nid, NULL, TRUE);
  }
  if (!isset($new_value) || $new_node->{$action} != $new_value) {
    drupal_set_message(ucfirst($action) . t(' (@new_value): %node may not have updated properly, please check and update manually if needed.', array(
      '%node' => $node->title,
      '@new_value' => $new_value,
    )), 'warning');
    watchdog('Ubercart product actions', ucfirst($action) . ' (@new_value): %node may not have updated properly, please check and update manually if needed.', array(
      '%node' => $node->title,
      '@new_value' => $new_value,
    ), WATCHDOG_WARNING, l(t('edit'), 'node/' . $node->nid . '/edit'));
  }
}