/**
  * Get the order data for the given ID.
  *
  * @since  2.5.0
  * @param  WC_Order $order The order instance
  * @return array
  */
 protected function get_order_data($order)
 {
     $order_post = get_post($order->id);
     $dp = wc_get_price_decimals();
     $order_data = array('id' => $order->id, 'order_number' => $order->get_order_number(), 'created_at' => $this->format_datetime($order_post->post_date_gmt), 'updated_at' => $this->format_datetime($order_post->post_modified_gmt), 'completed_at' => $this->format_datetime($order->completed_date, true), 'status' => $order->get_status(), 'currency' => $order->get_order_currency(), 'total' => wc_format_decimal($order->get_total(), $dp), 'subtotal' => wc_format_decimal($order->get_subtotal(), $dp), 'total_line_items_quantity' => $order->get_item_count(), 'total_tax' => wc_format_decimal($order->get_total_tax(), $dp), 'total_shipping' => wc_format_decimal($order->get_total_shipping(), $dp), 'cart_tax' => wc_format_decimal($order->get_cart_tax(), $dp), 'shipping_tax' => wc_format_decimal($order->get_shipping_tax(), $dp), 'total_discount' => wc_format_decimal($order->get_total_discount(), $dp), 'shipping_methods' => $order->get_shipping_method(), 'payment_details' => array('method_id' => $order->payment_method, 'method_title' => $order->payment_method_title, 'paid' => isset($order->paid_date)), 'billing_address' => array('first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address_1' => $order->billing_address_1, 'address_2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'postcode' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'phone' => $order->billing_phone), 'shipping_address' => array('first_name' => $order->shipping_first_name, 'last_name' => $order->shipping_last_name, 'company' => $order->shipping_company, 'address_1' => $order->shipping_address_1, 'address_2' => $order->shipping_address_2, 'city' => $order->shipping_city, 'state' => $order->shipping_state, 'postcode' => $order->shipping_postcode, 'country' => $order->shipping_country), 'note' => $order->customer_note, 'customer_ip' => $order->customer_ip_address, 'customer_user_agent' => $order->customer_user_agent, 'customer_id' => $order->get_user_id(), 'view_order_url' => $order->get_view_order_url(), 'line_items' => array(), 'shipping_lines' => array(), 'tax_lines' => array(), 'fee_lines' => array(), 'coupon_lines' => array());
     // add line items
     foreach ($order->get_items() as $item_id => $item) {
         $product = $order->get_product_from_item($item);
         $product_id = null;
         $product_sku = null;
         // Check if the product exists.
         if (is_object($product)) {
             $product_id = isset($product->variation_id) ? $product->variation_id : $product->id;
             $product_sku = $product->get_sku();
         }
         $meta = new WC_Order_Item_Meta($item, $product);
         $item_meta = array();
         foreach ($meta->get_formatted(null) as $meta_key => $formatted_meta) {
             $item_meta[] = array('key' => $meta_key, 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']);
         }
         $order_data['line_items'][] = array('id' => $item_id, 'subtotal' => wc_format_decimal($order->get_line_subtotal($item, false, false), $dp), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), 'total' => wc_format_decimal($order->get_line_total($item, false, false), $dp), 'total_tax' => wc_format_decimal($item['line_tax'], $dp), 'price' => wc_format_decimal($order->get_item_total($item, false, false), $dp), 'quantity' => wc_stock_amount($item['qty']), 'tax_class' => !empty($item['tax_class']) ? $item['tax_class'] : null, 'name' => $item['name'], 'product_id' => $product_id, 'sku' => $product_sku, 'meta' => $item_meta);
     }
     // Add shipping.
     foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) {
         $order_data['shipping_lines'][] = array('id' => $shipping_item_id, 'method_id' => $shipping_item['method_id'], 'method_title' => $shipping_item['name'], 'total' => wc_format_decimal($shipping_item['cost'], $dp));
     }
     // Add taxes.
     foreach ($order->get_tax_totals() as $tax_code => $tax) {
         $order_data['tax_lines'][] = array('id' => $tax->id, 'rate_id' => $tax->rate_id, 'code' => $tax_code, 'title' => $tax->label, 'total' => wc_format_decimal($tax->amount, $dp), 'compound' => (bool) $tax->is_compound);
     }
     // Add fees.
     foreach ($order->get_fees() as $fee_item_id => $fee_item) {
         $order_data['fee_lines'][] = array('id' => $fee_item_id, 'title' => $fee_item['name'], 'tax_class' => !empty($fee_item['tax_class']) ? $fee_item['tax_class'] : null, 'total' => wc_format_decimal($order->get_line_total($fee_item), $dp), 'total_tax' => wc_format_decimal($order->get_line_tax($fee_item), $dp));
     }
     // Add coupons.
     foreach ($order->get_items('coupon') as $coupon_item_id => $coupon_item) {
         $order_data['coupon_lines'][] = array('id' => $coupon_item_id, 'code' => $coupon_item['name'], 'amount' => wc_format_decimal($coupon_item['discount_amount'], $dp));
     }
     $order_data = apply_filters('woocommerce_cli_order_data', $order_data);
     return $this->flatten_array($order_data);
 }
示例#2
0
 /**
  * Get order line items (products) in a neatly-formatted array of objects
  * with properties:
  *
  * + id - item ID
  * + name - item name, usually product title, processed through htmlentities()
  * + description - formatted item meta (e.g. Size: Medium, Color: blue), processed through htmlentities()
  * + quantity - item quantity
  * + item_total - item total (line total divided by quantity, excluding tax & rounded)
  * + line_total - line item total (excluding tax & rounded)
  * + meta - formatted item meta array
  * + product - item product or null if getting product from item failed
  * + item - raw item array
  *
  * @since 3.0.0
  * @param \WC_Order $order
  * @return array
  */
 public static function get_order_line_items($order)
 {
     $line_items = array();
     foreach ($order->get_items() as $id => $item) {
         $line_item = new stdClass();
         $product = $order->get_product_from_item($item);
         $meta = SV_WC_Plugin_Compatibility::is_wc_version_gte_2_4() ? $item : $item['item_meta'];
         // get meta + format it
         $item_meta = new WC_Order_Item_Meta($meta);
         $item_meta = $item_meta->get_formatted();
         if (!empty($item_meta)) {
             $item_desc = array();
             foreach ($item_meta as $meta) {
                 $item_desc[] = sprintf('%s: %s', $meta['label'], $meta['value']);
             }
             $item_desc = implode(', ', $item_desc);
         } else {
             // default description to SKU
             $item_desc = is_callable(array($product, 'get_sku')) && $product->get_sku() ? sprintf('SKU: %s', $product->get_sku()) : null;
         }
         $line_item->id = $id;
         $line_item->name = htmlentities($item['name'], ENT_QUOTES, 'UTF-8', false);
         $line_item->description = htmlentities($item_desc, ENT_QUOTES, 'UTF-8', false);
         $line_item->quantity = $item['qty'];
         $line_item->item_total = isset($item['recurring_line_total']) ? $item['recurring_line_total'] : $order->get_item_total($item);
         $line_item->line_total = $order->get_line_total($item);
         $line_item->meta = $item_meta;
         $line_item->product = is_object($product) ? $product : null;
         $line_item->item = $item;
         $line_items[] = $line_item;
     }
     return $line_items;
 }
 /**
  * Get the order data format for a new row per line item, compatible with the legacy (pre 3.0) CSV Export format
  *
  * Note this code was adapted from the old code to maintain compatibility as close as possible, so it should
  * not be modified unless absolutely necessary
  *
  * @since 3.0
  * @param array $order_data an array of order data for the given order
  * @param WC_Order $order the WC_Order object
  * @return array modified order data
  */
 private function get_legacy_one_row_per_line_item($order_data, WC_Order $order)
 {
     $data = array();
     foreach ($order->get_items() as $_ => $item) {
         $product = $order->get_product_from_item($item);
         if (!is_object($product)) {
             $product = new WC_Product(0);
         }
         $item_meta = new WC_Order_Item_Meta($item);
         $variation = $item_meta->display(true, true);
         if ($variation) {
             $variation = str_replace(array("\r", "\r\n", "\n"), '', $variation);
         }
         $order_data['line_item_sku'] = $product->get_sku();
         $order_data['line_item_name'] = str_replace(array('“', '”'), '', $item['name']);
         $order_data['line_item_variation'] = $variation;
         $order_data['line_item_amount'] = $item['qty'];
         $order_data['line_item_price'] = $order->get_line_total($item);
         // convert country codes to full name
         if (isset(WC()->countries->countries[$order->billing_country])) {
             $order_data['billing_country'] = WC()->countries->countries[$order->billing_country];
         }
         if (isset(WC()->countries->countries[$order->shipping_country])) {
             $order_data['shipping_country'] = WC()->countries->countries[$order->shipping_country];
         }
         // set order ID to order number
         $order_data['order_id'] = ltrim($order->get_order_number(), _x('#', 'hash before the order number', 'woocommerce-customer-order-csv-export'));
         $data[] = $order_data;
     }
     // handle orders with no line items
     if (empty($data)) {
         $data = $order_data;
     }
     return $data;
 }
 /**
  * Shipstation compatibility:
  *
  * Ensure that non-virtual containers/children, which are shipped, have a valid price that can be used for insurance calculations.
  *
  * Note: If you charge a static price for the bundle but ship bundled items individually, the only working solution is to spread the total value among the bundled items.
  *
  * @param  double   $price
  * @param  WC_Order $order
  * @param  array    $item
  * @param  boolean  $inc_tax
  * @param  boolean  $round
  * @return double
  */
 function order_amount_bundle_total($price, $order, $item, $inc_tax, $round)
 {
     global $wp, $woocommerce_bundles;
     if (isset($wp->query_vars['wc-api']) && $wp->query_vars['wc-api'] === 'wc_shipstation') {
         if (isset($item['bundled_items']) && isset($item['bundle_cart_key']) && isset($item['bundled_shipping']) && $item['bundled_shipping'] === 'yes') {
             $bundle_key = $item['bundle_cart_key'];
             $bundle_qty = $item['qty'];
             $bundle_value = $price;
             foreach ($order->get_items('line_item') as $order_item) {
                 if (isset($order_item['bundled_by']) && $order_item['bundled_by'] === $bundle_key) {
                     $bundle_value += $order->get_line_total($order_item, $inc_tax, $round) / $bundle_qty;
                 }
             }
             $price = $round ? round($bundle_value, 2) : $bundle_value;
         } elseif (isset($item['bundled_by']) && isset($item['bundle_cart_key']) && isset($item['bundled_shipping']) && $item['bundled_shipping'] === 'yes') {
             $parent = $woocommerce_bundles->order->get_bundled_order_item_container($item, $order);
             if ($parent && isset($parent['per_product_shipping']) && $parent['per_product_shipping'] === 'yes' && isset($parent['per_product_pricing']) && $parent['per_product_pricing'] === 'no' && isset($parent['bundle_cart_key'])) {
                 $bundle_value = $order->get_line_total($parent, $inc_tax, $round);
                 $bundle_key = $parent['bundle_cart_key'];
                 $child_count = 0;
                 foreach ($order->get_items('line_item') as $child_item) {
                     if (isset($child_item['bundled_by']) && $child_item['bundled_by'] === $bundle_key) {
                         $bundle_value += $order->get_line_total($child_item, $inc_tax, $round);
                         $child_count += $child_item['qty'];
                     }
                 }
                 $price = $round ? round($bundle_value / $child_count, 2) : $bundle_value / $child_count;
             }
         }
     }
     return $price;
 }
示例#5
0
">
                                        <?php 
            the_title();
            ?>
                                    </div>

                                    <div>
                                        <?php 
            print_r("Id: " . $order->id);
            ?>
                                    </div>

                                    <div>
                                        <?php 
            foreach ($order_items as $item) {
                print_r("Amount Paid: \$" . $order->get_line_total($item));
            }
            ?>
                                    </div>

                                    <div>
                                        <?php 
            foreach ($order_items as $item) {
                $unit_cost = $order->get_item_total($item);
                $total_cost = $order->get_line_total($item);
                $duration = $total_cost / $unit_cost;
                print_r("Reading Duration: " . $duration . ' minutes');
            }
            ?>
                                    </div>
 function order_data($post_id)
 {
     global $wpdb;
     $WooCommerceNFe_Format = new WooCommerceNFe_Format();
     $order = new WC_Order($post_id);
     $coupons = $order->get_used_coupons();
     $coupons_percentage = array();
     $total_discount = 0;
     $data = array();
     if ($coupons) {
         foreach ($coupons as $coupon_code) {
             $coupon_obj = new WC_Coupon($coupon_code);
             if ($coupon_obj->discount_type == 'percent') {
                 $coupons_percentage[] = $coupon_obj->coupon_amount;
             }
         }
     }
     if ($order->get_fees()) {
         foreach ($order->get_fees() as $key => $item) {
             if ($item['line_total'] < 0) {
                 $discount = $item['line_total'] * -1;
                 $total_discount = $discount + $total_discount;
             } else {
                 $codigo_ean = get_option('wc_settings_woocommercenfe_ean');
                 $codigo_ncm = get_option('wc_settings_woocommercenfe_ncm');
                 $codigo_cest = get_option('wc_settings_woocommercenfe_cest');
                 $origem = get_option('wc_settings_woocommercenfe_origem');
                 $imposto = get_option('wc_settings_woocommercenfe_imposto');
                 $data['produtos'][] = array('nome' => $item['name'], 'sku' => $product->get_sku(), 'ean' => $codigo_ean, 'ncm' => $codigo_ncm, 'cest' => $codigo_cest, 'quantidade' => 1, 'unidade' => 'UN', 'peso' => '0.100', 'origem' => (int) $origem, 'subtotal' => number_format($item['line_subtotal'], 2), 'total' => number_format($item['line_total'], 2), 'classe_imposto' => $imposto);
             }
         }
     }
     $total_discount = $order->get_total_discount() + $total_discount;
     // Order
     $data = array('ID' => $post_id, 'operacao' => 1, 'natureza_operacao' => get_option('wc_settings_woocommercenfe_natureza_operacao'), 'modelo' => 1, 'emissao' => 1, 'finalidade' => 1, 'ambiente' => (int) get_option('wc_settings_woocommercenfe_ambiente'));
     $data['pedido'] = array('pagamento' => 0, 'presenca' => 2, 'modalidade_frete' => 0, 'frete' => get_post_meta($order->id, '_order_shipping', true), 'desconto' => $total_discount, 'total' => $order->order_total);
     //Informações COmplementares ao Fisco
     $fiscoinf = get_option('wc_settings_woocommercenfe_fisco_inf');
     if (!empty($fiscoinf) && strlen($fiscoinf) <= 2000) {
         $data['pedido']['informacoes_fisco'] = $fiscoinf;
     }
     //Informações Complementares ao Consumidor
     $consumidorinf = get_option('wc_settings_woocommercenfe_cons_inf');
     if (!empty($consumidorinf) && strlen($consumidorinf) <= 2000) {
         $data['pedido']['informacoes_complementares'] = $consumidorinf;
     }
     // Customer
     $tipo_pessoa = get_post_meta($post_id, '_billing_persontype', true);
     if (!$tipo_pessoa) {
         $tipo_pessoa = 1;
     }
     if ($tipo_pessoa == 1) {
         $data['cliente'] = array('cpf' => $WooCommerceNFe_Format->cpf(get_post_meta($post_id, '_billing_cpf', true)), 'nome_completo' => get_post_meta($post_id, '_billing_first_name', true) . ' ' . get_post_meta($post_id, '_billing_last_name', true), 'endereco' => get_post_meta($post_id, '_shipping_address_1', true), 'complemento' => get_post_meta($post_id, '_shipping_address_2', true), 'numero' => get_post_meta($post_id, '_shipping_number', true), 'bairro' => get_post_meta($post_id, '_shipping_neighborhood', true), 'cidade' => get_post_meta($post_id, '_shipping_city', true), 'uf' => get_post_meta($post_id, '_shipping_state', true), 'cep' => $WooCommerceNFe_Format->cep(get_post_meta($post_id, '_shipping_postcode', true)), 'telefone' => get_user_meta($post_id, 'billing_phone', true), 'email' => get_post_meta($post_id, '_billing_email', true));
     }
     if ($tipo_pessoa == 2) {
         $data['cliente'] = array('cnpj' => $WooCommerceNFe_Format->cnpj(get_post_meta($post_id, '_billing_cnpj', true)), 'razao_social' => get_post_meta($post_id, '_billing_company', true), 'ie' => get_post_meta($post_id, '_billing_ie', true), 'endereco' => get_post_meta($post_id, '_shipping_address_1', true), 'complemento' => get_post_meta($post_id, '_shipping_address_2', true), 'numero' => get_post_meta($post_id, '_shipping_number', true), 'bairro' => get_post_meta($post_id, '_shipping_neighborhood', true), 'cidade' => get_post_meta($post_id, '_shipping_city', true), 'uf' => get_post_meta($post_id, '_shipping_state', true), 'cep' => $WooCommerceNFe_Format->cep(get_post_meta($post_id, '_shipping_postcode', true)), 'telefone' => get_user_meta($post_id, 'billing_phone', true), 'email' => get_post_meta($post_id, '_billing_email', true));
     }
     // Products
     foreach ($order->get_items() as $key => $item) {
         $product_id = $item['product_id'];
         $variation_id = $item['variation_id'];
         $ignorar_nfe = get_post_meta($product_id, '_nfe_ignorar_nfe', true);
         if ($ignorar_nfe == 1 || $order->get_item_subtotal($item, false, false) == 0) {
             $data['pedido']['total'] -= $item['line_subtotal'];
             if ($coupons_percentage) {
                 foreach ($coupons_percentage as $percentage) {
                     $data['pedido']['total'] += $percentage / 100 * $item['line_subtotal'];
                     $data['pedido']['desconto'] -= $percentage / 100 * $item['line_subtotal'];
                 }
             }
             $data['pedido']['total'] = number_format($data['pedido']['total'], 2);
             $data['pedido']['desconto'] = number_format($data['pedido']['desconto'], 2);
             continue;
         }
         $emitir = apply_filters('emitir_nfe_produto', true, $product_id);
         if ($variation_id) {
             $emitir = apply_filters('emitir_nfe_produto', true, $variation_id);
         }
         if ($emitir) {
             $product = $order->get_product_from_item($item);
             // Vars
             $codigo_ean = get_post_meta($product_id, '_nfe_codigo_ean', true);
             $codigo_ncm = get_post_meta($product_id, '_nfe_codigo_ncm', true);
             $codigo_cest = get_post_meta($product_id, '_nfe_codigo_cest', true);
             $origem = get_post_meta($product_id, '_nfe_origem', true);
             $imposto = get_post_meta($product_id, '_nfe_classe_imposto', true);
             $peso = $product->get_weight();
             if (!$peso) {
                 $peso = '0.100';
             }
             if (!$codigo_ean) {
                 $codigo_ean = get_option('wc_settings_woocommercenfe_ean');
             }
             if (!$codigo_ncm) {
                 $codigo_ncm = get_option('wc_settings_woocommercenfe_ncm');
             }
             if (!$codigo_cest) {
                 $codigo_cest = get_option('wc_settings_woocommercenfe_cest');
             }
             if (!is_numeric($origem)) {
                 $origem = get_option('wc_settings_woocommercenfe_origem');
             }
             if (!$imposto) {
                 $imposto = get_option('wc_settings_woocommercenfe_imposto');
             }
             // Attributes
             $variacoes = '';
             foreach (array_keys($item['item_meta']) as $meta) {
                 if (strpos($meta, 'pa_') !== false) {
                     $atributo = $item[$meta];
                     $nome_atributo = str_replace('pa_', '', $meta);
                     $nome_atributo = $wpdb->get_var("SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = '{$nome_atributo}'");
                     $valor = strtoupper($item[$meta]);
                     $variacoes .= ' - ' . strtoupper($nome_atributo) . ': ' . $valor;
                 }
             }
             $data['produtos'][] = array('nome' => $item['name'] . $variacoes, 'sku' => $product->get_sku(), 'ean' => $codigo_ean, 'ncm' => $codigo_ncm, 'cest' => $codigo_cest, 'quantidade' => $item['qty'], 'unidade' => 'UN', 'peso' => $peso, 'origem' => (int) $origem, 'subtotal' => number_format($order->get_item_subtotal($item, false, false), 2), 'total' => number_format($order->get_line_total($item, false, false), 2), 'classe_imposto' => $imposto);
         }
     }
     return $data;
 }
 /**
  * Get order line items (products) in a neatly-formatted array of objects
  * with properties:
  *
  * + id - item ID
  * + name - item name, usually product title, processed through htmlentities()
  * + description - formatted item meta (e.g. Size: Medium, Color: blue), processed through htmlentities()
  * + quantity - item quantity
  * + item_total - item total (line total divided by quantity, excluding tax & rounded)
  * + line_total - line item total (excluding tax & rounded)
  * + meta - formatted item meta array
  * + product - item product or null if getting product from item failed
  * + item - raw item array
  *
  * @since 3.0.0
  * @param \WC_Order $order
  * @return array
  */
 public static function get_order_line_items($order)
 {
     $line_items = array();
     foreach ($order->get_items() as $id => $item) {
         $line_item = new stdClass();
         $product = $order->get_product_from_item($item);
         $item_desc = array();
         // add SKU to description if available
         if (is_callable(array($product, 'get_sku')) && $product->get_sku()) {
             $item_desc[] = sprintf('SKU: %s', $product->get_sku());
         }
         // get meta + format it
         $item_meta = new WC_Order_Item_Meta($item);
         $item_meta = $item_meta->get_formatted();
         if (!empty($item_meta)) {
             foreach ($item_meta as $meta) {
                 $item_desc[] = sprintf('%s: %s', $meta['label'], $meta['value']);
             }
         }
         $item_desc = implode(', ', $item_desc);
         $line_item->id = $id;
         $line_item->name = htmlentities($item['name'], ENT_QUOTES, 'UTF-8', false);
         $line_item->description = htmlentities($item_desc, ENT_QUOTES, 'UTF-8', false);
         $line_item->quantity = $item['qty'];
         $line_item->item_total = isset($item['recurring_line_total']) ? $item['recurring_line_total'] : $order->get_item_total($item);
         $line_item->line_total = $order->get_line_total($item);
         $line_item->meta = $item_meta;
         $line_item->product = is_object($product) ? $product : null;
         $line_item->item = $item;
         $line_items[] = $line_item;
     }
     return $line_items;
 }