Order.php
· 6.5 KiB · PHP
Orginalformat
// modified for QRh
<?php
namespace Encodigoweb\Woocommerce\Handlers;
use Encodigoweb\Woocommerce\Repository\OrderRepository;
use Exception;
use WC_Customer;
final class Order extends Handler
{
public function __construct()
{
parent::__construct();
$this->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 .= '<linea>
<EcomPedidoId>'.$item->get_order_id().'</EcomPedidoId>
<EcomNumLineaId>'.$item->get_id().'</EcomNumLineaId>
<ProductoReferencia>'.$product->get_sku().'</ProductoReferencia>
<Cantidad>'.$item->get_quantity().'</Cantidad>
<Importe>'.$total_line_tax_excl.'</Importe>
<ImporteConIva>'.$total_line_tax_incl.'</ImporteConIva>
</linea>';
}
//$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 = '<?xml version="1.0"?>
<pedido>
<CabeceraPedido>
<EcomPedidoId>'.$order->get_id().'</EcomPedidoId>
<CustomerId>'.$order->get_customer_id().'</CustomerId>
<CustomerName>'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'</CustomerName>
<CustomerCIF>'.$dni.'</CustomerCIF>
<CustomerMail>'.$order->get_billing_email().'</CustomerMail>
<CustomerTelefono>'.$order->get_billing_phone().'</CustomerTelefono>
<CustomerPais>'.$country_billing.'</CustomerPais>
<DirFactId>'.$order->get_id().'</DirFactId>
<DirFactNombre>'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'</DirFactNombre>
<DirFactDireccion>'.$order->get_billing_address_1().' '.$order->get_billing_address_2().'</DirFactDireccion>
<DirFactCodigoPostal>'.$order->get_billing_postcode().'</DirFactCodigoPostal>
<DirFactPoblacion>'.$order->get_shipping_city().'</DirFactPoblacion>
// <DirFactProvincia>'.$billing_state_name.'</DirFactProvincia>
<DirFactProvincia>'.$shipping_state_name.'</DirFactProvincia>
<DirFactProvinciaCodigo>'.$state_billing.'</DirFactProvinciaCodigo>
// <DirFactPais>'.$country_billing.'</DirFactPais>
<DirFactPais>'.$country_shipping.'</DirFactPais>
<DirFactTelefono>'.$order->get_billing_phone().'</DirFactTelefono>
<DirFactMail>'.$order->get_billing_email().'</DirFactMail>
<DirFactCIF>'.$dni.'</DirFactCIF>
<DirEnvId>'.$order->get_id().'</DirEnvId>
<DirEnvNombre>'.$order->get_shipping_first_name().' '.$order->get_shipping_last_name().'</DirEnvNombre>
<DirEnvDireccion>'.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().'</DirEnvDireccion>
<DirEnvCodigoPostal>'.$order->get_shipping_postcode().'</DirEnvCodigoPostal>
<DirEnvPoblacion>'.$order->get_shipping_city().'</DirEnvPoblacion>
<DirEnvProvincia>'.$shipping_state_name.'</DirEnvProvincia>
<DirEnvProvinciaCodigo>'.$state_shipping.'</DirEnvProvinciaCodigo>
<DirEnvPais>'.$country_shipping.'</DirEnvPais>
<DirEnvTelefono>'.$order->get_billing_phone().'</DirEnvTelefono>
<DirEnvMail>'.$order->get_billing_email().'</DirEnvMail>
<DirEnvCIF>'.$dni.'</DirEnvCIF>
<EcomTotalGastosEnvio>'.$order->get_shipping_total().'</EcomTotalGastosEnvio>
<Importe>'.$total_tax_excl.'</Importe>
<ImporteConIva>'.$total_tax_incl.'</ImporteConIva>
<MetodoPago>'.$order->get_payment_method().'</MetodoPago>
</CabeceraPedido>
<LineasPedido>
'.$order_lines.'
</LineasPedido>
</pedido>';
return $xml;
}
}
| 1 | // modified for QRh |
| 2 | |
| 3 | <?php |
| 4 | |
| 5 | namespace Encodigoweb\Woocommerce\Handlers; |
| 6 | |
| 7 | use Encodigoweb\Woocommerce\Repository\OrderRepository; |
| 8 | use Exception; |
| 9 | use WC_Customer; |
| 10 | |
| 11 | final class Order extends Handler |
| 12 | { |
| 13 | public function __construct() |
| 14 | { |
| 15 | parent::__construct(); |
| 16 | |
| 17 | $this->log_context = ['source' => 'Order']; |
| 18 | |
| 19 | $this->apis = ['order']; |
| 20 | |
| 21 | $this->order_repository = new OrderRepository(); |
| 22 | } |
| 23 | |
| 24 | public function process() |
| 25 | { |
| 26 | $this->logger->info('Inicio sincronización pedidos', $this->log_context); |
| 27 | |
| 28 | $posts = $this->order_repository->getOrders(); |
| 29 | |
| 30 | if (!empty($posts)) |
| 31 | { |
| 32 | foreach ($posts as $post) |
| 33 | { |
| 34 | $response = $this->sendOrder($post); |
| 35 | } |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | $this->logger->info('No hay pedidos para procesar', $this->log_context); |
| 40 | |
| 41 | echo 'No hay pedidos para procesar'; |
| 42 | } |
| 43 | |
| 44 | $this->logger->info('Fin sincronización pedidos', $this->log_context); |
| 45 | |
| 46 | echo 'Proceso Terminado'; |
| 47 | } |
| 48 | |
| 49 | public function sendOrder($post) |
| 50 | { |
| 51 | $xml = $this->generateXml($post); |
| 52 | |
| 53 | $api_config = $this->config['apis'][$this->apis[0]]; |
| 54 | |
| 55 | $api = new Api($api_config); |
| 56 | |
| 57 | $result = $api->callApi($xml); |
| 58 | |
| 59 | if ($result['response'] === true){ |
| 60 | $this->order_repository->insertOrder($post->ID); |
| 61 | |
| 62 | $this->logger->info('Pedido '.$post->ID.' enviado correctamente', $this->log_context); |
| 63 | }else{ |
| 64 | $this->logger->error('Pedido '.$post->ID.' '.$result['msg'], $this->log_context); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function generateXml($post) |
| 69 | { |
| 70 | $order = wc_get_order($post->ID); |
| 71 | |
| 72 | |
| 73 | $items = $order->get_items(); |
| 74 | |
| 75 | $order_lines = ''; |
| 76 | |
| 77 | foreach ($items as $item) |
| 78 | { |
| 79 | $product = wc_get_product($item->get_product_id()); |
| 80 | |
| 81 | $total_line_tax_excl = round($item->get_subtotal(), 2); |
| 82 | |
| 83 | $total_line_tax_incl = round($item->get_subtotal() + $item->get_subtotal_tax(), 2); |
| 84 | |
| 85 | $order_lines .= '<linea> |
| 86 | <EcomPedidoId>'.$item->get_order_id().'</EcomPedidoId> |
| 87 | <EcomNumLineaId>'.$item->get_id().'</EcomNumLineaId> |
| 88 | <ProductoReferencia>'.$product->get_sku().'</ProductoReferencia> |
| 89 | <Cantidad>'.$item->get_quantity().'</Cantidad> |
| 90 | <Importe>'.$total_line_tax_excl.'</Importe> |
| 91 | <ImporteConIva>'.$total_line_tax_incl.'</ImporteConIva> |
| 92 | </linea>'; |
| 93 | } |
| 94 | |
| 95 | //$customer = new WC_Customer($order->get_customer_id()); |
| 96 | |
| 97 | $dni = get_post_meta($post->ID, '_billing_nif', true); |
| 98 | |
| 99 | $country_billing = $order->get_billing_country(); |
| 100 | $state_billing = $order->get_billing_state(); |
| 101 | $billing_state_name = WC()->countries->get_states( $country_billing )[$state_billing]; |
| 102 | |
| 103 | $country_shipping = $order->get_shipping_country(); |
| 104 | $state_shipping = $order->get_shipping_state(); |
| 105 | $shipping_state_name = WC()->countries->get_states( $country_shipping )[$state_shipping]; |
| 106 | |
| 107 | $total_tax_excl = round($order->get_subtotal(), 2); |
| 108 | |
| 109 | $total_tax_incl = round($order->get_total(), 2); |
| 110 | |
| 111 | $xml = '<?xml version="1.0"?> |
| 112 | <pedido> |
| 113 | <CabeceraPedido> |
| 114 | <EcomPedidoId>'.$order->get_id().'</EcomPedidoId> |
| 115 | <CustomerId>'.$order->get_customer_id().'</CustomerId> |
| 116 | <CustomerName>'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'</CustomerName> |
| 117 | <CustomerCIF>'.$dni.'</CustomerCIF> |
| 118 | <CustomerMail>'.$order->get_billing_email().'</CustomerMail> |
| 119 | <CustomerTelefono>'.$order->get_billing_phone().'</CustomerTelefono> |
| 120 | <CustomerPais>'.$country_billing.'</CustomerPais> |
| 121 | <DirFactId>'.$order->get_id().'</DirFactId> |
| 122 | <DirFactNombre>'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'</DirFactNombre> |
| 123 | <DirFactDireccion>'.$order->get_billing_address_1().' '.$order->get_billing_address_2().'</DirFactDireccion> |
| 124 | <DirFactCodigoPostal>'.$order->get_billing_postcode().'</DirFactCodigoPostal> |
| 125 | <DirFactPoblacion>'.$order->get_shipping_city().'</DirFactPoblacion> |
| 126 | // <DirFactProvincia>'.$billing_state_name.'</DirFactProvincia> |
| 127 | <DirFactProvincia>'.$shipping_state_name.'</DirFactProvincia> |
| 128 | <DirFactProvinciaCodigo>'.$state_billing.'</DirFactProvinciaCodigo> |
| 129 | // <DirFactPais>'.$country_billing.'</DirFactPais> |
| 130 | <DirFactPais>'.$country_shipping.'</DirFactPais> |
| 131 | <DirFactTelefono>'.$order->get_billing_phone().'</DirFactTelefono> |
| 132 | <DirFactMail>'.$order->get_billing_email().'</DirFactMail> |
| 133 | <DirFactCIF>'.$dni.'</DirFactCIF> |
| 134 | <DirEnvId>'.$order->get_id().'</DirEnvId> |
| 135 | <DirEnvNombre>'.$order->get_shipping_first_name().' '.$order->get_shipping_last_name().'</DirEnvNombre> |
| 136 | <DirEnvDireccion>'.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().'</DirEnvDireccion> |
| 137 | <DirEnvCodigoPostal>'.$order->get_shipping_postcode().'</DirEnvCodigoPostal> |
| 138 | <DirEnvPoblacion>'.$order->get_shipping_city().'</DirEnvPoblacion> |
| 139 | <DirEnvProvincia>'.$shipping_state_name.'</DirEnvProvincia> |
| 140 | <DirEnvProvinciaCodigo>'.$state_shipping.'</DirEnvProvinciaCodigo> |
| 141 | <DirEnvPais>'.$country_shipping.'</DirEnvPais> |
| 142 | <DirEnvTelefono>'.$order->get_billing_phone().'</DirEnvTelefono> |
| 143 | <DirEnvMail>'.$order->get_billing_email().'</DirEnvMail> |
| 144 | <DirEnvCIF>'.$dni.'</DirEnvCIF> |
| 145 | <EcomTotalGastosEnvio>'.$order->get_shipping_total().'</EcomTotalGastosEnvio> |
| 146 | <Importe>'.$total_tax_excl.'</Importe> |
| 147 | <ImporteConIva>'.$total_tax_incl.'</ImporteConIva> |
| 148 | <MetodoPago>'.$order->get_payment_method().'</MetodoPago> |
| 149 | </CabeceraPedido> |
| 150 | <LineasPedido> |
| 151 | '.$order_lines.' |
| 152 | </LineasPedido> |
| 153 | </pedido>'; |
| 154 | |
| 155 | return $xml; |
| 156 | } |
| 157 | } |
| 158 |