Add search results to Cart

Emory Walton

Joined: 2006-08-03
Posts: 12
Posted: Wed, 2010-05-19 04:25

Hello,

How can I include the option to add search results to the cart (for subsequent zip downloading) for those with access to the shopping cart? It might also be nice to have a checkbox with each image found from a search, that would determine if the item should be added to the cart. Something like what is now available when viewing your cart, except that now, only individual photos, or entire albums (not search results) can be added to the cart.

Thanks very much

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Mon, 2010-05-24 15:21

This issue bothered me for a long time so I had a friend figure it out. It looks like he modified two files in the search folder.

in search/templates/SearchShowAll.tpl after line 123 add

Quote:
<label style="display:block;"><input type="checkbox" class="batchAddCart" value="{$itemId}" /> Add to Cart</label>

in search/HighlightResults.js add this code at the bottom

Quote:
function addSelectedToCart(baseLink)
{
var items = '';
var counter = 0;
var input_obj = document.getElementsByTagName('input');
for (var i = 0; i < input_obj.length; i++)
{
if (input_obj[i].type == 'checkbox' && input_obj[i].className == 'batchAddCart' && input_obj[i].checked == true)
{
if (counter > 0)
{
items = items + ',';
}
counter++;
items = items + input_obj[i].value.toString();
input_obj[i].checked = false;
}
}
if (counter > 0)
{
//window.location = baseLink + "&g2_itemId=" + items;
document.getElementById('batchAddToCartBuffer').src = baseLink + "&g2_itemId=" + items;
document.getElementById('cartAddedConfirmMsg').innerHTML = counter + " item" + (counter > 1 ? "s" : "") + " added to cart";
}
}

Do yourself a favor and make friends with a CS major.

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Mon, 2010-05-24 15:37

Forgot something. You'll also need to add the following at the end of SearchShowAll.tpl just before </form>

Quote:
<br />
<input class="inputTypeSubmit" type="submit" onClick="addSelectedToCart('{g->url arg1="controller=cart.AddtoCart" arg2="return=true"}');return false;" value="Add Selected to Cart" />
<span id="cartAddedConfirmMsg" style="color: red;"></span>
</div>
</div>
<iframe id="batchAddToCartBuffer" name="batchAddToCartBuffer" style="display: none;"></iframe>

 
Emory Walton

Joined: 2006-08-03
Posts: 12
Posted: Wed, 2010-05-26 06:06

Thank you.
I really appreciate your help.
That got me half of the way there.
Now there are check boxes to add any of the images found as results from a search to the cart.
There is also a box at the bottom to add the checked images to the cart.
When I click on that box, I get a message that items were added to the cart.

However, things seem to end right after that.
There is no option to view the cart after adding the items from the search results.

To be thorough, I added one additional item to the cart just by opening an item (the conventional way, without a search).
Then I was able to view the cart, but unfortunately the items from the search did not appear in the cart.
They apparently were not added to the cart even though I saw the message indicating that they were.

So in summary there are still two outstanding problems.
1) I cannot access the cart after adding items from search results.
2) Items do not seem to be actually added to the cart, although all of the other mechanisms now seem to be in place.

Is it possible that there were additional edits that were omitted?

Just as a note for anyone else who tries this, in order to get it to the point where I now am,
I also had to de-activate then re-activate the Search and the Cart modules (in the Gallery administration of Plugins )

Thanks once again for your help.

 
Another_Matt

Joined: 2009-08-20
Posts: 40
Posted: Wed, 2010-05-26 12:36

Ok, looks like you'll need to modify modules/cart/AddToCart.inc as well. My copy follows below.

Quote:
<?php
/*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/

GalleryCoreApi::requireOnce('modules/cart/classes/CartHelper.class');

/**
* This controller will handle adding an item to the cart.
* @package Cart
* @subpackage UserInterface
* @author Bharat Mediratta <bharat@menalto.com>
* @version $Revision: 17580 $
*/
class AddToCartController extends GalleryController {

/**
* @see GalleryController::handleRequest
*/
function handleRequest($form) {
global $gallery;

$inputIdRaw = GalleryUtilities::getRequestVariables('itemId');
if (empty($inputIdRaw))
{
return array(GalleryCoreApi::error(ERROR_MISSING_VALUE), null);
}
if (stristr($inputIdRaw, ','))
{
$inputIdList = explode(',', $inputIdRaw);
}
else
{
$inputIdList = array($inputIdRaw);
}

$ids = array();
foreach ($inputIdList as $oneItemId)
{
list ($ret, $oneItem) = GalleryCoreApi::loadEntitiesById($oneItemId, 'GalleryItem');
if ($ret)
{
return array($ret, null);
}
// check permissions
list ($ret, $hasPermission) = GalleryCoreApi::hasItemPermission($oneItemId, 'core.view');
if ($ret)
{
return array($ret, null);
}
if (!$hasPermission)
{
//return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
continue; // skip this object
}

if ($oneItem->getCanContainChildren())
{
list ($ret, $childIds) = GalleryCoreApi::fetchChildDataItemIds($oneItem);
if ($ret) {
return array($ret, null);
}
$ids = array_merge($ids, $childIds);
}
else
{
$ids[] = $oneItem->getId();
}
}

/*list ($ret, $item) = $this->getItem();
if ($ret) {
return array($ret, null);
}
*/
/* Figure out what ids we care about */
/*if ($item->getCanContainChildren()) {
list ($ret, $ids) = GalleryCoreApi::fetchChildDataItemIds($item);
if ($ret) {
return array($ret, null);
}
} else {
$ids = array($item->getId());
}*/

$ret = CartHelper::addItemsToCart($ids);
if ($ret) {
return array($ret, null);
}

/* Prepare our results */
$returnUrl = GalleryUtilities::getRequestVariables('return');
$results['redirect']['href'] = $returnUrl;
$results['status'] = array();
$results['error'] = array();

return array(null, $results);
}
}
?>

Sorry I can't be more definitive on these mods but I'm a photographer by trade.

 
Emory Walton

Joined: 2006-08-03
Posts: 12
Posted: Sat, 2010-05-29 05:12

Thanks. Unfortunately, the revision to that module stopped me from being able to access the cart altogether.
The code is well annotated, and I didn't notice anything that would be unique to your installation.
I really would like to use this feature, so please let me know if you have any other suggestions.
Thanks once again for your help.