Files
Miravia Connector Bot 552bce9f84 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-21 13:57:16 +02:00

269 lines
14 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
global $wpdb;
if(isset($_POST['miravia_action_nonce'])) {
if(wp_verify_nonce(sanitize_text_field($_POST['miravia_action_nonce']), 'save_miravia_conf')) {
update_option('miravia_delay_time', sanitize_text_field($_POST['secondsNotify']));
update_option('miravia_default_status', sanitize_text_field($_POST['miravia_order_defuault_status']));
update_option('_miravia_default_stock', sanitize_text_field($_POST['_miravia_default_stock']));
update_option('miravia_transport_mode', sanitize_text_field($_POST['miravia_transport_mode']));
update_option('miravia_ean_key', sanitize_text_field($_POST['miravia_ean_key']));
update_option('miravia_default_brand', sanitize_text_field($_POST['miravia_default_brand']));
update_option('miravia_default_unit', sanitize_text_field($_POST['miravia_default_unit']));
update_option('miravia_default_unit_value', sanitize_text_field($_POST['miravia_default_unit_value']));
if(isset($_POST['miraviaDebugMode']) and sanitize_text_field($_POST['miraviaDebugMode']) == '1') {
update_option('miravia_debug_mode', '1');
}else{
update_option('miravia_debug_mode', '0');
}
if(isset($_POST['miravia_only_stock']) and sanitize_text_field($_POST['miravia_only_stock']) == '1') {
update_option('miravia_only_stock', '1');
}else{
update_option('miravia_only_stock', '0');
}
if(isset($_POST['miravia_direct_api']) and sanitize_text_field($_POST['miravia_direct_api']) == '1') {
update_option('miravia_direct_api', '1');
}else{
update_option('miravia_direct_api', '0');
}
if(isset($_POST['miravia_personal_token'])) {
update_option('miravia_personal_token', sanitize_text_field($_POST['miravia_personal_token']));
}
if(isset($_POST['miravia_app_key'])) {
update_option('miravia_app_key', sanitize_text_field($_POST['miravia_app_key']));
}
if(isset($_POST['miravia_secret_key'])) {
update_option('miravia_secret_key', sanitize_text_field($_POST['miravia_secret_key']));
}
if(isset($_POST['miravia_access_token'])) {
update_option('miravia_access_token', sanitize_text_field($_POST['miravia_access_token']));
}
}
}
$defaultUnit = get_option('miravia_default_unit', 'units');
$defaultUnitValue = get_option('miravia_default_unit_value', '1');
$timeDelay = get_option('miravia_delay_time', 300);
$debugMode = get_option('miravia_debug_mode', '0');
$directApi = get_option('miravia_direct_api', '0');
$personalToken = get_option('miravia_personal_token', '');
$appKey = get_option('miravia_app_key', '');
$secretKey = get_option('miravia_secret_key', '');
$accessToken = get_option('miravia_access_token', '');
$transportMode = get_option('miravia_transport_mode', 'dbm');
$defaultBrand = get_option('miravia_default_brand', 'No Brand');
$statuses = wc_get_order_statuses();
$statusDefaultSelected = get_option('miravia_default_status', 'wc-processing');
$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
// MiraviaCore::debug($categories);
// $notifies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_name LIKE '%miravia_notify%'", ARRAY_A );
// // delete_option('miravia_notify_update_stock_in');
// LOG::add("Get notifies");
// LOG::add($notifies);
?>
<div class="wrap">
<h2>Configuration</h2>
<form method="post">
<?php wp_nonce_field( 'save_miravia_conf', 'miravia_action_nonce' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php echo __('Time Delay', 'miraviawoo')?>
</th>
<td>
<select name="secondsNotify">
<option value="30" <?php echo selected($timeDelay, 30,false)?>>30 secs</option>
<option value="60" <?php echo selected($timeDelay, 60,false)?>>1 min</option>
<option value="120" <?php echo selected($timeDelay, 120,false)?>>2 min</option>
<option value="240" <?php echo selected($timeDelay, 240,false)?>>4 min</option>
<option value="300" <?php echo selected($timeDelay, 300,false)?>>5 min</option>
<option value="300" <?php echo selected($timeDelay, 600,false)?>>10 min</option>
<option value="300" <?php echo selected($timeDelay, 3600,false)?>>1 h</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Debug Mode', 'miraviawoo')?>
<p class="description"><?php echo __('This active debug mode, developer support','miraviawoo')?></p>
</th>
<td>
<input type="checkbox" value="1" name="miraviaDebugMode" <?php echo checked($debugMode, '1', false)?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Use Feed API Only', 'miraviawoo')?>
<p class="description"><?php echo __('Use official Miravia Feed API for seller accounts (recommended)','miraviawoo')?></p>
</th>
<td>
<input type="checkbox" value="1" name="miravia_direct_api" <?php echo checked($directApi, '1', false)?> />
<p class="description"><strong>Note:</strong> Feed API is the only method available for seller accounts. Individual product APIs require 3rd party app registration.</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('App Key', 'miraviawoo')?>
<p class="description"><?php echo __('AliExpress/Miravia App Key from your developer account','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_app_key" value="<?php echo esc_attr($appKey)?>" style="width: 400px;" placeholder="Enter your App Key" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Secret Key', 'miraviawoo')?>
<p class="description"><?php echo __('AliExpress/Miravia Secret Key from your developer account','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_secret_key" value="<?php echo esc_attr($secretKey)?>" style="width: 400px;" placeholder="Enter your Secret Key" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Access Token', 'miraviawoo')?>
<p class="description"><?php echo __('AliExpress/Miravia Access Token for API authentication','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_access_token" value="<?php echo esc_attr($accessToken)?>" style="width: 400px;" placeholder="Enter your Access Token" />
<?php if($directApi == '1' && !empty($appKey) && !empty($secretKey) && !empty($accessToken)): ?>
<br><br>
<button type="button" id="test-api-connection" class="button">Test API Connection</button>
<div id="api-test-result" style="margin-top: 10px;"></div>
<?php endif; ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Status Orders', 'miraviawoo')?>
<p class="description"><?php echo __('Set default status for Miravia Orders','miraviawoo')?></p>
</th>
<td>
<select name="miravia_order_defuault_status">
<?php
foreach($statuses as $key => $status) {
if($key == 'wc-completed' || $key == 'wc-refunded' || $key == 'wc-failed' || $key == 'wc-cancelled') { continue; }
$selected = selected($key, $statusDefaultSelected, false);
echo '<option value="'.$key.'" '.$selected.'>'.$status.'</option>';
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Transport Mode', 'miraviawoo')?>
<p class="description"><?php echo __('Select DBM or DBS','miraviawoo')?></p>
</th>
<td>
<select name="miravia_transport_mode">
<option value="dbm" <?php selected($transportMode, 'dbm') ?>>DBM</option>
<option value="dbs" <?php selected($transportMode, 'dbs') ?>>DBS</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Stock Default', 'miraviawoo')?>
<p class="description"><?php echo __('When products have "In Stock" without quantity, set stock to send Miravia','miraviawoo')?></p>
</th>
<td>
<input type="number" value="<?php echo get_option('_miravia_default_stock', 100)?>" name="_miravia_default_stock" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('EAN Key', 'miraviawoo')?>
<p class="description"><?php echo __('This use for extract EAN code from products, enter the meta key for your EAN code','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_ean_key" value="<?php echo get_option('miravia_ean_key', '')?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Brand', 'miraviawoo')?>
<p class="description"><?php echo __('Set default Brand for your products','miraviawoo')?></p>
</th>
<td>
<select name="miravia_default_brand" class="search-select-miravia-brand">
<option value="<?php echo $defaultBrand ?>" <?php selected($defaultBrand, $defaultBrand) ?>><?php echo $defaultBrand ?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Unit', 'miraviawoo')?>
<p class="description"><?php echo __('Set default unit for all products','miraviawoo')?></p>
</th>
<td>
<select name="miravia_default_unit">
<option <?php echo selected('Units',$defaultUnit)?>value="Units">Units</option>
<option <?php echo selected('Liters',$defaultUnit)?>value="Liters">Liters</option>
<option <?php echo selected('Kilograms',$defaultUnit)?>value="Kilograms">Kilograms</option>
<option <?php echo selected('Grams',$defaultUnit)?>value="Grams">Grams</option>
<option <?php echo selected('Milliliters',$defaultUnit)?>value="Milliliters">Milliliters</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Unit Value', 'miraviawoo')?>
<p class="description"><?php echo __('Set default unit value for all products','miraviawoo')?></p>
</th>
<td>
<input type="text" name="miravia_default_unit_value" value="<?php echo $defaultUnitValue ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Default Category', 'miraviawoo')?>
<p class="description"><?php echo __('Set default unit value for all products','miraviawoo')?></p>
</th>
<td>
<select name="miravia_default_unit">
<?php
foreach($categories as $cat) {
echo "<option value='{$cat->term_id}'>{$cat->name}</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php echo __('Only Stock', 'miraviawoo')?>
<p class="description"><?php echo __('Check this if you want sync only stock','miraviawoo')?></p>
</th>
<td>
<input type="checkbox" value="1" name="miravia_only_stock" <?php echo checked(get_option('miravia_only_stock', '0'), '1', false)?>" />
</td>
</tr>
</table>
<input type="submit" class="button" value="<?php echo __('Save')?>" />
</form>
</div>
<script>
jQuery(document).ready(function($) {
$('#test-api-connection').click(function() {
var button = $(this);
var resultDiv = $('#api-test-result');
button.prop('disabled', true).text('Testing...');
resultDiv.html('<p>Testing API connection...</p>');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'test_miravia_api_connection',
nonce: '<?php echo wp_create_nonce('test_miravia_api'); ?>'
},
success: function(response) {
if(response.success) {
resultDiv.html('<div style="color: green; padding: 10px; background: #d4edda; border: 1px solid #c3e6cb; border-radius: 4px;"><strong>✓ Success:</strong> ' + response.data.message + '</div>');
} else {
resultDiv.html('<div style="color: #721c24; padding: 10px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px;"><strong>✗ Error:</strong> ' + response.data + '</div>');
}
},
error: function() {
resultDiv.html('<div style="color: #721c24; padding: 10px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px;"><strong>✗ Error:</strong> Failed to test connection</div>');
},
complete: function() {
button.prop('disabled', false).text('Test API Connection');
}
});
});
});
</script>