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>
This commit is contained in:
243
connector-miravia/classes/shared/MiraviaFeed.php
Normal file
243
connector-miravia/classes/shared/MiraviaFeed.php
Normal file
@@ -0,0 +1,243 @@
|
||||
<?php
|
||||
|
||||
class MiraviaFeed
|
||||
{
|
||||
private $products = [];
|
||||
public $miravia_feed_id = '';
|
||||
public $debug = false;
|
||||
|
||||
public $default_width = 0;
|
||||
public $default_height = 0;
|
||||
public $default_length = 0;
|
||||
public $default_weight = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function setProducts($products)
|
||||
{
|
||||
foreach($products as $p) {
|
||||
$this->products[$p->id] = $p;
|
||||
}
|
||||
}
|
||||
|
||||
public function addProduct(MiraviaProduct $product)
|
||||
{
|
||||
$this->products[$product->id] = $product;
|
||||
}
|
||||
|
||||
public function getProducts()
|
||||
{
|
||||
return $this->products;
|
||||
}
|
||||
|
||||
public function getProductsIds()
|
||||
{
|
||||
return array_keys($this->products);
|
||||
}
|
||||
|
||||
public function getJsonCreate($offset = 0, $limit = 200)
|
||||
{
|
||||
$ret = [];
|
||||
$prods = array_slice($this->products, $offset, $limit);
|
||||
foreach ($prods as $p) {
|
||||
if($p->created==0){
|
||||
$this->setDefaultValues($p);
|
||||
$ret[] = $p->getData();
|
||||
}
|
||||
}
|
||||
if(empty($ret)){
|
||||
return false;
|
||||
}
|
||||
$ret = [
|
||||
'products' => $ret
|
||||
];
|
||||
$flags = $this->debug ? JSON_PRETTY_PRINT : 0;
|
||||
return json_encode($ret, $flags);
|
||||
}
|
||||
|
||||
public function getJsonUpdate($offset = 0, $limit = 200)
|
||||
{
|
||||
$ret = [];
|
||||
$prods = array_slice($this->products, $offset, $limit);
|
||||
foreach ($prods as $p) {
|
||||
if($p->created==1 && strlen($p->id_miravia)>14){
|
||||
$this->setDefaultValues($p);
|
||||
$ret[] = $p->getData();
|
||||
}
|
||||
}
|
||||
if(empty($ret)){
|
||||
return false;
|
||||
}
|
||||
$ret = [
|
||||
'products' => $ret
|
||||
];
|
||||
$flags = $this->debug ? JSON_PRETTY_PRINT : 0;
|
||||
return json_encode($ret, $flags);
|
||||
}
|
||||
|
||||
public function setDefaultValues(MiraviaProduct &$product)
|
||||
{
|
||||
if((float)$product->weight == 0) $product->weight = $this->default_weight;
|
||||
if((int)$product->height == 0) $product->height = $this->default_height;
|
||||
if((int)$product->width == 0) $product->width = $this->default_width;
|
||||
if((int)$product->length == 0) $product->length = $this->default_length;
|
||||
}
|
||||
|
||||
public function getJsonUpdateStock($onlyStock = false, $offset = 0, $limit = 200)
|
||||
{
|
||||
$ret = [];
|
||||
$prods = array_slice($this->products, $offset, $limit);
|
||||
foreach ($prods as $p) {
|
||||
if(!empty($p->id_miravia)){
|
||||
if($onlyStock) {
|
||||
$ret[] = $p->getOnlyStockData();
|
||||
}else{
|
||||
$ret[] = $p->getStockData();
|
||||
}
|
||||
}
|
||||
}
|
||||
if(empty($ret)){
|
||||
return false;
|
||||
}
|
||||
$ret = [
|
||||
'products' => $ret
|
||||
];
|
||||
$flags = $this->debug ? JSON_PRETTY_PRINT : 0;
|
||||
return json_encode($ret, $flags);
|
||||
}
|
||||
|
||||
public function applyFilter($jsonFilter)
|
||||
{
|
||||
if(empty($jsonFilter)){
|
||||
return false;
|
||||
}
|
||||
$filter = new MiraviaFilter($jsonFilter);
|
||||
|
||||
if($ids = $filter->parseRecords($this->products)){
|
||||
return $ids;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function removeProductsById($ids)
|
||||
{
|
||||
if(!is_array($ids)){
|
||||
$ids = [$ids];
|
||||
}
|
||||
foreach ($ids as $id){
|
||||
unset($this->products[$id]);
|
||||
}
|
||||
}
|
||||
|
||||
public function keepProductsById($ids)
|
||||
{
|
||||
if(!is_array($ids)){
|
||||
$ids = [$ids];
|
||||
}
|
||||
$keys = array_keys($this->products);
|
||||
$ids = array_diff($keys, $ids);
|
||||
foreach ($ids as $id){
|
||||
unset($this->products[$id]);
|
||||
}
|
||||
}
|
||||
|
||||
public function applyNumericFieldAction($ids, $fields, $action, $value)
|
||||
{
|
||||
$afields = explode(',',$fields);
|
||||
foreach ($afields as $field){
|
||||
foreach ($ids as $id){
|
||||
if(isset($this->products[$id])){
|
||||
|
||||
switch ($action){
|
||||
case 'increment':
|
||||
if($this->products[$id]->{$field}) {
|
||||
$this->products[$id]->{$field} = (float)$this->products[$id]->{$field} + (float)$value;
|
||||
}
|
||||
foreach($this->products[$id]->combinations as &$combi){
|
||||
$combi->{$field} += (float)$value;
|
||||
}
|
||||
break;
|
||||
case 'decrement':
|
||||
if($this->products[$id]->{$field}) {
|
||||
$this->products[$id]->{$field} -= (float)$value;
|
||||
}
|
||||
foreach($this->products[$id]->combinations as &$combi){
|
||||
$combi->{$field} -= (float)$value;
|
||||
}
|
||||
break;
|
||||
case 'increment_percent':
|
||||
if($this->products[$id]->{$field}) {
|
||||
$this->products[$id]->{$field} += ((float)$this->products[$id]->{$field} * (float)$value / 100);
|
||||
}
|
||||
foreach($this->products[$id]->combinations as &$combi){
|
||||
$combi->{$field} += ((float)$combi->{$field} * (float)$value / 100);
|
||||
}
|
||||
break;
|
||||
case 'decrement_percent':
|
||||
if($this->products[$id]->{$field}) {
|
||||
$this->products[$id]->{$field} -= ((float)$this->products[$id]->{$field} * (float)$value / 100);
|
||||
}
|
||||
foreach($this->products[$id]->combinations as &$combi){
|
||||
$combi->{$field} -= ((float)$combi->{$field} * (float)$value / 100);
|
||||
}
|
||||
break;
|
||||
case 'multiply':
|
||||
if($this->products[$id]->{$field}) {
|
||||
$this->products[$id]->{$field} = ((float)$this->products[$id]->{$field} * (float)$value );
|
||||
}
|
||||
foreach($this->products[$id]->combinations as &$combi){
|
||||
$combi->{$field} = ((float)$combi->{$field} * (float)$value );
|
||||
}
|
||||
break;
|
||||
case 'set':
|
||||
if($this->products[$id]->{$field}) {
|
||||
$this->products[$id]->{$field} = (float)$value;
|
||||
}
|
||||
foreach($this->products[$id]->combinations as &$combi){
|
||||
$combi->{$field} = (float)$value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function applyTextFieldAction($ids, $field, $template)
|
||||
{
|
||||
foreach ($ids as $id) {
|
||||
if (isset($this->products[$id])) {
|
||||
$replaces = [
|
||||
'name' => $this->products[$id]->name,
|
||||
'description' => $this->products[$id]->description,
|
||||
'short_description' => $this->products[$id]->short_description,
|
||||
'manufacturer' => $this->products[$id]->getInfoValue('manufacturer'),
|
||||
'supplier' => $this->products[$id]->getInfoValue('supplier'),
|
||||
'category' => $this->products[$id]->getInfoValue('category'),
|
||||
];
|
||||
$this->products[$id]->{$field} = $template;
|
||||
foreach($replaces as $search => $replace){
|
||||
$this->products[$id]->{$field} = str_replace('%' . $search .'%', $replace, $this->products[$id]->{$field});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function applyLogisticsAction($ids, $delivery, $warehouse)
|
||||
{
|
||||
foreach ($ids as $id) {
|
||||
if (isset($this->products[$id])) {
|
||||
$this->products[$id]->delivery = $delivery;
|
||||
$this->products[$id]->warehouse = $warehouse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getProductCount()
|
||||
{
|
||||
return sizeof($this->products);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user