fix: use Odoo public image URLs instead of broken WP media upload
WC consumer key cannot auth against /wp/v2/media (401). Instead, pass
Odoo's public image URL in the product/variation data and let WC
download it directly from {odoo_base}/web/image/product.product/{id}/image_1920.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -246,30 +246,17 @@ class WooProductMap(models.Model):
|
||||
if wc_tax_class:
|
||||
var_data['tax_class'] = wc_tax_class
|
||||
|
||||
# Variant image
|
||||
# Variant image — pass Odoo's public URL, WC downloads it directly
|
||||
if variant.image_variant_1920:
|
||||
# Upload image
|
||||
try:
|
||||
import base64
|
||||
import requests as req
|
||||
img_bytes = base64.b64decode(variant.image_variant_1920)
|
||||
wp_url = inst.url.rstrip('/')
|
||||
filename = (variant.default_code or 'variant') + '.jpg'
|
||||
resp = req.post(
|
||||
f"{wp_url}/wp-json/wp/v2/media",
|
||||
auth=(inst.consumer_key, inst.consumer_secret),
|
||||
headers={
|
||||
'Content-Disposition': f'attachment; filename="{filename}"',
|
||||
'Content-Type': 'image/jpeg',
|
||||
},
|
||||
data=img_bytes,
|
||||
timeout=60,
|
||||
)
|
||||
if resp.status_code in (200, 201):
|
||||
media = resp.json()
|
||||
var_data['image'] = {'id': media['id']}
|
||||
except Exception as img_err:
|
||||
_logger.warning("Variant image upload failed: %s", img_err)
|
||||
odoo_base = inst.env['ir.config_parameter'].sudo().get_param('web.base.url', '')
|
||||
filename = (variant.default_code or 'variant') + '.jpg'
|
||||
if odoo_base:
|
||||
img_url = f"{odoo_base}/web/image/product.product/{variant.id}/image_1920"
|
||||
var_data['image'] = {
|
||||
'src': img_url,
|
||||
'name': filename,
|
||||
'alt': variant.display_name,
|
||||
}
|
||||
|
||||
try:
|
||||
wc_variation = client.create_product_variation(self.woo_product_id, var_data)
|
||||
|
||||
Reference in New Issue
Block a user