🔧 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>
128 lines
6.9 KiB
PHP
128 lines
6.9 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; }
|
|
|
|
if( !class_exists('MVOrder') ) {
|
|
class MVOrder {
|
|
function __construct(){
|
|
|
|
}
|
|
|
|
function create($order_miravia = false) {
|
|
//Obtener datos del pedido
|
|
$defaultStatus = get_option('miravia_default_status', 'wc-processing');
|
|
$order = wc_create_order();
|
|
if($order_miravia['address_shipping']['customerEmail']) {
|
|
$username = $order_miravia['address_shipping']['customerEmail'];
|
|
}else{
|
|
$username = $order_miravia['order_number'];
|
|
}
|
|
$user_id = username_exists( $username );
|
|
|
|
|
|
$billing_address = array(
|
|
'first_name' => $order_miravia['address_billing']['first_name'],
|
|
'last_name' => $order_miravia['address_billing']['last_name'],
|
|
'email' => $order_miravia['address_shipping']['customerEmail'],
|
|
'phone' => $order_miravia['address_billing']['phone'],
|
|
'address_1' => "{$order_miravia['address_billing']['address1']} {$order_miravia['address_billing']['address2']} {$order_miravia['address_billing']['address3']}",
|
|
'address_2' => "{$order_miravia['address_billing']['address4']} {$order_miravia['address_billing']['address5']}",
|
|
'city' => $order_miravia['address_billing']['city'],
|
|
'state' => $order_miravia['address_billing']['country'], //Ver esto
|
|
'postcode' => $order_miravia['address_billing']['post_code'],
|
|
'country' => $order_miravia['address_billing']['country'],
|
|
);
|
|
|
|
$shipping_address = array(
|
|
'first_name' => $order_miravia['address_shipping']['firstName'],
|
|
'last_name' => $order_miravia['address_shipping']['lastName'],
|
|
'address_1' => "{$order_miravia['address_shipping']['address1']} {$order_miravia['address_shipping']['address2']} {$order_miravia['address_shipping']['address3']}",
|
|
'address_2' => "{$order_miravia['address_shipping']['address4']} {$order_miravia['address_shipping']['address5']}",
|
|
'city' => $order_miravia['address_shipping']['city'],
|
|
'state' => $order_miravia['address_shipping']['country'], //Ver esto
|
|
'postcode' => $order_miravia['address_shipping']['postCode'],
|
|
'country' => $order_miravia['address_shipping']['country'],
|
|
);
|
|
|
|
$order->set_address( $billing_address, 'billing' );
|
|
$order->set_address( $shipping_address, 'shipping' );
|
|
update_post_meta($order->ID, '_miravia_order_id', $order_miravia['order_number']);
|
|
update_post_meta($order->ID, '_miravia_account_id', $order_miravia['account_id']);
|
|
// update_post_meta($order->ID, '_miravia_order_id', $order_miravia['order_number']);
|
|
// 1. User doesn't exist - Create it - send email - set address and define
|
|
if ( ! $user_id && $user_id == false ) {
|
|
$email = $username;
|
|
$password = wp_generate_password( 12, false );
|
|
$first_name = $order_miravia['address_shipping']['firstName'];
|
|
$last_name = $order_miravia['address_shipping']['lastName'];
|
|
|
|
$user_data = array(
|
|
'user_login' => $username,
|
|
'user_pass' => $password,
|
|
'user_email' => $email,
|
|
'first_name' => $first_name,
|
|
'last_name' => $last_name,
|
|
'role' => 'customer',
|
|
);
|
|
|
|
$user_id = wp_insert_user( $user_data );
|
|
|
|
// Update Billing and shipping user data
|
|
foreach( $billing_address as $key => $value ) {
|
|
update_user_meta( $user_id, 'billing_' . $key, $value );
|
|
}
|
|
|
|
foreach( $shipping_address as $key => $value ) {
|
|
update_user_meta( $user_id, 'shipping_' . $key, $value );
|
|
}
|
|
|
|
// No send notification on create user
|
|
// WC()->mailer()->get_emails()['WC_Email_Customer_New_Account']->trigger( $user_id, $password, true );
|
|
}
|
|
|
|
// For calculating taxes on items
|
|
$calculate_taxes_for = array(
|
|
'country' => ! empty($shipping_address['country']) ? $shipping_address['country'] : $billing_address['country'],
|
|
'state' => ! empty($shipping_address['state']) ? $shipping_address['state'] : $billing_address['state'],
|
|
'postcode' => ! empty($shipping_address['postcode']) ? $shipping_address['postcode'] : $billing_address['postcode'],
|
|
'city' => ! empty($shipping_address['city']) ? $shipping_address['city'] : $billing_address['city'],
|
|
);
|
|
|
|
foreach($order_miravia['order_items'] as $k => $item) {
|
|
$this->add_product($order, $item, $calculate_taxes_for);
|
|
}
|
|
|
|
$order->set_customer_id( $user_id );
|
|
|
|
$order->set_currency( get_woocommerce_currency() );
|
|
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
|
|
|
|
|
|
$note = 'From MIRAVIA Order';
|
|
$order->add_order_note( $note );
|
|
$order->set_customer_note( $note );
|
|
$order->set_status($defaultStatus, "Created from Miravia");
|
|
$order->calculate_totals();
|
|
$order->update_status('autoquote', true); // $order->save() is already included with update_status() method
|
|
}
|
|
|
|
function add_product($order, $product, $calculate_taxes_for) {
|
|
$product_sku = wc_get_product_id_by_sku($product['sku']);
|
|
$_product = $product_sku ? wc_get_product( $product_sku ) : false;
|
|
|
|
$cantidad = 1; //TODO ver esto
|
|
$priceWithOutTax = $product['item_price'] - (($product['item_price'] - $product['tax_amount']) / 100);
|
|
$dataLine = [
|
|
'name' => $product['name'],
|
|
'subtotal' => $priceWithOutTax,
|
|
'total' => $product['item_price']
|
|
];
|
|
$item_id = $order->add_product($_product, $cantidad, $dataLine);
|
|
|
|
wc_update_order_item_meta($item_id, '_miravia_delivery_option', $product['delivery_option_sof']);
|
|
wc_update_order_item_meta($item_id, '_miravia_order_item_id', $product['order_item_id']);
|
|
$line_item = $order->get_item( $item_id, false ); // Get the WC_Order_Item_Product Object instance from the Item Id
|
|
$line_item->calculate_taxes($calculate_taxes_for); // <== Calculating taxes
|
|
$line_item->save(); // Save data to WC_Order_Item_Product Object
|
|
}
|
|
}
|
|
} |