/** * Remove an item from the wishlist */ public function remove() { $f3 = \Base::instance(); // ----------------------------------------------------- // Start: validation // ----------------------------------------------------- // TODO validate the POST values // min: wishlistitem_hash $wishlistitem_hash = $this->inputfilter->clean($f3->get('PARAMS.wishlistitem_hash'), 'cmd'); // TODO if validation fails, respond appropriately if ($f3->get('AJAX')) { } else { } // ----------------------------------------------------- // End: validation // ----------------------------------------------------- // get the current user's wishlist, either based on session_id (visitor) or user_id (logged-in) $wishlist = \Shop\Models\Wishlists::fetch(); // remove the item try { $wishlist->removeItem($wishlistitem_hash); } catch (\Exception $e) { // TODO respond appropriately with failure message // return; } //echo \Dsc\Debug::dump( $wishlist ); // TODO respond appropriately // ajax? send response object // otherwise redirect to wishlist if ($f3->get('AJAX')) { } else { \Dsc\System::addMessage('Item removed from wishlist'); $f3->reroute('/shop/wishlist'); } }
/** * Get the current user's cart * * @return \Shop\Models\Carts */ public static function fetchForUser() { $cart = new static(); $identity = \Dsc\System::instance()->get('auth')->getIdentity(); $session_id = \Dsc\System::instance()->get('session')->id(); if (!empty($identity->id)) { $cart->load(array('user_id' => new \MongoId((string) $identity->id))); $cart->user_id = $identity->id; $session_cart = static::fetchForSession(); // if there was a user cart and there is a session cart, push all products in user cart to their wishlist if (!empty($session_cart->items) && !empty($cart->id) && $session_cart->id != $cart->id) { $wishlist = \Shop\Models\Wishlists::fetch(); $wishlist->merge($cart->cast()); $cart->remove(); $cart = $session_cart; $cart->user_id = $identity->id; $cart->save(); \Dsc\System::addMessage("All products from your previous cart were moved to your wishlist"); } // if there was no user cart but there IS a session cart, just add the user_id to the session cart and save it if (empty($cart->id) && !empty($session_cart->id)) { $cart = $session_cart; $cart->user_id = $identity->id; $cart->save(); } } return $cart; }