Forum

Thread tagged as: Problem, Shop

Add to cart form not working.

The add to cart form doesn't seem to be working. I get redirected to the cart page, but the cart remains empty. I can programmatically add items to my cart, but the form doesn't work...

Diagnostic:

Perch: 2.8.29, PHP: 5.6.10, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (2.8.29), assets (2.8.29), categories (2.8.29), perch_blog (5.0), perch_events (1.9.2), perch_forms (1.8.3), perch_shop_orders (1.0), perch_shop_products (1.0), perch_shop (1.0.1), perch_members (1.5), perch_twitter (3.5.1)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_forms', 'perch_members', 'perch_shop', 'perch_events', 'perch_twitter', );
PERCH_LOGINPATH: /perch
PERCH_PATH: /PATH_TO/perch
PERCH_CORE: /PATH_TO/perch/core
PERCH_RESFILEPATH: /PATH_TO/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: local.include-ed.org.uk
DOCUMENT_ROOT: /PATH_TO
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php

I'm listing products by category, using the following:

<?php
    // Get the categories we need
    $cats = perch_categories(array(
        'set' => 'products',
        'skip-template' => true,
        'raw' => true,
        'filter' => 'catParentID',
        'match' => 'eq',
        'value' => 1
    ));
    // Loop through, output the heading and the products
    foreach($cats as $cat)
    {
?>
        <h2><?php echo $cat['catTitle']; ?></h2>
        <?php 
            perch_shop_products(array(
                'category' => $cat['catPath'],
                'template' => 'products/list_tests',
            ));
        ?>
<?php
    }
?>

This is in a layout, which I'm including in my Blocks template. That doesn't seem to be the issue though (I tried just including the layout to the page template and still had the same issue)

For completeness, here's my list_tests template:

<perch:before>
<table class="table tests">
    <tbody>
</perch:before>
<tr>
    <th>
        <perch:shop id="title" type="text" />
    </th>
    <td>
        <perch:if exists="on_sale">
            <perch:shop id="sale_price" type="shop_currency_value" label="Price" size="m" min="0" step="any" />
            <p>Sale price! Usually <perch:shop id="price" type="shop_currency_value" label="Price" size="m" min="0" step="any" /></p>
        <perch:else />
            <perch:shop id="price" type="shop_currency_value" label="Price" size="m" min="0" step="any" />
        </perch:if>
    </td>
    <td>
    <perch:if not-exists="has_variants">
        <perch:form id="add_to_cart" app="perch_shop" action="/cart">
            <perch:input id="product" type="hidden" value="<perch:shop id="productID" env-autofill="false" />" />
            <perch:input type="submit" value="Add to cart" class="btn" />
        </perch:form>
    </perch:if>
    </td>
</tr>
<perch:after>
    </tbody>
</table>
</perch:after>

and here's the source of one of the forms on the page:

        <form id="form1_add_to_cart" action="/cart" method="post">
            <input id="form1_product" name="product" value="2" type="hidden" />
            <input class="btn" value="Add to cart" type="submit" /><input type="hidden" name="cms-form" value="YWRkX3RvX2NhcnQ6cGVyY2hfc2hvcDovdGVtcGxhdGVzL3Nob3AvcHJvZHVjdHMvbGlzdF90ZXN0cy5odG1sOjE0NjMwNjMzMTg=" />
        </form>
Paul Bell

Paul Bell 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That certainly looks like it should work. Does debug turn anything up?

Thanks, Drew. I had a look in Debug. It included the "calculating cart" section if I manually added a product (via perch_shop_add_to_cart(8); or similar), but not if I submitted the form.

I tried print_r($_POST); which was showing up an empty array. Adding a trailing slash to the action of the form:

action="/cart/"

rather than

action="/cart"

seems to have sorted it. The page now sees the POST array, and the product is added. Thanks for pointing me in the right direction. Wonder if there's a redirect in play that causes the issue (I've come across that one a couple of times with the forms app). I removed everything in my .htaccess, but I guess it could be because my cart file had been index.php in a cart folder.

Paul Bell said:

Thanks, Drew. I had a look in Debug. It included the "calculating cart" section if I manually added a product (via perch_shop_add_to_cart(8); or similar), but not if I submitted the form.

I tried print_r($_POST); which was showing up an empty array. Adding a trailing slash to the action of the form:

action="/cart/"

rather than

action="/cart"

seems to have sorted it. The page now sees the POST array, and the product is added. Thanks for pointing me in the right direction. Wonder if there's a redirect in play that causes the issue (I've come across that one a couple of times with the forms app). I removed everything in my .htaccess, but I guess it could be because my cart file had been index.php in a cart folder.

Thanks for the post Paul, same issue for me and that fixed it.

just worked it out.

the product I setup, with a Size option is broken!!

I created a new product, and the options size, small medium and large now when I do products/options ticking 'option Size', now shows the size and the values..

some how I broke the product when adding size, with no values

tony