/** * @param WishList $list */ public static function set_current($list) { if ($list) { $list->write(false, false, true); } // force LastEdited to change self::$current = $list; }
/** * @param array $data */ public function addtowishlist(array $data) { if (!class_exists('WishList')) { user_error('Wish List module not installed.'); } $groupedProduct = $this->getController()->data(); if (empty($data) || empty($data['Product']) || !is_array($data['Product'])) { $this->sessionMessage(_t('GroupedCartForm.EMPTY', 'Please select at least one product.'), 'bad'); $this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this); return $response ? $response : $this->controller->redirectBack(); } $list = WishList::current(); foreach ($data['Product'] as $id => $prodReq) { if (!empty($prodReq['Quantity']) && $prodReq['Quantity'] > 0) { $prod = Product::get()->byID($id); if ($prod && $prod->exists()) { $buyable = $prod; if (isset($prodReq['Attributes'])) { $buyable = $prod->getVariationByAttributes($prodReq['Attributes']); if (!$buyable || !$buyable->exists()) { $this->sessionMessage("{$prod->InternalItemID} is not available with the selected options.", "bad"); $this->extend('updateErrorResponse', $this->request, $response, $groupedProduct, $data, $this); return $response ? $response : $this->controller->redirectBack(); } } $list->addBuyable($buyable); } } } $this->extend('updateGroupWishListResponse', $this->request, $response, $groupedProduct, $data, $this); return $response ? $response : $this->controller->redirect(WishListPage::inst()->Link()); }
public function testMultipleLists() { WishList::set_current(null); $m1 = $this->objFromFixture('Member', 'm1'); $m2 = $this->objFromFixture('Member', 'm2'); $m1->logIn(); $p1 = $this->objFromFixture('Product', 'p1'); $p2 = $this->objFromFixture('Product', 'p2'); $p3 = $this->objFromFixture('Product', 'p3'); // should be able to retrieve a list of lists $allLists = WishList::get_for_user(); $this->assertNotNull($allLists); $this->assertTrue($allLists instanceof DataList); // should initially be 0 lists $this->assertEquals(0, WishList::get_for_user()->count()); $this->assertEquals(0, WishList::get_for_user($m2)->count()); // current method should create one list $l1 = WishList::current(); $l1->write(); $l1->addBuyable($p1); //Debug::dump(array($l1, WishList::get_for_user()->sql(), WishList::get_for_user()->count(), WishList::get_for_user()->getIDList())); $this->assertEquals(1, WishList::get_for_user()->count()); $this->assertEquals(0, WishList::get_for_user($m2)->count()); // after manually creating a list there should be two lists $l2 = new WishList(array('OwnerID' => $m1->ID, 'Title' => 'Christmas')); $l2->write(); $this->assertEquals(2, WishList::get_for_user()->count()); // after adding a product to one list, it should not be present in the other one // but should still report that it is in a list $l2->addBuyable($p2); $this->assertTrue($p1->IsInWishList()); $this->assertTrue($l1->hasBuyable($p1)); $this->assertFalse($l2->hasBuyable($p1)); $this->assertTrue($p2->IsInWishList()); $this->assertFalse($l1->hasBuyable($p2)); $this->assertTrue($l2->hasBuyable($p2)); $this->assertFalse($p3->IsInWishList()); $this->assertFalse($l1->hasBuyable($p3)); $this->assertFalse($l2->hasBuyable($p3)); // after creating a list for the a different user and adding // an item to that list, the item should not report that it is // in a list and should not be present in any of the other lists $l3 = new WishList(array('OwnerID' => $m2->ID, 'Title' => 'Christmas for someone else')); $l3->write(); $l3->addBuyable($p3); $this->assertEquals(2, WishList::get_for_user()->count()); $this->assertEquals(1, WishList::get_for_user($m2)->count()); $this->assertFalse($p3->IsInWishList()); $this->assertFalse($l1->hasBuyable($p3)); $this->assertFalse($l2->hasBuyable($p3)); // Buyable should be able to exist in two lists at once $l2->addBuyable($p1); $this->assertTrue($p1->IsInWishList()); $this->assertTrue($l1->hasBuyable($p1)); $this->assertTrue($l2->hasBuyable($p1)); // removing an item from one list should not remove it from the other $l1->removeBuyable($p1); $this->assertTrue($p1->IsInWishList()); $this->assertFalse($l1->hasBuyable($p1)); $this->assertTrue($l2->hasBuyable($p1)); // after removing item from both lists it should report as not being in a list $l2->removeBuyable($p1); $this->assertFalse($p1->IsInWishList()); $this->assertFalse($l1->hasBuyable($p1)); $this->assertFalse($l2->hasBuyable($p1)); }
/** * @return WishList */ public function CurrentList() { if (!isset($this->wishList)) { $this->wishList = WishList::current(); } return $this->wishList; }
/** * @param SS_HTTPRequest $request * @param AjaxHTTPResponse $response * @param GroupedProduct $groupedProduct * @param array $data * @param GroupedCartForm $form [optional] */ public function updateGroupWishListResponse(&$request, &$response, $groupedProduct, $data, $form = null) { if ($request->isAjax() && $this->owner->getController()->hasExtension('AjaxControllerExtension')) { if (!$response) { $response = $this->owner->getController()->getAjaxResponse(); } $this->setupRenderContexts($response, $groupedProduct, $form); $response->triggerEvent('wishlistadd'); $response->triggerEvent('wishlistchange', array('action' => 'add')); $n = 0; foreach ($data['Product'] as $info) { if ($info['Quantity'] > 0) { $n++; } } $s = $n == 1 ? '' : 's'; $response->triggerEvent('statusmessage', "{$n} item{$s} added to " . WishList::current()->getTitle()); } }