feat: WC standard price + sale price with smart sync logic

- Split woo_price into woo_regular_price and woo_sale_price
- Sync to WC: if standard=0 sets regular_price, otherwise sets sale_price
- Validation: standard price cannot be less than sale price
- Both prices shown in mapping UI with sale price highlighted in green
- Refresh Prices pulls both values from WC API

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsinghpal
2026-03-31 23:19:01 -04:00
parent b1e4ed5ec8
commit 80f9ddd0d6
5 changed files with 127 additions and 26 deletions

View File

@@ -196,9 +196,14 @@ class WooInstance(models.Model):
match_state = 'mapped'
auto_matched += 1
wc_price = 0.0
wc_regular_price = 0.0
wc_sale_price = 0.0
try:
wc_price = float(wc_prod.get('regular_price') or wc_prod.get('price') or 0)
wc_regular_price = float(wc_prod.get('regular_price') or 0)
except (ValueError, TypeError):
pass
try:
wc_sale_price = float(wc_prod.get('sale_price') or 0)
except (ValueError, TypeError):
pass
@@ -210,7 +215,8 @@ class WooInstance(models.Model):
'woo_product_id': wc_id,
'woo_product_name': wc_name,
'woo_sku': wc_sku,
'woo_price': wc_price,
'woo_regular_price': wc_regular_price,
'woo_sale_price': wc_sale_price,
'woo_permalink': wc_permalink,
'woo_product_type': wc_type if wc_type in ('simple', 'variable', 'grouped', 'external') else 'simple',
'state': match_state,
@@ -249,9 +255,14 @@ class WooInstance(models.Model):
var_state = 'mapped'
auto_matched += 1
var_price = 0.0
var_regular = 0.0
var_sale = 0.0
try:
var_price = float(var.get('regular_price') or var.get('price') or 0)
var_regular = float(var.get('regular_price') or 0)
except (ValueError, TypeError):
pass
try:
var_sale = float(var.get('sale_price') or 0)
except (ValueError, TypeError):
pass
@@ -261,7 +272,8 @@ class WooInstance(models.Model):
'woo_product_id': var_id,
'woo_product_name': var_name,
'woo_sku': var_sku,
'woo_price': var_price,
'woo_regular_price': var_regular,
'woo_sale_price': var_sale,
'woo_product_type': 'simple',
'woo_parent_id': wc_id,
'is_variation': True,
@@ -284,7 +296,7 @@ class WooInstance(models.Model):
return True
def action_refresh_prices(self):
"""Refresh WC prices for all mapped products from WooCommerce API."""
"""Refresh WC standard + sale prices for all products from WooCommerce API."""
self.ensure_one()
if self.state != 'connected':
raise UserError("Instance is not connected.")
@@ -297,13 +309,24 @@ class WooInstance(models.Model):
for m in maps:
try:
wc_prod = client.get_product(m.woo_product_id)
wc_price = 0.0
regular = 0.0
sale = 0.0
try:
wc_price = float(wc_prod.get('regular_price') or wc_prod.get('price') or 0)
regular = float(wc_prod.get('regular_price') or 0)
except (ValueError, TypeError):
pass
if wc_price != m.woo_price:
m.woo_price = wc_price
try:
sale = float(wc_prod.get('sale_price') or 0)
except (ValueError, TypeError):
pass
changed = False
if abs(regular - (m.woo_regular_price or 0)) > 0.001:
m.woo_regular_price = regular
changed = True
if abs(sale - (m.woo_sale_price or 0)) > 0.001:
m.woo_sale_price = sale
changed = True
if changed:
updated += 1
except Exception as e:
_logger.warning("Failed to refresh price for WC#%s: %s", m.woo_product_id, e)