Commit d0a492d0 authored by Mike Stefanello's avatar Mike Stefanello
Browse files

Set the node expiration only when the node is purchased and published (via Rules). [#2266549]

parent 0b029a2a
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -115,8 +115,9 @@ function commerce_node_checkout_rules_nodes_from_order($order) {
      // We don't need this item.
      continue;
    }
    $line_items[] = $line_item_wrapper;
    $line_items[] = entity_load_single($line_item_wrapper->type(), $line_item_wrapper->getIdentifier());
    $nodes[] = $line_item_wrapper->commerce_node_checkout_node->value();
  }

  return array('line_items' => $line_items, 'nodes' => $nodes);
}
+0 −38
Original line number Diff line number Diff line
@@ -238,44 +238,6 @@ function commerce_node_checkout_expire_views_api() {
  );
}

/**
 * Implements hook_commerce_node_checkout_line_item_alter().
 */
function commerce_node_checkout_expire_commerce_node_checkout_line_item_alter(&$line_item, $product, $node) {
  // Load the interval field from the product.
  $interval = field_get_items('commerce_product', $product, 'commerce_node_checkout_expire');
  if ($interval && !empty($interval[0])) {
    // Default the expiration time to be based off of the current time
    $expiration_start = 'now';

    // Load the last line item for this node
    if ($last_line_item = commerce_node_checkout_expire_get_node_last_line_item($node)) {
      // Create a wrapper for the line item
      $wrapper = entity_metadata_wrapper('commerce_line_item', $last_line_item);

      // Extract the last expiration time for this node
      $last_expiration = $wrapper->commerce_node_checkout_expires->value();

      // See if the last expiration time is still in the future
      if (REQUEST_TIME < $last_expiration) {
        // Use the last expiration time to base the new expiration time
        // of off. This allows a user to purchase additional time for a node
        // and have the time append to the remaining time.
        $expiration_start = $last_expiration;
      }
    }

    // Create the expiration date object
    $due_date_obj = new DateObject($expiration_start);

    // Apply the date interval
    if (interval_apply_interval($due_date_obj, $interval[0])) {
      // Update the expiry field on the line item.
      $line_item->commerce_node_checkout_expires[LANGUAGE_NONE][0]['value'] = $due_date_obj->format('U');
    }
  }
}

/**
 * Form submit callback for the node type settings form.
 */
+60 −0
Original line number Diff line number Diff line
@@ -67,6 +67,16 @@ function commerce_node_checkout_expire_rules_action_info() {
    ),
    'group' => t('Commerce Node Checkout'),
  );
  $actions['commerce_node_checkout_expire_rules_set_line_item_expiration'] = array(
    'label' => t('Set line item expiration date'),
    'parameter' => array(
      'line_item' => array(
        'type' => 'commerce_line_item',
        'label' => t('Line item'),
      ),
    ),
    'group' => t('Commerce Node Checkout'),
  );
  return $actions;
}

@@ -121,3 +131,53 @@ function commerce_node_checkout_expire_rules_node_set_notification($node) {
function commerce_node_checkout_expire_rules_node_unset_notification($node) {
  $node->{COMMERCE_NODE_CHECKOUT_NOTIFICATION_FIELD}[LANGUAGE_NONE][0]['sent'] = 0;
}

/**
 * Rules action callback to set the expiration date for a line item.
 */
function commerce_node_checkout_expire_rules_set_line_item_expiration($line_item) {
  // Create a wrapper for the line item
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

  // Extract the product
  $product = $line_item_wrapper->commerce_product->value();

  // Extract the node
  $node = $line_item_wrapper->commerce_node_checkout_node->value();

  // Load the interval field from the product.
  $interval = field_get_items('commerce_product', $product, 'commerce_node_checkout_expire');

  // See if an interval is set for this products
  if ($interval && !empty($interval[0])) {
    // Default the expiration time to be based off of the current time
    $expiration_start = 'now';

    // Load the last line item for this node
    if ($last_line_item = commerce_node_checkout_expire_get_node_last_line_item($node)) {
      // Create a wrapper for the line item
      $wrapper = entity_metadata_wrapper('commerce_line_item', $last_line_item);

      // Extract the last expiration time for this node
      $last_expiration = $wrapper->commerce_node_checkout_expires->value();

      // See if the last expiration time is still in the future
      if (REQUEST_TIME < $last_expiration) {
        // Use the last expiration time to base the new expiration time
        // of off. This allows a user to purchase additional time for a node
        // and have the time append to the remaining time.
        $expiration_start = $last_expiration;
      }
    }

    // Create the expiration date object
    $due_date_obj = new DateObject($expiration_start);

    // Apply the date interval
    if (interval_apply_interval($due_date_obj, $interval[0])) {
      // Update the expiry field on the line item.
      $line_item_wrapper->commerce_node_checkout_expires->set($due_date_obj->format('U'));
      $line_item_wrapper->save();
    }
  }
}
+31 −0
Original line number Diff line number Diff line
@@ -98,5 +98,36 @@ function commerce_node_checkout_expire_default_rules_configuration() {
      ]
    }
  }');
  $items['commerce_node_checkout_set_node_expiration_date'] = entity_import('rules_config', '{ "commerce_node_checkout_set_node_expiration_date" : {
      "LABEL" : "Set the purchased node expiration date",
      "PLUGIN" : "reaction rule",
      "OWNER" : "rules",
      "TAGS" : [ "Commerce Node Checkout Expire" ],
      "REQUIRES" : [
        "commerce_node_checkout",
        "commerce_node_checkout_expire",
        "commerce_checkout"
      ],
      "ON" : { "commerce_checkout_complete" : [] },
      "DO" : [
        { "commerce_node_checkout_rules_nodes_from_order" : {
            "USING" : { "order" : [ "commerce-order" ] },
            "PROVIDE" : {
              "line_items" : { "line_items" : "Associated Line items" },
              "nodes" : { "nodes" : "Associated Nodes" }
            }
          }
        },
        { "LOOP" : {
            "USING" : { "list" : [ "line-items" ] },
            "ITEM" : { "line_item" : "Current list item" },
            "DO" : [
              { "commerce_node_checkout_expire_rules_set_line_item_expiration" : { "line_item" : [ "line_item" ] } }
            ]
          }
        }
      ]
    }
  }');
  return $items;
}