Forum

Thread tagged as: Question, Addons, Shop

Cart remains empty when adding to cart

Happily using shop on a standard license

The cart was collecting items as expected earlier on

Then I set up a /products.php?p={slug} query page successfully

Now the cart has stopped collecting items

Suggestions welcome thank you

Fred Stidston

Fred Stidston 0 points

  • 5 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

We need to see your Diagnostics Report and all the relevant code to help.

What debugging steps have you followed so far?

I've updated to 2.8.30

Product page

Debug Message
[1] SELECT * FROM perch2_pages WHERE pagePath='/shop/product.php' LIMIT 1
[1] SELECT * FROM perch2_shop_cart WHERE cartID=7
[1] SELECT * FROM perch2_shop_cart WHERE cartID=7
[1] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/shop/product.php' OR regionPage='*' ORDER BY regionPage DESC
[4] SELECT DISTINCT idx.itemID FROM perch2_shop_index idx JOIN perch2_shop_products main ON idx.itemID=main.productID AND idx.itemKey='productID' AND ((idx.indexKey='status' AND idx.indexValue='1')) GROUP BY idx.itemID HAVING COUNT(idx.itemID)=1
[4] SELECT DISTINCT idx.itemID FROM perch2_shop_index idx JOIN perch2_shop_products main ON idx.itemID=main.productID AND idx.itemKey='productID' AND ((idx.indexKey='parentID' AND idx.indexValue='')) GROUP BY idx.itemID HAVING COUNT(idx.itemID)=1
[1] SELECT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_shop_index idx JOIN perch2_shop_products main ON idx.itemID=main.productID AND idx.itemKey='productID' JOIN perch2_shop_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='productID' AND idx2.indexKey='_id' AND idx.itemID IN ('1', '4', '5', '6') AND idx.itemID IN ('1', '4', '5', '6') WHERE 1=1 AND ((idx.indexKey='slug' AND idx.indexValue='makrana-marble-1')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, productID ) as tbl WHERE (productDeleted IS NULL) GROUP BY itemID, sortval ORDER BY sortval ASC
[32] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
[1] Using template: /templates/shop/products/product.html
[1] SELECT * FROM perch2_shop_brands WHERE brandID='4' AND brandDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch2_shop_currencies WHERE currencyID=47 LIMIT 1
[1] SELECT * FROM perch2_shop_tax_groups WHERE groupID='3' AND groupDeleted IS NULL LIMIT 1

Cart page

Debug Message
[1] SELECT * FROM perch2_pages WHERE pagePath='/shop/cart/index.php' LIMIT 1
[1] SELECT * FROM perch2_shop_cart WHERE cartID=7
[1] SELECT * FROM perch2_shop_cart WHERE cartID=7
[1] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/shop/cart/index.php' OR regionPage='*' ORDER BY regionPage DESC
Using cart from cache.
[32] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Using template: /templates/shop/cart/cart.html
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Can you post your Diagnostics Report please?

We also need information about the steps you have taken and your code. Otherwise we are just guessing.

Diagnotstic Report

Perch: 2.8.30, PHP: 5.6.10, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (2.8.30), assets (2.8.30), categories (2.8.30), perch_blog (5.0), perch_shop_orders (1.0.6), perch_shop_products (1.0.6), perch_shop (1.0.6), perch_members (1.5)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_members', 'perch_shop', ); ?>
PERCH_LOGINPATH: /perch
PERCH_PATH: /Applications/MAMP/htdocs/perch
PERCH_CORE: /Applications/MAMP/htdocs/perch/core
PERCH_RESFILEPATH: /Applications/MAMP/htdocs/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: 2edba60ed1f613d6dd804feb202456a2
Resource folder writeable: Yes
HTTP_HOST: localhost:8888
DOCUMENT_ROOT: /Applications/MAMP/htdocs
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php

I have installed perch standard and added shop then followed the getting started to set up products brands and taxes.

I have added the perch shop calls following

The shop page

<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Perch Example Page</title>
</head>
<body>
    <h2>Shop</h2>
    <?php perch_shop_products(); ?>
</body>
</html>

The products page

<?php include('../perch/runtime.php'); ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Perch Example Page</title>
</head>
<body>
    <?php perch_shop_product(perch_get('p'));?>
</body>
</html>

The cart page

<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Perch Example Page</title>
</head>
<body>
    <h2>Checkout</h2>
    <?php perch_shop_cart(); ?>
</body>
</html>

Rachel Andrew

Rachel Andrew 394 points
Perch Support

What changed from when it was working to now? If you go back, does it work again? You will need to give us something to go on - that's going to involve you doing some debugging and finding out what has caused it to stop working.

Changing back seems to make no difference. I made a fresh install and repeated the steps to set-up a minimum scenario.

The cart page does not appear to recieve any post variables. The cart page does recieve GET form actions.

The following test returns nothing

Home

    <form action="shop/cart/index.php" method="post">
      <input type="hidden" id="test" name="test" value="10"/>
      <input type="submit"/>
    </form>

Cart

    <?php foreach ($_POST as $key => $value) {
      echo $key . ' => ' . $value;
    }
    ?>

The add to cart button has the same effect as the test. There are no carts added to the table perch2_shop_cart table.

SELECT * FROM perch2_shop_cart WHERE cartID=10
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have any server side redirects in place?

Hi, I don't have redirects

Drew McLellan

Drew McLellan 2638 points
Perch Support

Does POST work elsewhere on your site?

Yes POST is working

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you give an example?

The test example above works now! I may have reinstalled members

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great.

The cart remain empty however!

Beyond another reinstall and setup do you have any suggestions to get perch_shop_cart() working?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show us your template?

Yes this is cart/cart.html and products/list.html both are unedited

<div class="wrapper cols2-nav-right">   
    <div class="primary-content">

<perch:if id="product_count" match="gt" value="0">
    <h1>Your cart (<perch:shop id="item_count" />)</h1>
    <perch:form id="cart" app="perch_shop">

        <perch:input type="submit" value="Update" hidden="true" aria-hidden="true" />

        <table class="cart">
            <tr>
                <th></th>
                <th>Code</th>
                <th>Item</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
            <perch:cartitems>
                <tr>
                    <td><perch:cartitem id="image" type="image" width="80" height="80" density="1.6" crop="true" output="tag" /></td>
                    <td><perch:cartitem id="sku" /></td>
                    <td>
                        <b><perch:cartitem id="title" /></b>
                        <p><i><perch:cartitem id="variant_desc" /></i></p>
                        <div class="desc">
                            <perch:cartitem id="description" type="textarea" markdown="true" />
                        </div>
                        <button type="submit" class="remove_cart" name="del:<perch:cartitem id="identifier" />" value="1">
                            Remove from cart
                        </button>
                    </td>
                    <td><perch:input id="qty:<perch:cartitem id="identifier" />" value="<perch:cartitem id="quantity" />" type="number" min="0" /></td>
                    <td class="money"><perch:cartitem id="price_without_tax" /></td>
                    <td class="money"><perch:cartitem id="total_without_tax_formatted" /></td>
                </tr>
            </perch:cartitems>

            <perch:if id="total_discounts" match="gt" value="0.00">
                <tr class="total">
                    <th colspan="5">
                        Discount (<perch:shop id="discount_code" />)
                    </th>
                    <td class="money">
                        <perch:shop id="total_discounts_formatted" />
                    </td>
                </tr>
                <tr class="total">
                    <th colspan="5">
                        Total
                    </th>
                    <td class="money">
                        <perch:shop id="total_items_discounted_formatted" />
                    </td>
                </tr>
            <perch:else />
                <tr class="total">
                    <th colspan="5">
                        Total
                    </th>
                    <td class="money">
                        <perch:shop id="total_items_formatted" />
                    </td>
                </tr>
            </perch:if>
            <tr class="total">
                <th colspan="5">
                    Tax
                </th>
                <td class="money">
                    <perch:shop id="total_tax_formatted" />
                </td>
            </tr>
            <tr class="total">
                <th colspan="5">
                    Grand total
                </th>
                <td class="money">
                    <perch:shop id="grand_total_formatted" />
                </td>
            </tr>
        </table>

        <div class="discount">
            <perch:label for="discount">
                Discount code
                <perch:input type="text" id="discount_code" />
            </perch:label>
        </div>

        <perch:input type="submit" value="Update" />

    </perch:form>

    <p>
        <a href="checkout">Checkout now</a>
    </p>

<perch:else />
<h1>Your cart</h1>

<p>Your cart is empty.</p>
</perch:if>





</div>
</div>





<style>
.cart {
    width: 100%;
    border-collapse: collapse;
}

.cart th {
    font-weight: 600;
}

.cart td,
.cart th {
    border: 1px solid #efefef;
    vertical-align: top;
    padding: 3px;
}

.cart .total,
.cart .money {
    text-align: right;
}

.discount {
    max-width: 300px;
}

.desc {
    font-size: small;
}

b {
    font-weight: 600;
    color: rgba(0, 0, 0, 0.6);
}

i {
    font-style: italic;
    font-size: small;
    color: rgba(0, 0, 0, 0.6);
}

.remove_cart {
    background: none;
    border: 0;
    padding: 0;
    margin: 0;
    text-decoration: underline;
}

input[type=number] {
    width: 35px;
}
</style>

products/list.html

<perch:before>
    <div class="wrapper cols2-nav-right">

        <div class="primary-content">
<h1>Products</h1>
<ul class="listing">
</perch:before>
<li>
    <a href="/shop/products/<perch:shop id="slug" type="slug" />/">
        <perch:shop id="title" type="text" />
    </a>

    <perch:if not-exists="has_variants">
        <perch:form id="add_to_cart" app="perch_shop" action="/shop/cart">
            <perch:input id="product" type="hidden" value="<perch:shop id="productID" env-autofill="false" />" />
            <perch:input type="submit" value="Add to cart" />
        </perch:form>
    </perch:if>
</li>
<perch:after>
</ul>
</div>
</div>
</perch:after>


Drew McLellan

Drew McLellan 2638 points
Perch Support

Does it make a difference if you change this:

<perch:input id="product" type="hidden" value="<perch:shop id="productID" env-autofill="false" />" />

to this?

<perch:input id="product" type="hidden" env-autofill="false" value="<perch:shop id="productID" env-autofill="false" />" />

No, the result is the same.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What is the value of the hidden field when you view source?

A long string

YWRkX3RvX2NhcnQ6cGVyY2hfc2hvcDovdGVtcGxhdGVzL3Nob3AvcHJvZHVjdHMvbGlzdC5odG1sOjE0Njc2NTM5MzU=