Order.php
· 6.5 KiB · PHP
Eredeti
// Order.php modified for dropshipping / API:
<?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>';
}
$user_id = $order->get_customer_id();
$customer = new WC_Customer($user_id);
// Get the company name from the user's profile
$company_name = get_user_meta($user_id, 'billing_company', true);
// Get the _nif meta key value from the user's profile
$nif = get_user_meta($user_id, '_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>'.$user_id.'</CustomerId>
<CustomerName>'.$company_name.'</CustomerName>
<CustomerCIF>'.$nif.'</CustomerCIF>
<CustomerMail>'.$order->get_billing_email().'</CustomerMail>
<CustomerTelefono>'.$order->get_billing_phone().'</CustomerTelefono>
<CustomerPais>'.$country_billing.'</CustomerPais>
<DirFactId>'.$order->get_id().'</DirFactId>
<DirFactNombre>'.$company_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>
<DirFactProvinciaCodigo>'.$state_billing.'</DirFactProvinciaCodigo>
<DirFactPais>'.$country_billing.'</DirFactPais>
<DirFactTelefono>'.$order->get_billing_phone().'</DirFactTelefono>
<DirFactMail>'.$order->get_billing_email().'</DirFactMail>
<DirFactCIF>'.$nif.'</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>'.$nif.'</DirEnvCIF>
<EcomTotalGastosEnvio>'.$order->get_shipping_total().'</EcomTotalGastosEnvio>
<Importe>'.$total_tax_excl.'</Importe>
<ImporteConIva>'.$total_tax_incl.'</ImporteConIva>
<MetodoPago>DEPOSITO</MetodoPago>
</CabeceraPedido>
<LineasPedido>
'.$order_lines.'
</LineasPedido>
</pedido>';
return $xml;
}
}
| 1 | // Order.php modified for dropshipping / API: |
| 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 | $items = $order->get_items(); |
| 73 | |
| 74 | $order_lines = ''; |
| 75 | |
| 76 | foreach ($items as $item) |
| 77 | { |
| 78 | $product = wc_get_product($item->get_product_id()); |
| 79 | |
| 80 | $total_line_tax_excl = round($item->get_subtotal(), 2); |
| 81 | |
| 82 | $total_line_tax_incl = round($item->get_subtotal() + $item->get_subtotal_tax(), 2); |
| 83 | |
| 84 | $order_lines .= '<linea> |
| 85 | <EcomPedidoId>'.$item->get_order_id().'</EcomPedidoId> |
| 86 | <EcomNumLineaId>'.$item->get_id().'</EcomNumLineaId> |
| 87 | <ProductoReferencia>'.$product->get_sku().'</ProductoReferencia> |
| 88 | <Cantidad>'.$item->get_quantity().'</Cantidad> |
| 89 | <Importe>'.$total_line_tax_excl.'</Importe> |
| 90 | <ImporteConIva>'.$total_line_tax_incl.'</ImporteConIva> |
| 91 | </linea>'; |
| 92 | } |
| 93 | |
| 94 | $user_id = $order->get_customer_id(); |
| 95 | $customer = new WC_Customer($user_id); |
| 96 | |
| 97 | // Get the company name from the user's profile |
| 98 | $company_name = get_user_meta($user_id, 'billing_company', true); |
| 99 | |
| 100 | // Get the _nif meta key value from the user's profile |
| 101 | $nif = get_user_meta($user_id, '_nif', true); |
| 102 | |
| 103 | $country_billing = $order->get_billing_country(); |
| 104 | $state_billing = $order->get_billing_state(); |
| 105 | $billing_state_name = WC()->countries->get_states($country_billing)[$state_billing]; |
| 106 | |
| 107 | $country_shipping = $order->get_shipping_country(); |
| 108 | $state_shipping = $order->get_shipping_state(); |
| 109 | $shipping_state_name = WC()->countries->get_states($country_shipping)[$state_shipping]; |
| 110 | |
| 111 | $total_tax_excl = round($order->get_subtotal(), 2); |
| 112 | |
| 113 | $total_tax_incl = round($order->get_total(), 2); |
| 114 | |
| 115 | $xml = '<?xml version="1.0"?> |
| 116 | <pedido> |
| 117 | <CabeceraPedido> |
| 118 | <EcomPedidoId>'.$order->get_id().'</EcomPedidoId> |
| 119 | <CustomerId>'.$user_id.'</CustomerId> |
| 120 | <CustomerName>'.$company_name.'</CustomerName> |
| 121 | <CustomerCIF>'.$nif.'</CustomerCIF> |
| 122 | <CustomerMail>'.$order->get_billing_email().'</CustomerMail> |
| 123 | <CustomerTelefono>'.$order->get_billing_phone().'</CustomerTelefono> |
| 124 | <CustomerPais>'.$country_billing.'</CustomerPais> |
| 125 | <DirFactId>'.$order->get_id().'</DirFactId> |
| 126 | <DirFactNombre>'.$company_name.'</DirFactNombre> |
| 127 | <DirFactDireccion>'.$order->get_billing_address_1().' '.$order->get_billing_address_2().'</DirFactDireccion> |
| 128 | <DirFactCodigoPostal>'.$order->get_billing_postcode().'</DirFactCodigoPostal> |
| 129 | <DirFactPoblacion>'.$order->get_shipping_city().'</DirFactPoblacion> |
| 130 | <DirFactProvincia>'.$billing_state_name.'</DirFactProvincia> |
| 131 | <DirFactProvinciaCodigo>'.$state_billing.'</DirFactProvinciaCodigo> |
| 132 | <DirFactPais>'.$country_billing.'</DirFactPais> |
| 133 | <DirFactTelefono>'.$order->get_billing_phone().'</DirFactTelefono> |
| 134 | <DirFactMail>'.$order->get_billing_email().'</DirFactMail> |
| 135 | <DirFactCIF>'.$nif.'</DirFactCIF> |
| 136 | <DirEnvId>'.$order->get_id().'</DirEnvId> |
| 137 | <DirEnvNombre>'.$order->get_shipping_first_name().' '.$order->get_shipping_last_name().'</DirEnvNombre> |
| 138 | <DirEnvDireccion>'.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().'</DirEnvDireccion> |
| 139 | <DirEnvCodigoPostal>'.$order->get_shipping_postcode().'</DirEnvCodigoPostal> |
| 140 | <DirEnvPoblacion>'.$order->get_shipping_city().'</DirEnvPoblacion> |
| 141 | <DirEnvProvincia>'.$shipping_state_name.'</DirEnvProvincia> |
| 142 | <DirEnvProvinciaCodigo>'.$state_shipping.'</DirEnvProvinciaCodigo> |
| 143 | <DirEnvPais>'.$country_shipping.'</DirEnvPais> |
| 144 | <DirEnvTelefono>'.$order->get_billing_phone().'</DirEnvTelefono> |
| 145 | <DirEnvMail>'.$order->get_billing_email().'</DirEnvMail> |
| 146 | <DirEnvCIF>'.$nif.'</DirEnvCIF> |
| 147 | <EcomTotalGastosEnvio>'.$order->get_shipping_total().'</EcomTotalGastosEnvio> |
| 148 | <Importe>'.$total_tax_excl.'</Importe> |
| 149 | <ImporteConIva>'.$total_tax_incl.'</ImporteConIva> |
| 150 | <MetodoPago>DEPOSITO</MetodoPago> |
| 151 | </CabeceraPedido> |
| 152 | <LineasPedido> |
| 153 | '.$order_lines.' |
| 154 | </LineasPedido> |
| 155 | </pedido>'; |
| 156 | |
| 157 | return $xml; |
| 158 | } |
| 159 | } |
| 160 |