ShoppingCart::remove PHP Method

remove() public method

Remove an item from the cart.
public remove ( Buyable $buyable, integer $quantity = null, $filter = [] ) : boolean
$buyable Buyable
$quantity integer - number of items to remove, or leave null for all items (default)
return boolean success/failure
    public function remove(Buyable $buyable, $quantity = null, $filter = array())
    {
        $order = $this->current();
        if (!$order) {
            return $this->error(_t("ShoppingCart.NoOrder", "No current order."));
        }
        // If an extension throws an exception, error out
        try {
            $order->extend("beforeRemove", $buyable, $quantity, $filter);
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
        $item = $this->get($buyable, $filter);
        if (!$item) {
            return false;
        }
        //if $quantity will become 0, then remove all
        if (!$quantity || $item->Quantity - $quantity <= 0) {
            $item->delete();
            $item->destroy();
        } else {
            $item->Quantity -= $quantity;
            $item->write();
        }
        // If an extension throws an exception, error out
        // TODO: There should be a rollback
        try {
            $order->extend("afterRemove", $item, $buyable, $quantity, $filter);
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
        $this->message(_t("ShoppingCart.ItemRemoved", "Item has been successfully removed."));
        return true;
    }

Usage Example

Esempio n. 1
0
                    break;
                case '2':
                    try {
                        $x = new TempStock();
                        $x->liberarStockColor($id, $user);
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                    break;
                case '3':
                    try {
                        $x = new TempStock();
                        $x->liberarStockColorTalle($id, $user);
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                    break;
                default:
                    try {
                        $x = new TempStock();
                        $x->liberarStockComunes($id, $user);
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                    break;
            }
        }
        echo "Items carrito liberados :" . $shoppingCart->remove($id);
    }
}
die;
All Usage Examples Of ShoppingCart::remove