Forum

Thread tagged as: Problem, Shop

Add to cart. Nothing happening. I'm stuck.

I'm implementing Shop on an existing Perch site. I've been struggling through so far getting product lists and products working. I'm now trying to implement the Cart but despite using only (copies of) default templates, I'm stuck.

Here's my 'Add to cart' form:

<perch:form id="add_to_cart" app="perch_shop" action="/shop/basket">
  <perch:input id="product" type="hidden" env-autofill="false" value="<perch:shop id="productID" type="hidden" env-autofill="false" />" />
  <perch:input type="submit" class="button" value="Add to cart" />
</perch:form>

Submitting the form on a product page (or list), takes me to the shop/basket/index.php page where I have:

perch_shop_cart();

But I just see the 'Your cart is empty' message. It appears items aren't being added and product_count is not greater than 0.

Am I missing a crucial step between the 'Add to Cart' form and the perch_shop_cart() function? I feel like I must be. Should there be something in between that explicitly adds the productID to the cart?

I'm no PHP master. In fact I'm pretty basic, but I've always managed fine with plain-ol-Perch. Really struggling to get my head around Shop though. Any help appreciated.

Pete Lambert

Pete Lambert 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you view source on the form in your browser, do you see the hidden field populated with the numeric product ID?

Yep.

<form id="form3_add_to_cart" action="/shop/basket" method="post">
  <input id="form3_product" name="product" value="2" type="hidden">
  <input class="button" value="Add to cart" type="submit">
  <input type="hidden" name="cms-form" value="YWRkX3RvX2NhcnQ6cGVyY2hfc2hvcDovdGVtcGxhdGVzL3Nob3AvcHJvZHVjdHMvcHJvZHVjdC5odG1sOjE0NjI4ODA2Nzk=">
</form>

I tried adding this to the cart page to see what happened:

perch_shop_add_to_cart(2)

and that seems to break the cart. It doesn't render the cart template or anything after it in the source.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Huh. Weird. Is there anything in your error log?

If you turn on debug, does that reveal anything?

Here's the debug output from the Cart page when accessed through a add_to_cart form:


[1] SELECT * FROM perch2_pages WHERE pagePath='/shop/basket/index.php' LIMIT 1 [1] SELECT * FROM perch2_shop_cart WHERE cartID=12 [1] SELECT * FROM perch2_shop_cart WHERE cartID=12 [1] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/shop/basket/index.php' OR regionPage='*' ORDER BY regionPage DESC [1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/treatments' OR pageSortPath='/treatments' LIMIT 1 [7] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-002%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/shop/basket/index.php' LIMIT 1 [2] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-007-003', '000-007', '000') ORDER BY pageTreePosition DESC [6] Using template: /templates/navigation/item.html [1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/shop' OR pageSortPath='/shop' LIMIT 1 [1] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-007%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/shop/basket/index.php' LIMIT 1 [2] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-007-003', '000-007', '000') ORDER BY pageTreePosition DESC Using cart from cache. [32] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 Using template: /templates/shop/cart/cart.html

I don't know about you but that's not telling me much. :-/

Nothing goin on in the PHP error log.

This is the error when I try the perch_shop_add_to_cart(2) though:

Fatal error: Call to a member function groupTaxRate() on boolean in /Users/petelambert/Working/innersense/perch/addons/apps/perch_shop/lib/PerchShop_Product.class.php on line 311

I don't know if that's relevant or not.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you set up a tax group and added the product to that group?

Ah! I hadn't. For some reason I'd removed the tax-group field from the product template.

So that now allows me to add a product using perch_shop_add_to_cart(2) but still no dice on adding items via the add_to_cart form.

But we're getting somewhere!

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does the debug output after submitting the form?


Debug Message [1] SELECT * FROM perch2_pages WHERE pagePath='/shop/basket/index.php' LIMIT 1 [1] SELECT * FROM perch2_shop_cart WHERE cartID=13 [1] SELECT * FROM perch2_shop_cart WHERE cartID=13 [1] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/shop/basket/index.php' OR regionPage='*' ORDER BY regionPage DESC [1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/treatments' OR pageSortPath='/treatments' LIMIT 1 [7] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-002%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/shop/basket/index.php' LIMIT 1 [2] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-007-003', '000-007', '000') ORDER BY pageTreePosition DESC [6] Using template: /templates/navigation/item.html [1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/shop' OR pageSortPath='/shop' LIMIT 1 [1] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-007%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/shop/basket/index.php' LIMIT 1 [2] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-007-003', '000-007', '000') ORDER BY pageTreePosition DESC Using cart from cache. [32] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 Using template: /templates/shop/cart/cart.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

It's like it's not seeing the form being posted at all.

I see you've got action="/shop/basket" set - is that URL accurate? You've not got the server configured to redirect to /shop/basket/ or something like that? That could throw away the posted data in the process of redirecting.

Hmm. I just changed the action to /shop/basket/index.php and it's started working.

Well, that's annoying.

Ugh. It was a bloody setting in MAMP. I'm so sorry for wasting your time today, Drew.

Actually, it wasn't even a setting in MAMP. It was just the lack of a trailing slash on the action directory:

action="/shop/basket"

didn't work.

action="/shop/basket/"

solved it.

Sorry again. That's had me tearing my hair out.

Drew McLellan

Drew McLellan 2638 points
Perch Support

No problem!