Forum

Thread tagged as: Problem, Shop

Getting Slug for Perch Shop

I'm just getting started with Perch Shop using vanilla Perch. I've gone through the initial setup, and have products displaying on my Shop page, when I specify a product slug on my individual product page that works as expected, but I'm having trouble getting it working for any product.

My product.php looks like this

<?php include('../perch/runtime.php');?>
<?php perch_layout('shop.header'); ?>

<div class="shop-header">
    <div class="container">
        <h1>Shop</h1>
    </div>
</div>

<div id="shop">

    <div class="container">

    <?php $product = perch_get('slug');

     perch_shop_product($product, [
            'template' => 'individual.html'
        ]); ?>

        <?php PerchUtil::output_debug(); ?>

    </div>

</div>

<?php perch_layout('global.footer'); ?>

<?php perch_get_javascript(); ?>

</body>
</html>

My list.html is

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

        <div class="primary-content">
<h1>Products</h1>
<ul class="listing">
</perch:before>
<li>
    <img src="<perch:shop id="image" type="image" label="Main product image" order="4" width="800" />" />
    <a href="/shop/<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.php">
            <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>

I'm obviously not getting the slug correctly, but not sure why or how to fix it. I'd be grateful for any pointers.

Phil Bowell

Phil Bowell 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's on your URL when it doesn't work?

The url is showing the slug, in this instance:

https://site.test/shop/the-jet-course-booklet-bk-jet-cmj

That slug matches the one displayed on the product in the admin.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Phil,

perch_get('slug') looks for /?slug=your-product-slug.

Are you using any rewrite rules?

Hi Hussein,

I have some for shop yes.

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteRule ^shop/([a-zA-Z0-9-/]+)$ shop/product.php?s=$1 [L]

</IfModule>
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Your rewrite is using ?s=, but you're using perch_get('slug') in your code. Try using perch_get('s') instead.

You sir, are a gentleman! It works now.

I think I had been using slug because that is the ID in product template.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Great!

For future reference, reading the perch_get() function documentation may be helpful: https://docs.grabaperch.com/functions/utilities/perch-get/