Templates

The Pricing Rules Relationship variable

To use, first create a Twig variable for your Pricing Rules Relationship field. In this example we are using the field handle salesRelationship, replace this with whatever handle you gave the field when you created it.

{% set saleRelationships = entry.salesRelationship %}

Then pass the variable to the getSaleIds function using the craft.pricingRulesRelationship variable.

{% set productIds = craft.pricingRulesRelationship.getSaleIds(saleRelationships) %}

This gives you an array of product IDs related to the Pricing Rules selected in the field. Expired rules are excluded automatically.

getSaleIds() also takes an optional second argument, hasStock, if you want to exclude out-of-stock products too (it defaults to false, so out-of-stock products are included unless you opt in):

{% set productIds = craft.pricingRulesRelationship.getSaleIds(saleRelationships, true) %}

Example dump of the array:

array:2 [▼
  0 => 3
  1 => 27
]

You can then pass into the products query using the id parameter.

{# Create a product query with the 'type' and 'limit' parameters #}
{% set newProducts = craft.commerce.products()
  .id(productIds)
  .all() %}

{# Display the products #}
{% for product in newProducts %}
  <h2>{{ product.title }}</h2>
  {{ product.summary|md }}
  <a href="{{ product.url }}">Learn more</a>
{% endfor %}

Special Offers Page Example

Here's a fuller example of a Special Offers Page, with pagination thrown in:

{% extends 'shop/_private/layouts' %}

{% set saleRelationships = entry.salesRelationship %}{# replace with your field handle #}
{% set productIds = craft.pricingRulesRelationship.getSaleIds(saleRelationships) %}

{% block main %}
   <!-- Template: {{ _self }}.twig -->
   <div class="sm:flex">
      <div class="sm:w-1/3 w-full">
         <h1 class="text-3xl block sm:inline">
            {{entry.title}}
         </h1>
         {{ entry.summary }}
      </div>
   </div>
   {% paginate craft.commerce.products().limit(6).id(productIds) as pageInfo, pageProducts %}
   
   {{ include('shop/products/_includes/grid', {
      products: pageProducts
   }) }}

   {{ include('shop/products/_includes/pagination', {
      products: pageProducts,
      pageInfo: pageInfo
   }) }}
{% endblock %}
Important

If the Pricing Rule has expired, it won't show in the template. Store Managers don't need to worry about manually turning off Pricing Rules Relationships. Out-of-stock products stay visible unless you pass hasStock: true to getSaleIds().