We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Problem, Shop

Unable to add product to cart

Hi,

I'm sure I'm doing something silly as this is the first time I've tried to use the perch_shop app but I cannot add a product to the cart.

Here is my template I'm using to display the product:

<h3 class="product__title">
    <img src="<perch:shop id="product_logo" type="image" label="Product Logo" order="4" width="600" />" alt="<perch:shop id="brand" type="shop_brand" label="Brand" allowempty="true" />" class="hero__item-product-logo  product__logo" />
</h3>

<h4 class="product__strapline"><perch:shop id="strapline" type="text" label="Strapline" order="3" markdown="true" /></h4>

<div class="product__media">
    <div class="product__featured-image" id="js-product-featured-image">
        <img src="<perch:shop id="image" type="image" label="Primary Product Image" order="4" width="600" />" alt="" />
    </div>

    <perch:repeater id="images" label="Additional Product Images" scope-parent="true">
        <perch:before><ul class="perch__additional-images" id="js-product-additional-images"></perch:before>
            <li><a href="<perch:shop id="image" type="image" width="800" />"><img src="<perch:shop id="image" type="image" width="40" height="40" density="1.6" crop="true" />" alt="<perch:shop id="parent.title" type="text" label="Title" required="true" order="2" /> image <perch:shop id="perch_item_index" />" /></a></li>
        <perch:after></ul></perch:after>
    </perch:repeater>
</div>

<div class="product__detail">
    <perch:shop id="description" type="textarea" label="Description" editor="markitup" order="3" markdown="true" size="s" />

    <perch:form id="add_to_cart" app="perch_shop" action="/cart">
    <perch:if exists="has_variants">
      <perch:productopts>
        <h3><perch:productopt id="title" /></h3>
        <perch:productvalues>
          <perch:before><ul></perch:before>
            <li>
              <label>
                <perch:input id="options" name="opt-<perch:productvalue id="optionID" />[]"
                  value="<perch:productvalue id="valueID" />" type="radio" required="required" />
                <perch:productvalue id="valueTitle" />
              </label>
            </li>
          <perch:after></ul></perch:after>
        </perch:productvalues>
      </perch:productopts>
    </perch:if>
    <perch:input id="product" type="hidden" value="<perch:shop id="productID" type="hidden" env-autofill="false" />" />
    <perch:input type="submit" value="Add to cart" />
  </perch:form>
</div>

I have /cart/index.php with the following content (this could be where I'm being stupid and missing a function call)

<?php require_once($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'manage' . DIRECTORY_SEPARATOR . 'runtime.php'); ?>

<?php perch_layout('head.default'); ?>

<?php perch_shop_cart(); ?>

<?php perch_layout('foot.default'); ?>

The debug output from my product.php page is as follows

Debug Message
[1] SELECT * FROM perch2_pages WHERE pagePath='/shop/product.php' LIMIT 1
[1] SELECT * FROM perch2_shop_cart WHERE cartID=30
[1] SELECT * FROM perch2_shop_cart WHERE cartID=30
[2] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/shop/product.php' OR regionPage='*' ORDER BY regionPage DESC
[7] 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
[3] 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', '13', '29', '30', '31', '7', '8') AND idx.itemID IN ('1', '13', '7') WHERE 1=1 AND ((idx.indexKey='slug' AND idx.indexValue='prismbrush')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey ) as tbl WHERE (productDeleted IS NULL) GROUP BY itemID, sortval ORDER BY sortval ASC
[23] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
[3] Using template: /templates/shop/products/product_detail.html
[1] SELECT * FROM perch2_shop_brands WHERE brandID='1' AND brandDeleted IS NULL LIMIT 1

and the debug details from my /cart/index.php page after submitting the form

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

Finally, here is my diagnostics report

PERCH INFORMATION

Perch: 2.8.31
Production mode: Production (100)
Installed apps: content (2.8.31), assets (2.8.31), categories (2.8.31), perch_forms (1.8.3), perch_shop_orders (1.0.6), perch_shop_products (1.0.6), perch_shop (1.0.6), perch_members (1.5)
DB driver: PDO
DB tables: perch2_categories (1), perch2_category_counts (0), perch2_category_sets (1), perch2_content_index (0), perch2_content_items (0), perch2_content_regions (6), perch2_forms (0), perch2_forms_responses (0), perch2_navigation (0), perch2_navigation_pages (0), perch2_page_templates (1), perch2_pages (3), perch2_resource_log (62), perch2_resource_tags (0), perch2_resources (54), perch2_resources_to_tags (0), perch2_settings (23), perch2_shop_addresses (0), perch2_shop_admin_index (60), perch2_shop_brands (1), perch2_shop_cart (30), perch2_shop_cart_data (0), perch2_shop_cart_items (0), perch2_shop_countries (250), perch2_shop_currencies (152), perch2_shop_customers (0), perch2_shop_emails (0), perch2_shop_index (484), perch2_shop_option_values (8), perch2_shop_options (2), perch2_shop_order_items (0), perch2_shop_order_promotions (0), perch2_shop_order_statuses (0), perch2_shop_orders (0), perch2_shop_orders_meta (1), perch2_shop_product_files (0), perch2_shop_product_option_values (13), perch2_shop_product_options (3), perch2_shop_product_tags (0), perch2_shop_products (43), perch2_shop_promotions (0), perch2_shop_search (7), perch2_shop_shipping_zone_countries (38), perch2_shop_shipping_zones (3), perch2_shop_shippings (2), perch2_shop_tax_exhibits (0), perch2_shop_tax_group_rates (1), perch2_shop_tax_groups (1), perch2_shop_tax_locations (1), perch2_shop_tax_rates (1), perch2_shop_variants (70), perch2_user_passwords (0), perch2_user_privileges (67), perch2_user_role_privileges (16), perch2_user_roles (2), perch2_users (1)
Users: 1
App runtimes:
<?php
$apps_list = [
    'content',
    'categories',
    'perch_members',
    'perch_shop',
    'perch_forms',
];
Editor plug-ins: markitup
H1: 5e4ee9e9f5765d0cd6345982f46ca7a9
L1: e7dfe6f7b1d72198c7c09fd59e40e942
F1: 2edba60ed1f613d6dd804feb202456a2
headerColour: #ffffff
content_singlePageEdit: 1
helpURL:
siteURL: /
hideBranding: 0
content_collapseList: 1
lang: en-gb
update_2.8.31: done
latest_version: 2.8.15
on_sale_version: 2.8.31
perch_shop_update: 1.0.6
headerScheme: light
perch_members_login_page: /members/login.php?r={returnURL}
perch_shop_price_tax_mode: exc
perch_shop_site_url: https://prismpot.com
perch_shop_product_url: /shop/product/{slug}
perch_shop_default_currency: 47
perch_shop_reporting_currency: 47
perch_shop_invoice_number_format: Invoice%d
dashboard: 0
hide_pwd_reset: 0
content_hideNonEditableRegions: 0
content_frontend_edit: 0
PERCH_DEVELOPMENT: 10
PERCH_STAGING: 50
PERCH_PRODUCTION: 100
PERCH_DB_USERNAME: root
PERCH_DB_SERVER: prismpot_prism_db_1
PERCH_DB_DATABASE: prismpot
PERCH_DB_PREFIX: perch2_
PERCH_TZ: UTC
PERCH_EMAIL_FROM: dave@unfinisheddev.com
PERCH_EMAIL_FROM_NAME: Dave Kirk
PERCH_LOGINPATH: /manage
PERCH_PATH: /opt/rh/httpd24/root/var/www/html/manage
PERCH_CORE: /opt/rh/httpd24/root/var/www/html/manage/core
PERCH_RESFILEPATH: /opt/rh/httpd24/root/var/www/html/manage/resources
PERCH_RESPATH: /manage/resources
PERCH_HTML5: 1
PERCH_DEBUG: 1
PERCH_RUNWAY:
PERCH_ERROR_MODE: DIE
PERCH_DATE_LONG: %d %B %Y
PERCH_DATE_SHORT: %d %b %Y
PERCH_TIME_SHORT: %H:%M
PERCH_TIME_LONG: %H:%M:%S
PERCH_RUNWAY_ROUTED:
PERCH_STRONG_PASSWORDS:
PERCH_PREVIEW_ARG: preview
PERCH_TEMPLATE_PATH: /opt/rh/httpd24/root/var/www/html/manage/templates
PERCH_DEFAULT_DOC: index.php
PERCH_DEFAULT_EXT: .php
PERCH_PRODUCTION_MODE: 100
PERCH_RWD:
PERCH_HTML_ENTITIES:
PERCH_SSL:
PERCH_STRIPSLASHES:
PERCH_PROGRESSIVE_FLUSH: 1
PERCH_PARANOID:
PERCH_FORCE_SECURE_COOKIES:
PERCH_PASSWORD_MIN_LENGTH: 6
PERCH_MAX_FAILED_LOGINS: 10
PERCH_AUTH_LOCKOUT_DURATION: 1 HOUR
PERCH_VERIFY_UPLOADS:
PERCH_AUTH_PLUGIN:
PERCH_DB_CHARSET: utf8
PERCH_DB_PORT:
PERCH_DB_SOCKET:
PERCH_SESSION_TIMEOUT_MINS: 20
PERCH_SHOP_VERSION: 1.0.6
PERCH_APPS_EDITOR_PLUGIN: markitup
PERCH_APPS_EDITOR_MARKUP_LANGUAGE: textile
HOSTING SETTINGS

PHP: 7.0.9
Zend: 3.0.0
OS: Linux
SAPI: apache2handler
Safe mode: not detected
MySQL client: mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $
MySQL server: 5.7.13
Free disk space: 87.64 GB
Extensions: Core, date, libxml, openssl, pcre, zlib, filter, hash, Reflection, SPL, session, standard, apache2handler, bz2, calendar, ctype, curl, dom, mbstring, fileinfo, ftp, gd, gettext, gmp, iconv, imagick, intl, json, exif, mysqlnd, mysqli, PDO, pdo_mysql, pdo_sqlite, Phar, posix, shmop, SimpleXML, soap, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xml, wddx, xmlreader, xmlwriter, xsl, zip, Zend OPcache, xdebug
GD: Yes
ImageMagick: Yes
PHP max upload size: 14M
PHP max form post size: 16M
PHP memory limit: 256M
Total max uploadable file size: 14M
Resource folder writeable: Yes
Session timeout: 24 minutes
Native JSON: Yes
Filter functions: Yes
Transliteration functions: Yes
UNIQUE_ID: V9Fxyh3ep1Vmfadwm85F-gAAAAQ
HTTP_HOST: prismpot.docker
HTTP_CONNECTION: keep-alive
HTTP_UPGRADE_INSECURE_REQUESTS: 1
HTTP_USER_AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_REFERER: http://prismpot.docker/manage/core/settings/diagnostics/
HTTP_ACCEPT_ENCODING: gzip, deflate, sdch
HTTP_ACCEPT_LANGUAGE: en-GB,en-US;q=0.8,en;q=0.6
HTTP_COOKIE: cmsa=1; PHPSESSID=5qf1dl2467vjnd9drvsnfv5853
PATH: /opt/rh/httpd24/root/usr/bin:/opt/rh/httpd24/root/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LD_LIBRARY_PATH: /opt/rh/httpd24/root/usr/lib64
SERVER_SOFTWARE: Apache/2.4.18 (Red Hat)
SERVER_NAME: prismpot.docker
SERVER_ADDR: 172.17.42.4
SERVER_PORT: 80
REMOTE_ADDR: 172.17.8.1
DOCUMENT_ROOT: /opt/rh/httpd24/root/var/www/html
REQUEST_SCHEME: http
CONTEXT_DOCUMENT_ROOT: /opt/rh/httpd24/root/var/www/html
SERVER_ADMIN: root@localhost
SCRIPT_FILENAME: /opt/rh/httpd24/root/var/www/html/manage/core/settings/diagnostics/index.php
REMOTE_PORT: 52407
GATEWAY_INTERFACE: CGI/1.1
SERVER_PROTOCOL: HTTP/1.1
REQUEST_METHOD: GET
QUERY_STRING: extended
REQUEST_URI: /manage/core/settings/diagnostics/?extended
SCRIPT_NAME: /manage/core/settings/diagnostics/index.php
PHP_SELF: /manage/core/settings/diagnostics/index.php
REQUEST_TIME_FLOAT: 1473343946.828
REQUEST_TIME: 1473343946
David Kirk

David Kirk 1 points

  • 4 years ago

And no sooner have I posted I found the issue with that - due to a htaccess rewrite rule the form action needed to be action="/cart/"

However it's still not adding the product to the cart and I'm getting the following error in the debug output

Debug Message
[1] SELECT * FROM perch2_pages WHERE pagePath='/cart/index.php' LIMIT 1
[1] SELECT * FROM perch2_shop_cart WHERE cartID=30
[1] SELECT * FROM perch2_shop_cart WHERE cartID=30
[23] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
[1] SELECT itemID, itemQty FROM perch2_shop_cart_items WHERE cartID=30 AND productID=0
[1] SELECT * FROM perch2_shop_products WHERE productID=0 AND productDeleted IS NULL LIMIT 1
Limiting based on stock.
------------------------------ Calculating cart ------------------------------
[1] SELECT * FROM perch2_shop_cart WHERE cartID=30
[1] SELECT * FROM perch2_shop_cart WHERE cartID=30
[1] SELECT * FROM perch2_shop_tax_locations WHERE locationID=1 AND locationDeleted IS NULL LIMIT 1
[1] SELECT locationID FROM perch2_shop_tax_locations WHERE locationIsHome=1 LIMIT 1
[1] SELECT * FROM perch2_shop_tax_locations WHERE locationID=1 AND locationDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch2_shop_currencies WHERE currencyID=47 LIMIT 1
[1] SELECT COUNT(productID) AS product_count, SUM(itemQty) AS item_count FROM perch2_shop_cart_items WHERE cartID=30
Tax mode: exc
[nil] SELECT * FROM perch2_shop_cart_items WHERE cartID=30 ORDER BY itemID ASC
UPDATE perch2_shop_cart SET cartTotalItems=NULL, cartTotalProducts='0', cartTotalWithTax='0.00', cartTotalWithoutTax='0.00' WHERE cartID='30'
Invalid query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cartTotalItems' cannot be null
INSERT INTO perch2_shop_cart_data(cartID,productID,cartData) VALUES('30','prismbrush','{\"product\":\"prismbrush\"}')
[2] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/cart/index.php' OR regionPage='*' ORDER BY regionPage DESC
Using template: /addons/apps/perch_shop/templates/shop/cart/cart.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does your add to cart form look like?

Hi Drew,

I was using this in my product_detail.html template to display the add to cart form, which I got from the examples section of the docs

<perch:form id="add_to_cart" app="perch_shop" action="/cart"> 
    <perch:if exists="has_variants"> 
        <perch:productopts> 
            <h3><perch:productopt id="title" /></h3> 
            <perch:productvalues> 
                <perch:before><ul></perch:before> 
                <li>
                    <label><perch:input id="options" name="opt-<perch:productvalue id="optionID" />[]" value="<perch:productvalue id="valueID" />" type="radio" required="required" /><perch:productvalue id="valueTitle" /></label> 
                </li> 
                <perch:after></ul></perch:after> 
            </perch:productvalues> 
         </perch:productopts> 
    </perch:if> 
    <perch:input id="product" type="hidden" value="<perch:shop id="productID" type="hidden" env-autofill="false" />" /> 
    <perch:input type="submit" value="Add to cart" /> 
</perch:form>

However, I have since found another example that you posted in another thread in the forum which uses a select box (which I also prefer from a UX point of view) and that is now working for me - so I either missed something when I took the example off of the docs or my setup isn't compatible with that type of form. This is the form I'm using now and everything is working

<perch:form id="add_to_cart" app="perch_shop" action="/cart/">
        <perch:if exists="has_variants">
            <perch:input id="product" type="select" options="<perch:shop id="_variant_opts" type="hidden" />" placeholder="Please choose" required="true" class="form-control" />
        <perch:else />
            <perch:input id="product" type="hidden" value="<perch:shop id="productID" type="hidden" env-autofill="false" />" /> 
        </perch:if>
    <perch:input type="submit" value="Add to cart" />
</perch:form>

I can't see a way to mark the thread as solved / fixed so please mark it as such if you want