details:   https://code.tryton.org/tryton/commit/88f32d9e0426
branch:    default
user:      Cédric Krier <[email protected]>
date:      Tue Aug 12 15:32:34 2025 +0200
description:
        Set compare-at price on Shopify using non-sale price

        Closes #14155
diffstat:

 modules/web_shop_shopify/CHANGELOG  |   1 +
 modules/web_shop_shopify/product.py |  24 +++++++++++++----
 modules/web_shop_shopify/web.py     |  51 +++++++++++++++++++++++++++---------
 3 files changed, 57 insertions(+), 19 deletions(-)

diffs (195 lines):

diff -r 072e73a4830e -r 88f32d9e0426 modules/web_shop_shopify/CHANGELOG
--- a/modules/web_shop_shopify/CHANGELOG        Thu Oct 09 19:12:53 2025 +0200
+++ b/modules/web_shop_shopify/CHANGELOG        Tue Aug 12 15:32:34 2025 +0200
@@ -1,3 +1,4 @@
+* Set compare-at price using non-sale price
 * Add support for product kit
 * Add carrier selection
 * Use GraphQL API
diff -r 072e73a4830e -r 88f32d9e0426 modules/web_shop_shopify/product.py
--- a/modules/web_shop_shopify/product.py       Thu Oct 09 19:12:53 2025 +0200
+++ b/modules/web_shop_shopify/product.py       Tue Aug 12 15:32:34 2025 +0200
@@ -274,7 +274,9 @@
     def search_shopify_sku(cls, name, clause):
         return [('code',) + tuple(clause[1:])]
 
-    def get_shopify(self, shop, price, tax, shop_taxes_included=True):
+    def get_shopify(
+            self, shop, sale_price, sale_tax, price, tax,
+            shop_taxes_included=True):
         shopify_id = self.get_shopify_identifier(shop)
         if shopify_id:
             shopify_id = id2gid('ProductVariant', shopify_id)
@@ -286,13 +288,20 @@
                     }, {'id': shopify_id})['data']['productVariant'] or {}
         else:
             variant = {}
+        sale_price = self.shopify_price(
+            sale_price, sale_tax, taxes_included=shop_taxes_included)
+        if sale_price is not None:
+            variant['price'] = str(sale_price.quantize(Decimal('.00')))
+        else:
+            variant['price'] = None
         price = self.shopify_price(
             price, tax, taxes_included=shop_taxes_included)
         if price is not None:
-            variant['price'] = str(price.quantize(Decimal('.00')))
+            variant['compareAtPrice'] = str(
+                price.quantize(Decimal('.00')))
         else:
-            variant['price'] = None
-        variant['taxable'] = bool(tax)
+            variant['compareAtPrice'] = None
+        variant['taxable'] = bool(sale_tax)
 
         for identifier in self.identifiers:
             if identifier.type == 'ean':
@@ -637,9 +646,12 @@
             if image.web_shop:
                 yield image
 
-    def get_shopify(self, shop, price, tax, shop_taxes_included=True):
+    def get_shopify(
+            self, shop, sale_price, sale_tax, price, tax,
+            shop_taxes_included=True):
         variant = super().get_shopify(
-            shop, price, tax, shop_taxes_included=shop_taxes_included)
+            shop, sale_price, sale_tax, price, tax,
+            shop_taxes_included=shop_taxes_included)
         for image in self.shopify_images:
             file = {
                 'alt': image.description,
diff -r 072e73a4830e -r 88f32d9e0426 modules/web_shop_shopify/web.py
--- a/modules/web_shop_shopify/web.py   Thu Oct 09 19:12:53 2025 +0200
+++ b/modules/web_shop_shopify/web.py   Tue Aug 12 15:32:34 2025 +0200
@@ -322,6 +322,14 @@
                 categories = shop.get_categories()
                 products, prices, taxes = shop.get_products(
                     key=lambda p: p.template.id)
+                sale_prices, sale_taxes = prices, taxes
+
+                context = shop.get_context()
+                with Transaction().set_context(_non_sale_price=True):
+                    sale_context = shop.get_context()
+                    if context != sale_context:
+                        _, prices, taxes = shop.get_products()
+
                 if shopify_shop['currencyCode'] != shop.currency.code:
                     raise ShopifyError(gettext(
                             'web_shop_shopify.msg_shop_currency_different',
@@ -351,13 +359,17 @@
                         t_products, key=template.products.index)
                     p_inventory_items = [
                         inventory_items[p] for p in t_products]
+                    p_sale_prices = [sale_prices[p.id] for p in t_products]
+                    p_sale_taxes = [sale_taxes[p.id] for p in t_products]
                     p_prices = [prices[p.id] for p in t_products]
                     p_taxes = [taxes[p.id] for p in t_products]
                     if shop._shopify_product_is_to_update(
-                            template, t_products, p_prices, p_taxes):
+                            template, t_products, p_sale_prices, p_sale_taxes,
+                            p_prices, p_taxes):
                         shop._shopify_update_product(
                             shopify_shop, categories, template, t_products,
-                            p_inventory_items, p_prices, p_taxes)
+                            p_inventory_items, p_sale_prices, p_sale_taxes,
+                            p_prices, p_taxes)
                         Transaction().commit()
 
                 for category in shop.categories_removed:
@@ -428,18 +440,21 @@
             category.set_shopify_identifier(self)
 
     def _shopify_product_is_to_update(
-            self, template, products, prices, taxes):
+            self, template, products, sale_prices, sale_taxes, prices, taxes):
         return (
             template.is_shopify_to_update(self)
             or any(
-                prod.is_shopify_to_update(self, price=p, tax=t)
-                for prod, p, t in zip(products, prices, taxes))
+                prod.is_shopify_to_update(
+                    self, sale_price=s_p, sale_tax=s_t, price=p, tax=t)
+                for prod, s_p, s_t, p, t in zip(
+                    products, sale_prices, sale_taxes, prices, taxes))
             or any(
                 prod in self.products_removed for prod in products))
 
     def _shopify_update_product(
             self, shopify_shop, categories, template, products,
-            inventory_items, prices, taxes, product_fields=None):
+            inventory_items, sale_prices, sale_taxes, prices, taxes,
+            product_fields=None):
         pool = Pool()
         Identifier = pool.get('web.shop.shopify_identifier')
 
@@ -466,12 +481,16 @@
         shopify_product = template.get_shopify(self, categories)
         variants = []
         for position, (
-            product, inventory_item, price, tax) in enumerate(zip(
-                    products, inventory_items, prices, taxes),
+            product, inventory_item,
+            sale_price, sale_tax,
+            price, tax) in enumerate(zip(
+                    products, inventory_items,
+                    sale_prices, sale_taxes,
+                    prices, taxes),
                 start=1):
             self._shopify_check_product(product)
             variant = product.get_shopify(
-                self, price, tax,
+                self, sale_price, sale_tax, price, tax,
                 shop_taxes_included=shopify_shop['taxesIncluded'])
             variant['inventoryItem'] = inventory_item.get_shopify(
                 self, shop_weight_unit=shopify_shop['weightUnit'])
@@ -522,14 +541,18 @@
                 'variants', shopify_product)
 
             for (product, inventory_item,
+                sale_price, sale_tax,
                 price, tax,
                 shopify_variant) in zip(
                     products, inventory_items,
+                    sale_prices, sale_taxes,
                     prices, taxes,
                     shopify_variants):
                 identifier = product.set_shopify_identifier(
                     self, gid2id(shopify_variant['id']))
                 update_extra = {
+                    'sale_price': sale_price,
+                    'sale_tax': sale_tax,
                     'price': price,
                     'tax': tax,
                     }
@@ -838,16 +861,17 @@
     __name__ = 'web.shop'
 
     def _shopify_product_is_to_update(
-            self, template, products, prices, taxes):
+            self, template, products, sale_prices, sale_taxes, prices, taxes):
         return (
             super()._shopify_product_is_to_update(
-                template, products, prices, taxes)
+                template, products, sale_prices, sale_taxes, prices, taxes)
             or any(
                 i.is_shopify_to_update(self) for i in template.shopify_images))
 
     def _shopify_update_product(
             self, shopify_shop, categories, template, products,
-            inventory_items, prices, taxes, product_fields=None):
+            inventory_items, sale_prices, sale_taxes, prices, taxes,
+            product_fields=None):
         pool = Pool()
         Identifier = pool.get('web.shop.shopify_identifier')
 
@@ -867,7 +891,8 @@
                 })
         shopify_product = super()._shopify_update_product(
             shopify_shop, categories, template, products, inventory_items,
-            prices, taxes, product_fields=product_fields)
+            sale_prices, sale_taxes, prices, taxes,
+            product_fields=product_fields)
 
         try:
             shopify_media = graphql.iterate(

Reply via email to