Datos de pedidos en WooCommerce versión 3.0+

Desde la última gran actualización de WooCommerce a su versión 3.0 las cosas han cambiado mucho:

No debemos acceder a las propiedades de WC_Order directamente como hacíamos antes y si lo intentamos, nos saldrá un error (aunque funcionará durante un tiempo, pero está “deprecated”).

Ahora se necesitan nuevos métodos, WC_Order y WC_Abstract_Order para conseguir los datos y para “setearlos” respectivamente
Hay una nueva clase para los elementos de los pedidos: WC_Order_Item class.
Por lo tanto, las propiedades de los elementos de pedido no estarán accesibles como antes en un bucle foreach y tendremos que usar este método específico de conseguir y establecer datos.

Uso de algunos métodos WC_Order y WC_Abstract_Order (ejemplo):

<br />
&lt;?php<br />
// Get an instance of the WC_Order object (same as before)<br />
 $order    = wc_get_order( $order_id );</p>
<p>// Get the order ID<br />
 $order_id = $order-&gt;get_id();</p>
<p>// Get the custumer ID<br />
 $order_id = $order-&gt;get_user_id();</p>
<p>// ... and so on ...<br />
?&gt;<br />

 

Obtener y acceder a las propiedades de los datos del pedido (en una matriz de valores):

<br />
&lt;?php<br />
// Get an instance of the WC_Order object<br />
 $order      = wc_get_order( $order_id );<br />
 $order_data = $order-&gt;get_data();</p>
<p>// The Order data<br />
 $order_id                   = $order_data['id'];<br />
 $order_parent_id            = $order_data['parent_id'];<br />
 $order_status               = $order_data['status'];<br />
 $order_currency             = $order_data['currency'];<br />
 $order_version              = $order_data['version'];<br />
 $order_payment_method       = $order_data['payment_method'];<br />
 $order_payment_method_title = $order_data['payment_method_title'];<br />
 $order_payment_method       = $order_data['payment_method'];<br />
 $order_payment_method       = $order_data['payment_method'];<br />
?&gt;<br />

 

Creación y modificación de fecha del objeto (WC_DateTime):

<br />
&lt;?php<br />
// Using a formated date ( with php date() function as method)<br />
 $order_date_created  = $order_data['date_created']-&gt;date('Y-m-d H:i:s');<br />
 $order_date_modified = $order_data['date_modified']-&gt;date('Y-m-d H:i:s');</p>
<p>// Using a timestamp ( with php getTimestamp() function as method)<br />
 $order_timestamp_created  = $order_data['date_created']-&gt;getTimestamp();<br />
 $order_timestamp_modified = $order_data['date_modified']-&gt;getTimestamp();<br />
 $order_discount_total     = $order_data['discount_total'];<br />
 $order_discount_tax       = $order_data['discount_tax'];<br />
 $order_shipping_total     = $order_data['shipping_total'];<br />
 $order_shipping_tax       = $order_data['shipping_tax'];<br />
 $order_total              = $order_data['cart_tax'];<br />
 $order_total_tax          = $order_data['total_tax'];<br />
 $order_customer_id        = $order_data['customer_id'];<br />
// ... and so on<br />
?&gt; 

 

Información de facturación:

<br />
&lt;?php<br />
$order_billing_first_name = $order_data['billing']['first_name'];<br />
$order_billing_last_name  = $order_data['billing']['last_name'];<br />
$order_billing_company    = $order_data['billing']['company'];<br />
$order_billing_address_1  = $order_data['billing']['address_1'];<br />
$order_billing_address_2  = $order_data['billing']['address_2'];<br />
$order_billing_city       = $order_data['billing']['city'];<br />
$order_billing_state      = $order_data['billing']['state'];<br />
$order_billing_postcode   = $order_data['billing']['postcode'];<br />
$order_billing_country    = $order_data['billing']['country'];<br />
$order_billing_email      = $order_data['billing']['email'];<br />
$order_billing_phone      = $order_data['billing']['phone'];<br />
?&gt;<br />

 

Información de envío:

<br />
&lt;?php<br />
$order_shipping_first_name = $order_data['shipping']['first_name'];<br />
$order_shipping_last_name  = $order_data['shipping']['last_name'];<br />
$order_shipping_company    = $order_data['shipping']['company'];<br />
$order_shipping_address_1  = $order_data['shipping']['address_1'];<br />
$order_shipping_address_2  = $order_data['shipping']['address_2'];<br />
$order_shipping_city       = $order_data['shipping']['city'];<br />
$order_shipping_state      = $order_data['shipping']['state'];<br />
$order_shipping_postcode   = $order_data['shipping']['postcode'];<br />
$order_shipping_country    = $order_data['shipping']['country'];<br />
?&gt;<br />

 

Obtener los elementos de pedido y acceder a los datos con el método WC_Order_Item:

<br />
&lt;?php<br />
// Get an instance of the WC_Order object<br />
$order = wc_get_order( $order_id );</p>
<p>// Iterating through each WC_Order_Item objects<br />
foreach( $order-&gt; get_items() as $item_key =&gt; $item_values ):</p>
<p>    ## Using WC_Order_Item methods ##</p>
<p>    // Item ID is directly accessible from the $item_key in the foreach loop or<br />
    $item_id   = $item_values-&gt;get_id();</p>
<p>    $item_name = $item_values-&gt;get_name(); // Name of the product<br />
    $item_type = $item_values-&gt;get_type(); // Type of the order item (&quot;line_item&quot;)</p>
<p>    ## Access Order Items data properties (in an array of values) ##<br />
    $item_data = $item_values-&gt;get_data();</p>
<p>    $product_name      = $item_data['name'];<br />
    $product_id        = $item_data['product_id'];<br />
    $variation_id      = $item_data['variation_id'];<br />
    $quantity          = $item_data['quantity'];<br />
    $tax_class         = $item_data['tax_class'];<br />
    $line_subtotal     = $item_data['subtotal'];<br />
    $line_subtotal_tax = $item_data['subtotal_tax'];<br />
    $line_total        = $item_data['total'];<br />
    $line_total_tax    = $item_data['total_tax'];</p>
<p>endforeach;<br />
?&gt;<br />

Así, el uso del método get_data() nos permite acceder a los datos protegidos (en forma de array asociativo)

Deje un Comentario

*Campos obligatorios Por favor valide los campos obligatorios

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.