feat: add Refresh Prices button to pull WC prices for existing mappings

Existing mapped products had no WC price since they were fetched before
the woo_price field existed. action_refresh_prices fetches current prices
from WC API for all mapped products.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsinghpal
2026-03-31 22:35:13 -04:00
parent 919dbcb4cf
commit 3ea283247a
3 changed files with 55 additions and 0 deletions

View File

@@ -280,6 +280,34 @@ class WooInstance(models.Model):
'Fetched %d products, auto-matched %d by SKU' % (total_fetched, auto_matched))
return True
def action_refresh_prices(self):
"""Refresh WC prices for all mapped products from WooCommerce API."""
self.ensure_one()
if self.state != 'connected':
raise UserError("Instance is not connected.")
client = self._get_client()
maps = self.env['woo.product.map'].search([
('instance_id', '=', self.id),
('woo_product_id', '>', 0),
])
updated = 0
for m in maps:
try:
wc_prod = client.get_product(m.woo_product_id)
wc_price = 0.0
try:
wc_price = float(wc_prod.get('regular_price') or wc_prod.get('price') or 0)
except (ValueError, TypeError):
pass
if wc_price != m.woo_price:
m.woo_price = wc_price
updated += 1
except Exception as e:
_logger.warning("Failed to refresh price for WC#%s: %s", m.woo_product_id, e)
self._log_sync('product', 'woo_to_odoo', self.name, 'success',
'Refreshed prices for %d products' % updated)
return True
def action_sync(self):
"""Manual sync trigger from the UI."""
self.ensure_one()