Display quantity rules and volume pricing on your store
This page was printed on Jul 27, 2023. For the current version, visit https://help.shopify.com/en/manual/b2b/catalogs/code.
Quantity rules for B2B catalog are supported onfree Shopify themes , version 8.0.0 or later, and volume pricing is supported on version 11.0.0 or later. If you want to display quantity rules and volume pricing on your store, then it's suggested that youupdate your store's theme to the latest version.
If you don't want to change orupdate your theme , then you can add the following code to your theme to display quantity rules and volume pricing. Before you make updates to your theme files, ensure that youduplicate your theme to create a backup copy.
Grow your business
This is an advanced tutorial. If you're not comfortable reading and editing theme code, then you can work with a developer orhire a Shopify Expert .
产品车数量
A product variant's cart quantity value can be displayed on the product page or featured product section. The value can be fetched by using Liquid.
Add Liquid product cart quantity code
You can add code to the following files in your theme to support cart quantity:
main-product.liquid
or equivalent
featured-product.liquid
or equivalent
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open the file that you want to edit.
Create a new line at the bottom of the file, and add the following code:
{% comment %} Cart quantity {% endcomment %} {{ section . id }} " data-product-url="{{ product . url }} " data-section-id="{{ section . id }} " data-variant-id="{{ product . selected_or_first_available_variant . id }} ">{{ cart | line_items_for : product | sum : 'quantity' }} {{- 'products.product.quantity.in_cart' | t : quantity : cart_qty -}}
ClickSave .
Add Javascript cart quantity code
When a variant's cart quantity changes, the value displayed on the product page or featured product section must be updated. The updated value can be fetched by using Javascript code.
You can add code to thetheme.js
file or equivalent.
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open thetheme.js
file.
Create a new line at the bottom of the file, and add the following code:
let productUrl = document . querySelector ( ' [data-product-url] ' ). dataset . productUrl ; let sectionId = document . querySelector ( ' [data-section-id] ' ). dataset . sectionId ; let variantId = document . querySelector ( ' [data-variant-id] ' ). dataset . variantId ; // Fetch updated HTML from Section Rendering API fetch ( ` ${ productUrl } ?section_id= ${ sectionId } &variant= ${ variantId } ` ) . then (( response ) => response . text ()) . then (( responseText ) => { // Replace the current HTML in DOM with the updated HTML const updatedHtml = new DOMParser (). parseFromString ( responseText , ' text/html ' ); // Update the cart quantity const currentCartQuantity = document . querySelector ( `#CartQuantity- ${ sectionId } ` ); const updatedCartQuantity = updatedHtml . querySelector ( `#CartQuantity- ${ sectionId } ` ); currentCartQuantity . innerHTML = updatedCartQuantity . innerHTML ; });
ClickSave .
Quantity rules
A product variant's quantity rules can be displayed on the product page or featured product section. The rules can be fetched by using Liquid.
Add Liquid quantity rules code
You can add code to the following files in your theme to support quantity rules:
main-product.liquid
or equivalent
featured-product.liquid
or equivalent
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open the file that you want to edit.
Create a new line at the bottom of the file, and then add the following code:
{% comment %} Quantity rules {% endcomment %} {{section . id }} " data-product-url="{{ product . url }} " data-section-id="{{ section . id }} " data-variant-id="{{ product . selected_or_first_available_variant . id }} ">{%- if product . selected_or_first_available_variant . quantity_rule . increment > 1 -%} {{- 'products.product.quantity.multiples_of' | t : quantity : product . selected_or_first_available_variant . quantity_rule . increment -}} {%- endif -%} {%- if product . selected_or_first_available_variant . quantity_rule . min > 1 -%} - {{- 'products.product.quantity.minimum_of' | t : quantity : product . selected_or_first_available_variant . quantity_rule . min -}} {%- endif -%} {%- if product . selected_or_first_available_variant . quantity_rule . max != null -%} - {{- 'products.product.quantity.maximum_of' | t : quantity : product . selected_or_first_available_variant . quantity_rule . max -}} {%- endif -%}
ClickSave .
Add Javascript quantity rules code
Each variant of a product can have its own set of quantity rules. After a different variant is selected, the quantity rules displayed on a product page or featured product section need to be updated. The updated value can be fetched using Javascript code.
theme.js
or equivalent
en.default.json
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open thetheme.js
file.
Create a new line at the bottom of the file, and then add the following code:
let productUrl = document . querySelector ( ' [data-product-url] ' ). dataset . productUrl ; let sectionId = document . querySelector ( ' [data-section-id] ' ). dataset . sectionId ; let variantId = document . querySelector ( ' [data-variant-id] ' ). dataset . variantId ; // `variantId` is set to the current variant's id. Replace this value with the updated variant's id // Fetch updated HTML from Section Rendering API fetch ( ` ${ productUrl } ?section_id= ${ sectionId } &variant= ${ variantId } ` ) . then (( response ) => response . text ()) . then (( responseText ) => { // Replace the current HTML in DOM with the updated HTML const updatedHtml = new DOMParser (). parseFromString ( responseText , ' text/html ' ); // Update the quantity rules const currentQuantityRules = document . querySelector ( `#QuantityRules- ${ sectionId } ` ); const updatedQuantityRules = updatedHtml . querySelector ( `#QuantityRules- ${ sectionId } ` ); currentQuantityRules . innerHTML = updatedQuantityRules . innerHTML ; });
ClickSave .
Volume pricing
Add Liquid volume pricing code
You can add code to the following files in your theme to display volume pricing:
main-product.liquid
or equivalent
featured-product.liquid
or equivalent
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open the file that you want to edit.
Create a new line at the bottom of the file, and then add the following code:
{%- if product . quantity_price_breaks_configured? -%} {{ 'products.product.volume_pricing.note' | t }}
{{section . id }} ">{%- if product . selected_or_first_available_variant . quantity_price_breaks . size > 0 -%} {{ 'products.product.volume_pricing.title' | t }} {{ product . selected_or_first_available_variant . quantity_rule . min }} +{%- assign price = product . selected_or_first_available_variant . price | money_with_currency -%} {{ “products.product.volume_pricing.price_at_each” | t : price : variant_price }} ">{{ 'sections.quick_order_list.each' | t : money : price -}} {%- for price_break in product.selected_or_first_available_variant.quantity_price_breaks -%} {%- assign price_break_price = price_break . price | money_with_currency -%} {%-if forloop.index >= 3 -%} show-more-item hidden{%- endif -%} "> {{- price_break . minimum_quantity -}} + {%- assign price = price_break . price | money_with_currency -%} {{ “products.product.volume_pricing.price_at_each” | t : price : price_break_price }} ">{{ 'sections.quick_order_list.each' | t : money : price -}} {%- endfor -%} {%- if product . selected_or_first_available_variant . quantity_price_breaks . size >= 3 -%} {{section . id }} " type="button" > + {{ 'products.facets.show_more' | t }} {%- endif -%} {%- endif -%}
ClickSave .
Add Javascript volume pricing code
You can add code to the following file in your theme to display volume pricing:
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open thetheme.js
file.
Create a new line at the bottom of the file, and then add the following code:
if ( ! customElements . get ( ' show-more-button ' )) { customElements . define ( ' show-more-button ' , class ShowMoreButton extends HTMLElement { constructor () { super (); const button = this . querySelector ( ' button ' ); button . addEventListener ( ' click ' , ( event ) => { this . expandShowMore ( event ); const nextElementToFocus = event . target . closest ( ' .parent-display ' ). querySelector ( ' .show-more-item ' ); if ( nextElementToFocus && ! nextElementToFocus . classList . contains ( ' hidden ' ) && nextElementToFocus . querySelector ( ' input ' )) { nextElementToFocus . querySelector ( ' input ' ). focus (); } }); } expandShowMore ( event ) { const parentDisplay = event . target . closest ( ' [id^="Show-More-"] ' ). closest ( ' .parent-display ' ); const parentWrap = parentDisplay . querySelector ( ' .parent-wrap ' ); this . querySelectorAll ( ' .label-text ' ). forEach (( element ) => element . classList . toggle ( ' hidden ' )); parentDisplay . querySelectorAll ( ' .show-more-item ' ). forEach (( item ) => item . classList . toggle ( ' hidden ' )); if ( ! this . querySelector ( ' .label-show-less ' )) { this . classList . add ( ' hidden ' ); } } } ); }
ClickSave .
Add locales
Add JSON translation strings
Steps:
From your Shopify admin, go to欧宝体育官网入口首页 >Themes .
Find the theme that you want to edit, click the... button to open the actions menu, and then clickEdit code .
Open theen.default.json
file.
Create a new line at the bottom of the file, and then add the following code:
" products " : { " product " : { " volume_pricing " : { " title " : " Volume Pricing " , " note " : " Volume pricing available " , " price_at_each " : " at /ea " , } " facets " : { " show_more " : " Show more " } } }
ClickSave .