🔧 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>
187 lines
5.1 KiB
PHP
187 lines
5.1 KiB
PHP
<?php
|
|
|
|
class MiraviaProduct
|
|
{
|
|
public $id = 0;
|
|
public $id_miravia = '';
|
|
public $miravia_status = '';
|
|
public $created = 0;
|
|
public $sku = '';
|
|
public $reference_sku = '';
|
|
public $lang = 'es-ES'; // ISO lang code
|
|
public $name = '';
|
|
public $short_description = '';
|
|
public $description = '';
|
|
public $brand = 'no brand';
|
|
public $model = '';
|
|
public $ean_code = '';
|
|
public $warranty = '';
|
|
public $id_category = '';
|
|
public $id_brand = '';
|
|
public $images = []; // Public URLs
|
|
public $video = '';
|
|
public $price = 0;
|
|
public $special_price = 0;
|
|
public $quantity = 0;
|
|
public $width = 0;
|
|
public $height = 0;
|
|
public $length = 0;
|
|
public $weight = 0;
|
|
public $warehouse = '';
|
|
public $delivery = '';
|
|
public $extra_attributes = []; // [ "id_attribute" => "value", ... ]
|
|
public $combinations = []; // Array of MiraviaCombination class
|
|
public $info = []; // Internal info (for filters)
|
|
|
|
protected $private_vars = ['private_vars', 'error', 'id', 'combinations', 'info'];
|
|
public $error = '';
|
|
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function addAttr($key = false, $value = false) {
|
|
if($key and $value) {
|
|
$this->extra_attributes[$key] = $value;
|
|
}
|
|
}
|
|
|
|
public function getData()
|
|
{
|
|
$product = [];
|
|
foreach ($this as $key => $value){
|
|
if(!in_array($key, $this->private_vars)){
|
|
if($key === 'images' && is_array($value) && !empty($value)){
|
|
$product['Images'] = ['Image' => $value];
|
|
} else if($key !== 'images') {
|
|
$product[$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
$combinations = $this->getCombinations();
|
|
$product['combinations'] = $combinations;
|
|
return $product;
|
|
}
|
|
|
|
public function getStockData()
|
|
{
|
|
$combis = [];
|
|
foreach ($this->combinations as $combination){
|
|
$combis[] = $combination->getStockData();
|
|
}
|
|
if(count($combis)==0){
|
|
$combis =[[
|
|
'sku' => $this->sku,
|
|
'price' => $this->price,
|
|
'special_price' => $this->special_price,
|
|
'quantity' => $this->quantity >= 0 ? $this->quantity : 0
|
|
]];
|
|
$this->reference_sku = $this->sku;
|
|
}
|
|
$ret = [
|
|
'id' => $this->id,
|
|
'id_miravia' => $this->id_miravia,
|
|
'sku' => $this->sku,
|
|
'combinations' => $combis
|
|
];
|
|
if(!empty($this->warehouse)){
|
|
$ret['warehouse'] = $this->warehouse;
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public function getOnlyStockData()
|
|
{
|
|
$combis = [];
|
|
foreach ($this->combinations as $combination){
|
|
$combis[] = $combination->getOnlyStockData();
|
|
}
|
|
if(count($combis)==0){
|
|
$combis =[[
|
|
'sku' => $this->sku,
|
|
'quantity' => $this->quantity >= 0 ? $this->quantity : 0
|
|
]];
|
|
$this->reference_sku = $this->sku;
|
|
}
|
|
$ret = [
|
|
'id' => $this->id,
|
|
'id_miravia' => $this->id_miravia,
|
|
'sku' => $this->sku,
|
|
'combinations' => $combis
|
|
];
|
|
if(!empty($this->warehouse)){
|
|
$ret['warehouse'] = $this->warehouse;
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public function getJSON($pretty = false)
|
|
{
|
|
$flags = $pretty ? JSON_PRETTY_PRINT : 0;
|
|
return json_encode($this->getData(), $flags);
|
|
}
|
|
|
|
public function getCombinations()
|
|
{
|
|
$ret = [];
|
|
foreach ($this->combinations as $combination){
|
|
$ret[] = $combination->getData();
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public function createProduct($apiKey)
|
|
{
|
|
if(!empty($this->id_miravia)){
|
|
return $this->updateProduct($apiKey);
|
|
}
|
|
$jreq = json_encode($this->getData());
|
|
$link = new MiraviaLink($apiKey);
|
|
$ret = $link->createProduct($jreq);
|
|
if($ret == false) {
|
|
$this->error = $link->last_error;
|
|
}
|
|
if(isset($ret['data'])){
|
|
if(isset($ret['data']['item_id'])){
|
|
$this->id_miravia = $ret['data']['item_id'];
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function updateProduct($apiKey)
|
|
{
|
|
if(empty($this->id_miravia)){
|
|
$this->error = 'Invalid Miravia Id Product';
|
|
return false;
|
|
}
|
|
$jreq = json_encode($this->getData());
|
|
$link = new MiraviaLink($apiKey);
|
|
$ret = $link->updateProduct($this->id_miravia, $jreq);
|
|
if($ret == false) {
|
|
$this->error = $link->last_error;
|
|
}
|
|
if(isset($ret['data'])){
|
|
if(isset($ret['data']['item_id'])){
|
|
$this->id_miravia = $ret['data']['item_id'];
|
|
}
|
|
return true;
|
|
}
|
|
if(isset($ret['message'])){
|
|
$this->error = $ret['message'];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function getInfoValue($field)
|
|
{
|
|
if(isset($this->info[$field])){
|
|
return $this->info[$field];
|
|
}
|
|
return '';
|
|
}
|
|
|
|
}
|
|
|