/**
 * Handles requests to update a cart item.
 *
 * @param $nid
 *   Node id of the cart item.
 * @param $data
 *   Array of extra information about the item.
 * @param $qty
 *   The quantity of this item in the cart.
 * @param $cid
 *   The cart id. Defaults to NULL, which indicates that the current user's cart
 *   should be retrieved with uc_cart_get_id().
 */
function hook_uc_update_cart_item($nid, $data = array(), $qty, $cid = NULL)
{
    $cid = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
    $result = \Drupal::entityQuery('uc_cart_item')->condition('cart_id', $cid)->condition('nid', $nid)->condition('data', serialize($data))->execute();
    if (!empty($result)) {
        $item = \Drupal\uc_cart\Entity\CartItem::load(current(array_keys($result)));
        if ($item->qty->value != $qty) {
            $item->qty->value = $qty;
            $item->save();
        }
    }
}
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function addItem($nid, $qty = 1, $data = NULL, $msg = TRUE)
 {
     $node = Node::load($nid);
     if (is_null($data) || !isset($data['module'])) {
         $data['module'] = 'uc_product';
     }
     // Invoke hook_uc_add_to_cart() to give other modules a chance to affect the process.
     $result = \Drupal::moduleHandler()->invokeAll('uc_add_to_cart', array($nid, $qty, $data));
     if (is_array($result) && !empty($result)) {
         foreach ($result as $row) {
             if ($row['success'] === FALSE) {
                 // Module implementing the hook does NOT want this item added!
                 if (isset($row['message']) && !empty($row['message'])) {
                     $message = $row['message'];
                 } else {
                     $message = t('Sorry, that item is not available for purchase at this time.');
                 }
                 if (isset($row['silent']) && $row['silent'] === TRUE) {
                     return $this->getAddItemRedirect();
                 } else {
                     drupal_set_message($message, 'error');
                 }
                 // Stay on this page.
                 $query = \Drupal::request()->query;
                 return Url::fromRoute('<current>', [], ['query' => UrlHelper::filterQueryParameters($query->all())]);
             }
         }
     }
     // Now we can go ahead and add the item because either:
     //   1) No modules implemented hook_uc_add_to_cart(), or
     //   2) All modules implementing that hook want this item added.
     $result = \Drupal::entityQuery('uc_cart_item')->condition('cart_id', $this->id)->condition('nid', $nid)->condition('data', serialize($data))->execute();
     if (empty($result)) {
         // If the item isn't in the cart yet, add it.
         $item_entity = CartItem::create(array('cart_id' => $this->id, 'nid' => $nid, 'qty' => $qty, 'data' => $data));
         $item_entity->save();
         if ($msg) {
             drupal_set_message(t('<strong>@product-title</strong> added to <a href=":url">your shopping cart</a>.', ['@product-title' => $node->label(), ':url' => Url::fromRoute('uc_cart.cart')->toString()]));
         }
     } else {
         // If it is in the cart, update the item instead.
         if ($msg) {
             drupal_set_message(t('Your item(s) have been updated.'));
         }
         $item_entity = CartItem::load(current(array_keys($result)));
         $qty += $item_entity->qty->value;
         \Drupal::moduleHandler()->invoke($data['module'], 'uc_update_cart_item', array($nid, $data, min($qty, 999999), $this->id));
     }
     // Invalidate the cache.
     Cache::invalidateTags(['uc_cart:' . $this->id]);
     // Invalidate the cart order.
     // @todo Remove this and cache the order object with a tag instead?
     $session = \Drupal::service('session');
     $session->set('uc_cart_order_rebuild', TRUE);
     return $this->getAddItemRedirect();
 }