Forum

Thread tagged as: Problem, Addons, Shop

issues with perch_shop_cart

Hi,

I'm trying to integrate the shop app into my site (running on MAMP locally) and have hit difficulties when trying to add to, and display, the cart on a page. I have created a php page (index.php) for the main shopfront inside a folder called shop (which is located in the root directory of the site along with home.php contact.php etc. This is the same as I did for the Blog App. The shop index page is displaying products correctly as per the list.html template. However, when I attempt to add to cart (with the default action="shop/cart") I get an error that the URL doesn't exist on the server. So I tried creating a php file / page for the cart and adding the perch_shop_cart() php line to it (see cart.php below), then directing the list.html form action to cart.php. This returns a completely blank page with a php 500 internal server error. If I remove the command from cart.php to display the cart, the page displays blank but but appears (minus the cart obviously) only after hitting refresh. I can only assume that there is a way that the shop needs to be integrated that I've missed somewhere. As I say I've based the process on the instructions for installing the blog as there is only a tutorial covering initial install of shop. Which worked fine. Please let me know what obvious mistake I've made!

DIAGNOSTICS

Perch: 3.0.8, PHP: 7.0.15, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (3.0.8), assets (3.0.8), categories (3.0.8), perch_blog (5.5), perch_forms (1.9), perch_shop_orders (1.2.2), perch_shop_products (1.2.2), perch_shop (1.2.2), perch_members (1.6.2)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_forms', 'perch_blog', 'perch_members', 'perch_shop', ); ?>
PERCH_LOGINPATH: /cms
PERCH_PATH: /Users/Andy/Desktop/LB2/cms
PERCH_CORE: /Users/Andy/Desktop/LB2/cms/core
PERCH_RESFILEPATH: /Users/Andy/Desktop/LB2/cms/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: 0c66c2e1f82f9e0b7617b2cb8270f2c7
Resource folder writeable: Yes
HTTP_HOST: localhost:8888
DOCUMENT_ROOT: /Users/Andy/Desktop/LB2
REQUEST_URI: /cms/core/settings/diagnostics/
SCRIPT_NAME: /cms/core/settings/diagnostics/index.php

SHOP INDEX

<?php include('../cms/runtime.php');

      perch_layout('global.header');
?>

<!-- calls the products template from list.html-->

<?php perch_shop_products(); ?>


<!-- footer-->
  <div class="footer-basic">
        <footer>
            <div class="social"><a href="#"><i class="icon ion-social-instagram"></i></a><a href="#"><i class="icon ion-social-snapchat"></i></a><a href="#"><i class="icon ion-social-twitter"></i></a><a href="#"><i class="icon ion-social-facebook"></i></a></div>
            <ul class="list-inline">
                <li><a href="#">Home </a></li>
                <li><a href="#">Contact </a></li>
                <li><a href="#">FAQ </a></li>
                <li><a href="#">Terms</a></li>
                <li><a href="#">Privacy Policy</a></li>
            </ul>
            <p class="copyright">Let's Bake© 2017</p>
        </footer>
    </div>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.8.2/js/lightbox.min.js"></script>

</body>

</html>

PRODUCTS LISTING (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:categories id="category" set="products" label="Category">
    <perch:category id="catTitle" />
</perch:categories>
    <perch:if not-exists="has_variants">
        <perch:form id="add_to_cart" app="perch_shop" action="/shop/cart.php"> <!--have tried both this and just the "/shop/cart" default. default causes error of URL not found-->
            <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>

CART PHP

<?php include('../cms/runtime.php'); 

perch_layout('global.header'); 

?>

<?php perch_shop_cart()?>   

     <!--have tried calling cms runtime from various folder levels ../../ etc. no luck-->

       <div class="footer-basic">
        <footer>
            <div class="social"><a href="#"><i class="icon ion-social-instagram"></i></a><a href="#"><i class="icon ion-social-snapchat"></i></a><a href="#"><i class="icon ion-social-twitter"></i></a><a href="#"><i class="icon ion-social-facebook"></i></a></div>
            <ul class="list-inline">
                <li><a href="#">Home </a></li>
                <li><a href="#">Contact </a></li>
                <li><a href="#">FAQ </a></li>
                <li><a href="#">Terms</a></li>
                <li><a href="#">Privacy Policy</a></li>
            </ul>
            <p class="copyright">Let's Bake© 2017</p>
        </footer>
    </div>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.8.2/js/lightbox.min.js"></script>

</body>

</html>
Andy Taylor

Andy Taylor 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

This returns a completely blank page with a php 500 internal server error.

If you look in your error log, it'll tell you what the error is.

I'm getting this in the error log. Any help would be appreciated.


[21-Jun-2017 15:55:35 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_prices() on boolean in /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/lib/PerchShop_Cart.class.php:723 Stack trace: #0 /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/lib/PerchShop_Cart.class.php(244): PerchShop_Cart->calculate_cart() #1 /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/lib/PerchShop_Cart.class.php(123): PerchShop_Cart->recalculate_summary_data() #2 /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/lib/PerchShop_Runtime.class.php(205): PerchShop_Cart->add_to_cart('6', 1) #3 /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/runtime/forms.php(31): PerchShop_Runtime->add_to_cart_from_form(Object(PerchAPI_SubmittedForm)) #4 /Users/Andy/Desktop/LB2/cms/core/lib/Perch.class.php(131): perch_shop_form_handler(Object(PerchAPI_SubmittedForm)) #5 /Users/Andy/Desktop/LB2/cms/core/inc/forms.php(9): Perch->dispatch_form('add_to_cart:per...', Array, Array) #6 /Users/Andy/Desktop/LB2/cms/core/inc/forms.php(26): perch_find_posted_forms() #7 /Users/Andy/D in /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/lib/PerchShop_Cart.class.php on line 723
Drew McLellan

Drew McLellan 2638 points
Perch Support

For some reason it's not finding your product. What can you tell me about product ID 6?

Hi Drew,

I'm not sure where the ID is from. I added a product to the shop through the admin as per the instructions for installation just to get things up and running. Is there somewhere you think I should look specifically to get more detail for you? Perhaps I did or didn't do something when adding the products?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is that not the product you're trying to add to the cart?

Yes, sorry, there's only one product at the moment.

I've tried to delete and add a new product to see if the issue cleared but no joy. The error log is the same but with the new product ID of 7 (as expected). I've looked at /PerchShop_Cart.class.php:723 which shows as the following,


$item = $Product->get_prices($Item->itemQty(), $cart['cartPricing'], $tax_mode, $CustomerTaxLocation,

I've tried fiddling with various tax options, stock levels etc but no change.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have prices set for the selected currency?

Hi Drew, I've set currency to GPB and entered a price for the product of 1.00

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is the error still the same?

Hi Drew, prices were already set before. Sorry wasn't clear on that. Yes the error is still there and is the same.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What else can you tell me? I can't see why this wouldn't work from the information I have.

This error appears when I go to the single product view in my product.php page. Not sure if it's relevant?

[23-Jun-2017 13:40:20 UTC] PHP Warning:  Missing argument 1 for perch_shop_product(), called in /Users/Andy/Desktop/LB2/shop/product.php on line 6 and defined in /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/runtime/products.php on line 25
[23-Jun-2017 13:40:20 UTC] PHP Notice:  Undefined variable: slug in /Users/Andy/Desktop/LB2/cms/addons/apps/perch_shop/runtime/products.php on line 33

On another note, would the file structure of the site affect things? I have a folder called shop in the root directory with three files index.php , cart.php and product.php. All of the Perch templates displayed on those pages are kept in the Perch/Templates/Shop folder (including cart.html) where they were originally?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, how are you calling perch_shop_product() ?

This is product.php to display the product. It does show the product with the various things such as the price etc. and again from there add to cart goes to the blank page.

<?php include('../cms/runtime.php'); 

      perch_layout('global.header');

?>
 <?php perch_shop_product(); 

?>

<!-- footer-->
  <div class="footer-basic">
        <footer>
            <div class="social"><a href="#"><i class="icon ion-social-instagram"></i></a><a href="#"><i class="icon ion-social-snapchat"></i></a><a href="#"><i class="icon ion-social-twitter"></i></a><a href="#"><i class="icon ion-social-facebook"></i></a></div>
            <ul class="list-inline">
                <li><a href="#">Home </a></li>
                <li><a href="#">Contact </a></li>
                <li><a href="#">FAQ </a></li>
                <li><a href="#">Terms</a></li>
                <li><a href="#">Privacy Policy</a></li>
            </ul>
            <p class="copyright">Let's Bake© 2017</p>
        </footer>
    </div>
    <script src="../assets/js/jquery.min.js"></script>
    <script src="../assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.8.2/js/lightbox.min.js"></script>
</body>

</html>

And this is product.html

<perch:shop id="sku" type="text" label="SKU" required="true" order="1" />
<perch:shop id="title" type="text" label="Title" required="true" order="2" />
<perch:shop id="slug" type="slug" editable="true" indelible="true" label="Slug" for="title sku" order="10" divider-before="Meta data" />
<perch:shop id="description" type="textarea" label="Description" editor="markitup" order="3" markdown="true" size="s" />
<perch:shop id="image" type="image" label="Main product image" order="4" width="800" />
<perch:shop id="image" type="image" width="80" height="80" density="1.6" crop="true" />
<perch:shop id="status" type="shop_status" label="Status" />

<perch:shop id="brand" type="shop_brand" label="Brand" allowempty="true" />

<perch:categories id="category" set="products" label="Category">
    <perch:category id="catTitle" />
</perch:categories>

<perch:shop id="catalog_only" type="shop_catalog_only" label="Catalog only" />
<perch:shop id="price" type="shop_currency_value" label="Price" divider-before="Pricing" size="s" min="0" step="any" />
<perch:shop id="sale_price" type="shop_currency_value" label="Sale price" size="s" min="0" step="any" />
<perch:shop id="trade_price" type="shop_currency_value" label="Trade price" size="s" min="0" step="any" runway="true" />
<perch:shop id="on_sale" type="checkbox" value="1" label="Use sale price" />
<perch:shop id="tax_group" type="shop_tax_group" label="Tax group" required="true" />


<perch:shop id="stock_status" type="shop_stock_status" label="Stock status" divider-before="Stock" required="true" />
<perch:shop id="stock_level" type="number" label="Stock level" size="s" />
<perch:shop id="stock_location" type="shop_stock_location" label="Count stock" />
<perch:shop id="max_in_cart" type="number" label="Max quantity in cart" size="s" />

<perch:shop id="requires_shipping" type="shop_requires_shipping" label="Requires shipping" divider-before="Shipping" />
<perch:shop id="weight" type="number" label="Shipping weight" size="s" />
<perch:shop id="width" type="number" label="Width" size="s" />
<perch:shop id="height" type="number" label="Height" size="s" />
<perch:shop id="depth" type="number" label="Depth" size="s" />

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

Okay, so crucially, this was not a code issue. Something had gone wrong within the database on the local server. I created a new database and the problem seems to have gone away. Apologies if I've caused any wasted head scratching time! Thanks for all of your help, I wouldn't have got there If I hadn't gone back and started from scratch!