ShoppingCart::get PHP Method

get() public method

Finds an existing order item.
public get ( Buyable $buyable, $customfilter = [] ) : OrderItem
$buyable Buyable
return OrderItem the item requested, or false
    public function get(Buyable $buyable, $customfilter = array())
    {
        $order = $this->current();
        if (!$buyable || !$order) {
            return false;
        }
        $buyable = $this->getCorrectBuyable($buyable);
        $filter = array('OrderID' => $order->ID);
        $itemclass = Config::inst()->get(get_class($buyable), 'order_item');
        $relationship = Config::inst()->get($itemclass, 'buyable_relationship');
        $filter[$relationship . "ID"] = $buyable->ID;
        $required = array('Order', $relationship);
        if (is_array($itemclass::config()->required_fields)) {
            $required = array_merge($required, $itemclass::config()->required_fields);
        }
        $query = new MatchObjectFilter($itemclass, array_merge($customfilter, $filter), $required);
        $item = $itemclass::get()->where($query->getFilter())->first();
        if (!$item) {
            return $this->error(_t("ShoppingCart.ItemNotFound", "Item not found."));
        }
        return $item;
    }

Usage Example

 /**
  * Check to see if the shopping cart only contains downloadable
  * products.
  *
  * @return Boolean
  */
 public function onlyDownloadable()
 {
     $cart = ShoppingCart::get();
     foreach ($cart->getItems() as $item) {
         if (!$item->FindStockItem() instanceof DownloadableProduct) {
             return false;
         }
     }
     return true;
 }
All Usage Examples Of ShoppingCart::get