How to add your own Shopp tag

How to add your own Shopp tag

After working with Shopp for some time you will be familiar with the shopp(‘object’, ‘property’, ‘options’) tag that is used all over the Shopp templates. These Shopp tags make it super easy to customize the display of your webshop. There are a lot of tags built-in, but what is even better… you can create your own tags!

Let’s say you would like to use a Shopp tag like shopp(‘product’, ‘qrcode’) to display a QR-code with the product url.

First step: Tell Shopp about this tag

Backup your functions.php file before proceeding, just so you can put the original file back in case things go south.

Add the following command to your functions.php file

add_filter('shopp_themeapi_product_qrcode',  'shopp_qr_code',  20,  3);

This tells Shopp to execute function shopp_qr_code(), whenever it sees a Shopp tag like shopp(‘product’, ‘qrcode’). We are using ‘shopp_themeapi_product_’ because product is the object we want to work with. If you wanted to add a feature to a cartitem you would use ‘shopp_themeapi_cartitem_’.

Second step: Add the function to execute the command

Now let’s add that function shopp_qr_code() to functions.php

function shopp_qr_code( $result, $options, $Product ) {
   $width        = 150;
   $height       = 150;
   $url          = shopp( $Product, 'get-url');
   $alt          = shopp( $Product, 'get-name');

   $result = sprintf(
      '<img src="//chart.apis.google.com/chart?cht=%1$s&chs=%2$dx%3$d&chl=%4$s&choe=%5$s" alt="%6$s" />',
      'qr',
      $width,
      $height,
      htmlspecialchars($url),
      'UTF-8',
      htmlspecialchars($alt)
   );

   return $result;
}

This code grabs the url and name of the product and uses those, along with some size settings, to create an <img src=”/> HTML tag. This procedure uses the ‘chart.apis.google.com’ service of Google. This service is deprecated but still works.

That is it!

Once the above code is added to your functions.php, you can see the code in action by
adding shopp(‘product’, ‘qrcode’) to your product.php template.

Image for Shopp is slow article donate-even-when-free link-to-php-article shopp-add-email-phone-to-receipt
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *