npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bva/recommendations

v0.0.10

Published

JS recommendations widget using Shopify native recommendations API

Downloads

13

Readme

Recommendations

JS wrapper around Shopify's Recommendation API that provides an easy to use interface to get HTML on the page.

Installation

If you're using Node you can install this package in the following ways:

Yarn

yarn add @bva/recommendations

NPM

npm install @bva/recommendations

If you're not using Yarn or NPM, see below in Usage for more info on adding the minified file directly to your projects.

Usage

Recommendations is a simple widget using Shopify's native recommendations API that can be included on any Shopify website. Check out how easy it is to implement using ES6 modules:

import Recommendations from '@bva/recommendations';

const config = {
  productId: 10590155084,
  limit: 5,
};
const recs = new Recommendations(config);
recs.initialize();

If you're not using NPM and ES6 import/export syntax, you can also grab the minified dist/recommendations.min.js and include it in your HTML before your custom JS. Its equally simple:

<body>
  <div data-recommendations-insertion></div>

  <script src="recommendations.min.js"></script>
  <script>
    var config = {
      productId: 10590155084,
      limit: 6
    };
    var recs = new Shopify.Recommendations(config);
    recs.initialize();
  </script>

Perfect for your new slate projects

Configuration Object

A configuration is required when initializing your Recommendations instance and uses the following properties:

  • productId - The ID of the product recommendations will be based off of
  • hiddenTag - (default: "recommendations::hide") Tag that hides specific products
  • insertion - (default: "[data-recommendations-insertion]") HTML element that the reviews will be inserted into
  • limit - (default: 4)The amount of products that will be displayed (max 10)
  • template - (see below for default) A function that returns a string of HTML for a single product

Product Id

The Product ID is what will be used in the request to Shopify to figure out which products to display. It must be passed to your instance upon instantiation via 1 of the following methods.

HTML (Recommended Method)

Add the data attribute data-recommendations-main-product-id to your insertion element and add the product id as a value to that. This is the recommended method because you'll usually have access to the Product ID via liquid. It is much easier for a future developer to read this and understand what's happening.

<div data-recommendations-insertion data-recommendations-main-product-id="1234567890"></div>

In Liquid

<div data-recommendations-insertion data-recommendations-main-product-id="{{ product.id }}"></div>

JS

Pass the productId value along with your configuration object.

const config = {
  productId: 1234567890,
  limit: 5,
};

const recs = Shopify.Recommendations(config);
recs.initialize();

In Liquid

<script>
  Shopify.customVariables = {
    productId: {{ product.id }}
  };
</script>
const config = {
  productId: window.Shopify.customVariables.productId,
  limit: 5,
};

...

Template

A template is the HTML that is displayed for each product in the recommendations widget. The template config option is a function that takes 1 argument product, which is the product object returned from Shopify. The return vaue should be a string of HTML. The product argument has access to all imformation that would usually be accessible when pulling product data from Shopify.

The default template looks like:

function(product) {
  return(`
    <div class="recommendation__product" data-recommendations-product-id="${product.id}">
      <a href="${product.url}" class="recommendation__wrapper">
        <div class="recommendation__product-image-wrapper">
          <img class="recommendation__product-image" src="${product.featured_image}" alt="${product_title} image" />
        </div>
        <div class="recommendation__product-details">
          <h3 class="recommendation__product-title">${product_title}</h3>
          <span class="recommendation__product-price">${product_price}</span>
        </div>
      </a>
    </div>
  `);
}

Methods

initialize

Initializes a Recommendations instance and adds it to the page.

const recs = Shopify.Recommendations({ variantId: 1234567890 });
recs.initialize();

reinitialize

Needs documentation

uninitialize

Needs documentation

updateOptions

Needs documentation

Events

  • recommendations.initialized: Fires when a recommendation instance is initialized. The callback you will find the product data in event.detail.data and the current instance in event.detail.context.

Changelog

To see any recent changes view the CHANGELOG.