// modified for QRh
log_context = ['source' => 'Order'];
$this->apis = ['order'];
$this->order_repository = new OrderRepository();
}
public function process()
{
$this->logger->info('Inicio sincronización pedidos', $this->log_context);
$posts = $this->order_repository->getOrders();
if (!empty($posts))
{
foreach ($posts as $post)
{
$response = $this->sendOrder($post);
}
}
else
{
$this->logger->info('No hay pedidos para procesar', $this->log_context);
echo 'No hay pedidos para procesar';
}
$this->logger->info('Fin sincronización pedidos', $this->log_context);
echo 'Proceso Terminado';
}
public function sendOrder($post)
{
$xml = $this->generateXml($post);
$api_config = $this->config['apis'][$this->apis[0]];
$api = new Api($api_config);
$result = $api->callApi($xml);
if ($result['response'] === true){
$this->order_repository->insertOrder($post->ID);
$this->logger->info('Pedido '.$post->ID.' enviado correctamente', $this->log_context);
}else{
$this->logger->error('Pedido '.$post->ID.' '.$result['msg'], $this->log_context);
}
}
public function generateXml($post)
{
$order = wc_get_order($post->ID);
$items = $order->get_items();
$order_lines = '';
foreach ($items as $item)
{
$product = wc_get_product($item->get_product_id());
$total_line_tax_excl = round($item->get_subtotal(), 2);
$total_line_tax_incl = round($item->get_subtotal() + $item->get_subtotal_tax(), 2);
$order_lines .= '
'.$item->get_order_id().'
'.$item->get_id().'
'.$product->get_sku().'
'.$item->get_quantity().'
'.$total_line_tax_excl.'
'.$total_line_tax_incl.'
';
}
//$customer = new WC_Customer($order->get_customer_id());
$dni = get_post_meta($post->ID, '_billing_nif', true);
$country_billing = $order->get_billing_country();
$state_billing = $order->get_billing_state();
$billing_state_name = WC()->countries->get_states( $country_billing )[$state_billing];
$country_shipping = $order->get_shipping_country();
$state_shipping = $order->get_shipping_state();
$shipping_state_name = WC()->countries->get_states( $country_shipping )[$state_shipping];
$total_tax_excl = round($order->get_subtotal(), 2);
$total_tax_incl = round($order->get_total(), 2);
$xml = '
'.$order->get_id().'
'.$order->get_customer_id().'
'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'
'.$dni.'
'.$order->get_billing_email().'
'.$order->get_billing_phone().'
'.$country_billing.'
'.$order->get_id().'
'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'
'.$order->get_billing_address_1().' '.$order->get_billing_address_2().'
'.$order->get_billing_postcode().'
'.$order->get_shipping_city().'
// '.$billing_state_name.'
'.$shipping_state_name.'
'.$state_billing.'
// '.$country_billing.'
'.$country_shipping.'
'.$order->get_billing_phone().'
'.$order->get_billing_email().'
'.$dni.'
'.$order->get_id().'
'.$order->get_shipping_first_name().' '.$order->get_shipping_last_name().'
'.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().'
'.$order->get_shipping_postcode().'
'.$order->get_shipping_city().'
'.$shipping_state_name.'
'.$state_shipping.'
'.$country_shipping.'
'.$order->get_billing_phone().'
'.$order->get_billing_email().'
'.$dni.'
'.$order->get_shipping_total().'
'.$total_tax_excl.'
'.$total_tax_incl.'
'.$order->get_payment_method().'
'.$order_lines.'
';
return $xml;
}
}