fix: serve variant images via custom endpoint instead of product URL
Product image URL was serving placeholder because writing to image_1920
doesn't work for variants (computed field). New approach:
1. Store image in wizard line table (attachment=False, bytea column)
2. Serve directly via /woo/image/{line_id}/{filename} endpoint
3. WC downloads real image data from this URL
4. Also saves to image_variant_1920 for Odoo reference
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -248,33 +248,22 @@ class WooVariantPushWizard(models.TransientModel):
|
||||
if wc_tax_class:
|
||||
var_data['tax_class'] = wc_tax_class
|
||||
|
||||
# Save wizard image to Odoo product, then pass URL to WC
|
||||
# Serve image directly from wizard line via custom endpoint
|
||||
if line.image:
|
||||
img_data = line.image
|
||||
img_size = len(img_data) if img_data else 0
|
||||
_logger.info("Variant %d image data: type=%s size=%d", variant.id, type(img_data).__name__, img_size)
|
||||
# Save the image from the wizard to the actual Odoo product
|
||||
if img_size > 100: # Skip tiny placeholders
|
||||
variant.sudo().write({'image_1920': img_data})
|
||||
self.env.cr.commit() # Force commit so the image is available for download
|
||||
_logger.info("Saved image to Odoo product %d (%d bytes)", variant.id, img_size)
|
||||
else:
|
||||
_logger.warning("Skipping tiny image for variant %d (%d bytes)", variant.id, img_size)
|
||||
|
||||
# Now build the public URL for WC to download
|
||||
# Commit the line data so the image endpoint can read it
|
||||
self.env.cr.commit()
|
||||
odoo_base = inst.env['ir.config_parameter'].sudo().get_param('web.base.url', '')
|
||||
if odoo_base:
|
||||
img_name = (line.sku or variant.default_code or 'variant') + '.png'
|
||||
# Add timestamp to bust WC cache
|
||||
import time
|
||||
cache_bust = int(time.time())
|
||||
img_url = f"{odoo_base}/web/image/product.product/{variant.id}/image_1920/{img_name}?t={cache_bust}"
|
||||
img_url = f"{odoo_base}/woo/image/{line.id}/{img_name}"
|
||||
var_data['image'] = {
|
||||
'src': img_url,
|
||||
'name': img_name,
|
||||
'alt': line.variant_name or '',
|
||||
}
|
||||
_logger.info("Variant image URL: %s", img_url)
|
||||
# Also save to Odoo product for future reference
|
||||
variant.sudo().write({'image_variant_1920': line.image})
|
||||
|
||||
try:
|
||||
client.update_product_variation(pm.woo_product_id, wc_var_id, var_data)
|
||||
|
||||
Reference in New Issue
Block a user