Files
MiraviaConnector/connector-miravia/classes/class.product copy.php
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

125 lines
4.8 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
if( !class_exists('ARIProduct') ) {
class ARIProduct {
private $product = false;
private $defaul_image = '';
private $category = 0;
public $ItemId = 0;
public $Images = array('Image' => array());
public $Attributes = array();
public $Skus = array('Sku' => array());
public $PrimaryCategory = 0;
function __construct($id){
$this->product = wc_get_product($id);
//Por defecto de la categoria
$terms = get_the_terms( $this->product->get_id(), 'product_cat' );
$this->category = $terms[0];
//Exist Product
$miraviaId = get_post_meta($id, '_miravia_product_id', true);
if($miraviaId) {
$this->ItemId = $miraviaId;
}
$atributos_guardados = get_term_meta($this->category->term_id, "_miravia_attr", true);
if($atributos_guardados) {
$this->Attributes = $atributos_guardados;
}
//Completar los datos básicos
$this->PrimaryCategory = get_term_meta($this->category->term_id, "_miravia_category", true);
$this->Attributes['name'] = $this->product->get_name();
$this->Attributes['description'] = $this->product->get_description();
//Cargar imagenes
$attachment_ids = $this->product->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id ) {
$this->Images['Image'][] = $this->defaul_image;
}
$this->get_skus();
}
function get_skus() {
if($this->product->is_type('simple')) {
$skus = [$this->product];
}else{
$skus = $this->product->get_available_variations();
}
foreach($skus as $index => $sku) {
$this->Skus['Sku'][$index]["SellerSku"] = $sku->get_sku();
$this->Skus['Sku'][$index]["quantity"] = $sku->get_stock_quantity();
$this->Skus['Sku'][$index]["price"] = $sku->get_regular_price();
if($sku->is_on_sale()) {
$this->Skus['Sku'][$index]["special_price"] = $sku->get_sale_price();
}
$this->Skus['Sku'][$index]["price"] = $sku->get_regular_price();
if($sku->get_manage_stock() === false) {
if($sku->get_stock_status() == "instock") {
$stock_available = intval(100); //Stock por defecto
}else{
$stock_available = 0;
}
}else{
$stock_available = $sku->get_stock_quantity();
}
$this->Skus['Sku'][$index]["quantity"] = $stock_available;
$this->Skus['Sku'][$index]["package_height"] = $sku->get_height();
$this->Skus['Sku'][$index]["package_length"] = $sku->get_length();
$this->Skus['Sku'][$index]["ean_code"] = '0'; //Implementar
$this->Skus['Sku'][$index]["package_width"] = $sku->get_width();
$this->Skus['Sku'][$index]["package_weight"] = $sku->get_weight();
$this->Skus['Sku'][$index]["package_content"] = ''; //Implementar
$attachment_ids = $sku->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id ) {
$this->Skus['Sku'][$index]["Images"]["Image"][] = $this->defaul_image;
}
}
}
function send() {
global $MIRAVIAWOO;
$send_product = json_encode(array("Request" => array("Product" => array(
'Attributes' => $this->Attributes,
'PrimaryCategory' => $this->PrimaryCategory,
'Skus' => $this->Skus,
'Images' => $this->Images
))));
$result_product = $MIRAVIAWOO->client->post('/product/create', array(
'body' => $send_product
));
return $result_product;
}
function update() {
global $MIRAVIAWOO;
$send_product = json_encode(array("Request" => array("Product" => array(
'ItemId' => $this->ItemId,
'Attributes' => $this->Attributes,
'PrimaryCategory' => $this->PrimaryCategory,
'Skus' => $this->Skus,
'Images' => $this->Images
))));
$result_product = $MIRAVIAWOO->client->post('/product/update', array(
'payload' => $send_product
));
error_log(json_encode($result_product));
return $result_product;
}
}
}