Files
Miravia Connector Bot a7d7dbb164 Fix image upload structure for Miravia API compliance
🔧 Bug Fixes:
- Fixed product image structure to match Miravia API requirements
- Updated MiraviaProduct.php getData() method to wrap images in {"Image": [...]} format
- Updated MiraviaCombination.php getData() method to wrap SKU images properly
- Resolved error "[4224] The Main image of the product is required"

📋 Changes:
- Modified getData() methods to transform flat image arrays to nested structure
- Product images: images[] → Images: {"Image": [...]}
- SKU images: images[] → Images: {"Image": [...]}
- Maintains backward compatibility for empty image arrays

🎯 Impact:
- Product uploads will now pass Miravia's image validation
- Both product-level and SKU-level images properly formatted
- Complies with official Miravia API documentation structure

🤖 Generated with Claude Code (https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-17 08:11:23 +02:00

211 lines
9.3 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('MVProduct') ) {
class MVProduct {
public $product = false;
public $id = 0;
public $id_woocommerce = 0;
public $id_miravia = 0;
public $profile = 0;
public $defaul_image = '';
public $last_updated = '0000-00-00 00:00:00';
function __construct($id, $profile = 0) {
global $wpdb;
$saved = MiraviaCore::get_product($id, $profile['id']);
$ean = '';
$keyEAN = get_option('miravia_ean_key', '');
if($keyEAN != '') {
$ean = get_post_meta($id, $keyEAN, true);
}
$defaultBrand = get_option('miravia_default_brand', 'No Brand');
$productBrand = get_post_meta($id, '_miravia_brand_product', true);
if($productBrand) {
$defaultBrand = $productBrand;
}
//Units
$defaultUnit = get_option('miravia_default_unit', 'units');
$defaultUnitValue = get_option('miravia_default_unit_value', '1');
$productUnit = get_post_meta($id, '_miravia_unit', true);
$productUnitValue = get_post_meta($id, '_miravia_unit_value', true);
if($productUnit) {
$defaultUnit = $productUnit;
}
if($productUnitValue) {
$defaultUnitValue = $productUnitValue;
}
// LOG::add($saved);
if($saved) {
$this->id = $saved->id;
$this->id_woocommerce = $saved->id_woocommerce;
$this->id_miravia = $saved->id_miravia;
$this->profile = $saved->profile_id;
$this->last_updated = $saved->last_updated;
$_product = wc_get_product(intval($this->id_woocommerce));
}else{
$this->id_woocommerce = $id;
//Load product
$_product = wc_get_product(intval($this->id_woocommerce));
$this->profile = $profile['id'];
$wpdb->insert($wpdb->prefix.'miravia_products', array(
'id_woocommerce' => $this->id_woocommerce,
'sku' => $_product->get_sku() ?: $id,
'id_miravia' => $this->id_miravia,
'profile_id' => $this->profile,
'stock' => $_product->get_regular_price(),
'price' => $_product->get_regular_price(),
'sale_price' => $_product->get_sale_price(),
));
$this->id = $wpdb->insert_id;
}
if($_product->get_manage_stock() === false) {
if($_product->get_stock_status() == "instock") {
$stock_available = get_option('_miravia_default_stock', 100); //Stock por defecto
}else{
$stock_available = 0;
}
}else{
$stock_available = $_product->get_stock_quantity();
}
$local_category = MiraviaCore::get_miravia_category($_product, $profile);
// $attributes_product = MiraviaCore::get_attributes($_product, $local_category);
// $attributes_product = $_product->get_attributes();
// die("<pre>".print_r($attributes_product, true)."</pre>");
$this->product = new MiraviaProduct();
$this->product->id = $this->id_woocommerce;
$this->product->id_miravia = $this->id_miravia;
$this->product->sku = $_product->get_sku() ?: $id;
$this->product->lang = 'es-ES'; // ISO lang cod
$this->product->name = $_product->get_name();
$this->product->short_description = $_product->get_short_description();
$this->product->description = $_product->get_description();
$this->product->brand = $defaultBrand;
$this->product->model = '';
$this->product->ean_code = $ean;
$this->product->warranty = '';
$this->product->id_category = $profile['miravia_category'];
$this->product->id_brand = '';
$this->product->images = []; // Se completa después
$this->product->video = '';
$this->product->price = $_product->get_regular_price();
$this->product->special_price = $_product->get_sale_price();
$this->product->width = $_product->get_width();
$this->product->height = $_product->get_height();
$this->product->length = $_product->get_length();
$this->product->weight = $_product->get_weight();
$this->product->delivery = get_option('miravia_transport_mode', 'dbm');
$this->product->info = array(
'id_category' => $local_category
);
//$this->product->extra_attributes = []; # TODO agregar caracteristicas
//Attrs
$this->product->addAttr("unit_count_type", $defaultUnit);
$this->product->addAttr("Unit_Count", $defaultUnitValue);
if($_product->is_type( 'variable' )) {
$combinations = [];
$variations = $_product->get_available_variations();
// MiraviaCore::debug($variations);
foreach($variations as $v) {
if($v['is_in_stock'] == true) {
if($v['max_qty'] == "") {
$stock_available = get_option('_miravia_default_stock', 100); //Stock por defecto
}else{
$stock_available = $v["max_qty"];
}
}else{
$stock_available = 0; //Stock por defecto
}
$ean = '';
$keyEAN = get_option('miravia_ean_key', '');
if($keyEAN != '') {
$ean = get_post_meta($v['variation_id'], $keyEAN, true);
}
$com = new MiraviaCombination();
$com->sku = $v['sku'];
$com->ean_code = $ean;
$com->price = $v['display_regular_price'];
$com->special_price = $v['display_price'];
$com->quantity = intval($stock_available);
$com->images = [];
$com->width = $v['dimensions']['width'];
$com->height = $v['dimensions']['height'];
$com->length = $v['dimensions']['length'];
$com->weight = $v['weight'];
$com->variation = [];
$encontrados = [];
$labels = [];
$lastAttribute = "";
foreach($v['attributes'] as $k => $a) {
$term_name = ( $term = get_term_by( 'slug', $a, str_replace("attribute_", "", $k) ) ) ? $term->name : $a;
$nameWithOutAttribute = str_replace("attribute_", "", $k);
if(isset($encontrados[$nameWithOutAttribute])) {
$encontrados[$nameWithOutAttribute] .= $term_name;
}else{
$encontrados[$nameWithOutAttribute] = $term_name;
}
}
foreach($encontrados as $b => $c) {
$com->addVariation($b, $c);
}
$combinations[] = $com;
}
$this->product->combinations = $combinations;
}
//Set stock
$this->product->quantity = $stock_available;
//Load images
$attachment_ids = $_product->get_gallery_image_ids();
$sizeImage = get_option('miravia_size_image', 'single-post-thumbnail');
$featured = wp_get_attachment_image_src( get_post_thumbnail_id( $this->product->id ), $sizeImage );
if($featured and is_array($featured)) {
$this->product->images[] = $featured[0];
}
foreach( $attachment_ids as $attachment_id ) {
$this->product->images[] = wp_get_attachment_image_src( $attachment_id, $sizeImage )[0];
}
}
public function getData() {
$error = false;
//Control de productos sin variaciones ni precio
if(($this->product->price == 0 or $this->product->price == "") and count($this->product->combinations) == 0) {
LOG::add("Producto {$this->product->id} no agregado por no tener precio y no tener variaciones", false, 'errors_products_send');
$error = true;
}
if(count($this->product->images) == 0) {
LOG::add("Producto {$this->product->id} no tiene imagenes, no enviado", false, 'errors_products_send');
$error = true;
}
if($error) {
return false;
}
return $this->product;
}
function send() {
return false;
}
}
}