Forum

Thread tagged as: Shop

Shop: variant added to cart in addition to base product

I'm taking over a partially developed Shop site and am having a problem where "Add to Cart" causes a product with variants to be added to the cart twice. The product is added to the cart as both a variant and non-variant.

I'm using the code from https://docs.grabaperch.com/addons/shop/examples/add-to-cart-options/

              <perch:form id="add_to_cart" app="perch_shop" action="/shop/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 class="button dark" type="submit" value="Add to cart">
              </perch:form>

Here's the cart debug message I get when a variant product gets added to the cart:

Debug Message - Perch 3.1.3
[1] SELECT * FROM perch3_pages WHERE pagePath='/shop/cart.php' LIMIT 1
[1] SELECT * FROM perch3_shop_cart WHERE cartID=505
[1] SELECT * FROM perch3_shop_cart WHERE cartID=505
[29] SELECT DISTINCT settingID, settingValue FROM perch3_settings WHERE userID=0
[1] SELECT * FROM perch3_shop_products WHERE productID='26' AND productDeleted IS NULL LIMIT 1
[1] SELECT valueSKUCode FROM perch3_shop_option_values WHERE valueID IN ('1') AND optionID IN (1)
[1] SELECT productID FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL AND (sku REGEXP "[[:<:]]BLC[[:>:]]") LIMIT 1
[1] SELECT itemID, itemQty FROM perch3_shop_cart_items WHERE cartID=505 AND productID=295
[1] SELECT * FROM perch3_shop_products WHERE productID=295 AND productDeleted IS NULL LIMIT 1
INSERT INTO perch3_shop_cart_items(productID,itemQty,cartID) VALUES('295',1,'505')
------------------------------ Calculating cart ------------------------------
[1] SELECT * FROM perch3_shop_cart WHERE cartID=505
[1] SELECT * FROM perch3_shop_cart WHERE cartID=505
[1] SELECT * FROM perch3_shop_tax_locations WHERE locationID=1 AND locationDeleted IS NULL LIMIT 1
[1] SELECT locationID FROM perch3_shop_tax_locations WHERE locationIsHome=1 LIMIT 1
[1] SELECT * FROM perch3_shop_tax_locations WHERE locationID=1 AND locationDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_currencies WHERE currencyID=142 LIMIT 1
[1] SELECT COUNT(productID) AS product_count, SUM(itemQty) AS item_count FROM perch3_shop_cart_items WHERE cartID=505
Tax mode: inc
[1] SELECT * FROM perch3_shop_cart_items WHERE cartID=505 ORDER BY itemID ASC
[1] SELECT * FROM perch3_shop_products WHERE productID=295 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[5] SELECT productID, productVariantDesc, stock_level FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL ORDER BY productOrder ASC
[1] SELECT o.* FROM perch3_shop_options o, perch3_shop_product_options po WHERE o.optionID=po.optionID AND po.productID=26 AND o.optionDeleted IS NULL ORDER BY optionPrecendence ASC
[11] SELECT valueID AS id, valueTitle AS title, valueSKUCode AS skucode FROM perch3_shop_option_values WHERE optionID=1 AND valueDeleted IS NULL ORDER BY valueOrder ASC
[5] SELECT pov.prodoptID, pov.optionID, pov.valueID, o.optionTitle, ov.* FROM perch3_shop_product_option_values pov, perch3_shop_options o, perch3_shop_option_values ov WHERE pov.productID='26' AND pov.optionID=o.optionID AND pov.valueID=ov.valueID AND o.optionDeleted IS NULL AND ov.valueDeleted IS NULL AND o.optionID='1' ORDER BY o.optionPrecendence ASC, ov.valueOrder ASC
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_tax_groups WHERE groupID=1 AND groupDeleted IS NULL LIMIT 1
[1] SELECT r.rateValue FROM perch3_shop_tax_group_rates gr, perch3_shop_tax_rates r WHERE gr.rateID=r.rateID AND gr.groupID=1 AND gr.locationID=1
[1] SELECT * FROM perch3_shop_tax_groups WHERE groupID=1 AND groupDeleted IS NULL LIMIT 1
[1] SELECT r.rateValue FROM perch3_shop_tax_group_rates gr, perch3_shop_tax_rates r WHERE gr.rateID=r.rateID AND gr.groupID=1 AND gr.locationID=1
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[5] SELECT productID, productVariantDesc, stock_level FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL ORDER BY productOrder ASC
[1] SELECT o.* FROM perch3_shop_options o, perch3_shop_product_options po WHERE o.optionID=po.optionID AND po.productID=26 AND o.optionDeleted IS NULL ORDER BY optionPrecendence ASC
[11] SELECT valueID AS id, valueTitle AS title, valueSKUCode AS skucode FROM perch3_shop_option_values WHERE optionID=1 AND valueDeleted IS NULL ORDER BY valueOrder ASC
[5] SELECT pov.prodoptID, pov.optionID, pov.valueID, o.optionTitle, ov.* FROM perch3_shop_product_option_values pov, perch3_shop_options o, perch3_shop_option_values ov WHERE pov.productID='26' AND pov.optionID=o.optionID AND pov.valueID=ov.valueID AND o.optionDeleted IS NULL AND ov.valueDeleted IS NULL AND o.optionID='1' ORDER BY o.optionPrecendence ASC, ov.valueOrder ASC
[nil] SELECT * FROM perch3_shop_promotions WHERE promoFrom<='2019-01-06 01:43:00' AND promoTo>'2019-01-06 01:43:00' AND promoActive=1 AND promoDeleted IS NULL ORDER BY promoOrder ASC
UPDATE perch3_shop_cart SET cartTotalItems='1', cartTotalProducts='1', cartTotalWithTax='185.00', cartTotalWithoutTax='185.00' WHERE cartID='505'
INSERT INTO perch3_shop_cart_data(cartID,productID,cartData) VALUES('505','26','{\"opt-1\":[\"1\"],\"product\":\"26\"}')
[7] SELECT regionKey, regionHTML FROM perch3_content_regions WHERE regionPage='/shop/cart.php' OR regionPage='*' ORDER BY regionPage DESC
[1] SELECT itemID, itemQty FROM perch3_shop_cart_items WHERE cartID=505 AND productID=26
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
INSERT INTO perch3_shop_cart_items(productID,itemQty,cartID) VALUES('26',1,'505')
------------------------------ Calculating cart ------------------------------
[1] SELECT * FROM perch3_shop_cart WHERE cartID=505
[1] SELECT * FROM perch3_shop_cart WHERE cartID=505
[1] SELECT * FROM perch3_shop_tax_locations WHERE locationID=1 AND locationDeleted IS NULL LIMIT 1
[1] SELECT locationID FROM perch3_shop_tax_locations WHERE locationIsHome=1 LIMIT 1
[1] SELECT * FROM perch3_shop_tax_locations WHERE locationID=1 AND locationDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_currencies WHERE currencyID=142 LIMIT 1
[1] SELECT COUNT(productID) AS product_count, SUM(itemQty) AS item_count FROM perch3_shop_cart_items WHERE cartID=505
Tax mode: inc
[2] SELECT * FROM perch3_shop_cart_items WHERE cartID=505 ORDER BY itemID ASC
[1] SELECT * FROM perch3_shop_products WHERE productID=295 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[5] SELECT productID, productVariantDesc, stock_level FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL ORDER BY productOrder ASC
[1] SELECT o.* FROM perch3_shop_options o, perch3_shop_product_options po WHERE o.optionID=po.optionID AND po.productID=26 AND o.optionDeleted IS NULL ORDER BY optionPrecendence ASC
[11] SELECT valueID AS id, valueTitle AS title, valueSKUCode AS skucode FROM perch3_shop_option_values WHERE optionID=1 AND valueDeleted IS NULL ORDER BY valueOrder ASC
[5] SELECT pov.prodoptID, pov.optionID, pov.valueID, o.optionTitle, ov.* FROM perch3_shop_product_option_values pov, perch3_shop_options o, perch3_shop_option_values ov WHERE pov.productID='26' AND pov.optionID=o.optionID AND pov.valueID=ov.valueID AND o.optionDeleted IS NULL AND ov.valueDeleted IS NULL AND o.optionID='1' ORDER BY o.optionPrecendence ASC, ov.valueOrder ASC
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_tax_groups WHERE groupID=1 AND groupDeleted IS NULL LIMIT 1
[1] SELECT r.rateValue FROM perch3_shop_tax_group_rates gr, perch3_shop_tax_rates r WHERE gr.rateID=r.rateID AND gr.groupID=1 AND gr.locationID=1
[1] SELECT * FROM perch3_shop_tax_groups WHERE groupID=1 AND groupDeleted IS NULL LIMIT 1
[1] SELECT r.rateValue FROM perch3_shop_tax_group_rates gr, perch3_shop_tax_rates r WHERE gr.rateID=r.rateID AND gr.groupID=1 AND gr.locationID=1
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[5] SELECT productID, productVariantDesc, stock_level FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL ORDER BY productOrder ASC
[1] SELECT o.* FROM perch3_shop_options o, perch3_shop_product_options po WHERE o.optionID=po.optionID AND po.productID=26 AND o.optionDeleted IS NULL ORDER BY optionPrecendence ASC
[11] SELECT valueID AS id, valueTitle AS title, valueSKUCode AS skucode FROM perch3_shop_option_values WHERE optionID=1 AND valueDeleted IS NULL ORDER BY valueOrder ASC
[5] SELECT pov.prodoptID, pov.optionID, pov.valueID, o.optionTitle, ov.* FROM perch3_shop_product_option_values pov, perch3_shop_options o, perch3_shop_option_values ov WHERE pov.productID='26' AND pov.optionID=o.optionID AND pov.valueID=ov.valueID AND o.optionDeleted IS NULL AND ov.valueDeleted IS NULL AND o.optionID='1' ORDER BY o.optionPrecendence ASC, ov.valueOrder ASC
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[1] SELECT * FROM perch3_shop_tax_groups WHERE groupID=1 AND groupDeleted IS NULL LIMIT 1
[1] SELECT r.rateValue FROM perch3_shop_tax_group_rates gr, perch3_shop_tax_rates r WHERE gr.rateID=r.rateID AND gr.groupID=1 AND gr.locationID=1
[1] SELECT * FROM perch3_shop_tax_groups WHERE groupID=1 AND groupDeleted IS NULL LIMIT 1
[1] SELECT r.rateValue FROM perch3_shop_tax_group_rates gr, perch3_shop_tax_rates r WHERE gr.rateID=r.rateID AND gr.groupID=1 AND gr.locationID=1
[nil] SELECT * FROM perch3_shop_promotions WHERE promoFrom<='2019-01-06 01:43:00' AND promoTo>'2019-01-06 01:43:00' AND promoActive=1 AND promoDeleted IS NULL ORDER BY promoOrder ASC
UPDATE perch3_shop_cart SET cartTotalItems='2', cartTotalProducts='2', cartTotalWithTax='370.00', cartTotalWithoutTax='370.00' WHERE cartID='505'
[1] SELECT * FROM perch3_shop_products WHERE productID=26 AND productDeleted IS NULL LIMIT 1
[5] SELECT productID, productVariantDesc, stock_level FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL ORDER BY productOrder ASC
[1] SELECT o.* FROM perch3_shop_options o, perch3_shop_product_options po WHERE o.optionID=po.optionID AND po.productID=26 AND o.optionDeleted IS NULL ORDER BY optionPrecendence ASC
[11] SELECT valueID AS id, valueTitle AS title, valueSKUCode AS skucode FROM perch3_shop_option_values WHERE optionID=1 AND valueDeleted IS NULL ORDER BY valueOrder ASC
[5] SELECT pov.prodoptID, pov.optionID, pov.valueID, o.optionTitle, ov.* FROM perch3_shop_product_option_values pov, perch3_shop_options o, perch3_shop_option_values ov WHERE pov.productID='26' AND pov.optionID=o.optionID AND pov.valueID=ov.valueID AND o.optionDeleted IS NULL AND ov.valueDeleted IS NULL AND o.optionID='1' ORDER BY o.optionPrecendence ASC, ov.valueOrder ASC
[5] SELECT productID, productVariantDesc, stock_level FROM perch3_shop_products WHERE parentID=26 AND productDeleted IS NULL ORDER BY productOrder ASC
[1] SELECT o.* FROM perch3_shop_options o, perch3_shop_product_options po WHERE o.optionID=po.optionID AND po.productID=26 AND o.optionDeleted IS NULL ORDER BY optionPrecendence ASC
[11] SELECT valueID AS id, valueTitle AS title, valueSKUCode AS skucode FROM perch3_shop_option_values WHERE optionID=1 AND valueDeleted IS NULL ORDER BY valueOrder ASC
[5] SELECT pov.prodoptID, pov.optionID, pov.valueID, o.optionTitle, ov.* FROM perch3_shop_product_option_values pov, perch3_shop_options o, perch3_shop_option_values ov WHERE pov.productID='26' AND pov.optionID=o.optionID AND pov.valueID=ov.valueID AND o.optionDeleted IS NULL AND ov.valueDeleted IS NULL AND o.optionID='1' ORDER BY o.optionPrecendence ASC, ov.valueOrder ASC
[2] Using template: /templates/shop/cart/cart.html

Any ideas?

Shane Lenzen

Shane Lenzen 18 points

  • 2 years ago

Anyone have any ideas about what could cause this? I've hit a wall.

Hi Shane

I vaguely remember the example template (which I believe your template is based upon) not being very useful to my variant requirements when I last used shop but as that is nearly two years ago please excuse my poor memory.

In my shop build I have the conditional on whether the product has variants or not. If it does then it pulls in a variant template that uses a variant id form input to add the variant to the cart. If not then the standard product id form is displayed.

<perch:if exists="has_variants">
<perch:template path="variant_detail.html"/>
</perch:if>

Variant template extract…

<perch:variants>
<dl>
<dt><perch:variant id="productVariantDesc" type="text" /></dt>
<dd><perch:variant id="price" type="shop_currency_value" /></dd>
</dl>
<perch:form id="add_to_cart" app="perch_shop" action="/shop/basket">
<perch:input id="product" type="hidden" value="<perch:variant id="productID" type="hidden" env-autofill="false" />" />  
<perch:input type="submit" value="Add to cart"/>    
</perch:form>
</perch:variants>

Hope this helps somehow.

Jon

Thanks Jon. That makes total sense, but this isn't working for me: <perch:variant id="productID" type="hidden" env-autofill="false" />. It outputs this: <input id="form1_product" name="product" value="<perch:variant id=" type="hidden">">

Implemented like this:

            <perch:if id="catalog_only" value="0">
              <perch:form id="add_to_cart" app="perch_shop" action="/shop/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:input id="product" type="hidden" value="<perch:variant id="productID" type="hidden" env-autofill="false" />">
                <perch:else>
                  <perch:input id="product" type="hidden" value="<perch:shop id="productID" type="hidden" env-autofill="false" />">
                </perch:if>
                <perch:input class="button dark" type="submit" value="Add to cart">
              </perch:form>
            <perch:else>
              <p>This item is available in-store only.</p>
            </perch:if>

Hi Shane

Any variant tags need to be inside <perch:variants> pair tags. Works like categories if that makes sense.

Jon

Thanks Jon. Still isn't working, but I suspect something is wrong deeper in the build. Thanks for the help.