the_shortcodes = array(
'aiow_order_items_table',
);
parent::__construct();
}
/**
* Extra attributes.
*
* @param array $atts Attributes.
* @return array
*/
function add_extra_atts( $atts ) {
$modified_atts = array_merge( array(
'order_id' => ( isset( $_GET['order_id'] ) ) ? $_GET['order_id'] : get_the_ID(),
'hide_currency' => 'no',
'table_class' => '',
'shipping_as_item' => '',
'discount_as_item' => '',
'columns' => '',
'columns_titles' => '',
'columns_styles' => '',
'tax_percent_format' => '%.2f %%',
'item_image_width' => 0,
'item_image_height' => 0,
'product_image_width' => 0,
'product_image_height' => 0,
'price_prefix' => '',
'quantity_prefix' => '',
'style_item_name_variation' => 'font-size:smaller;',
'variation_as_metadata' => 'yes',
'wc_extra_product_options_show_price' => 'no',
'order_user_roles' => '',
'exclude_by_categories' => '',
'exclude_by_tags' => '',
'exclude_by_attribute__name' => '',
'exclude_by_attribute__value' => '',
'add_variation_info_to_item_name' => 'yes',
'insert_page_break' => '',
'multiply_cost' => 1,
'multiply_profit' => 1,
'refunded_items_table' => 'no',
'hide_zero_prices' => 'no',
'multicolumns_glue' => '
',
'sort_by_column' => 0,
'product_barcode_width' => 60,
'product_barcode_height' => 60,
), $atts );
return $modified_atts;
}
/**
* Init all attribute.
*
* @param array $atts Attribute.
* @return array
*/
function init_atts( $atts ) {
$this->the_order = ( 'shop_order' === get_post_type( $atts['order_id'] ) ) ? wc_get_order( $atts['order_id'] ) : null;
if ( ! $this->the_order ) {
return false;
}
if ( 0 != $atts['item_image_width'] ) {
$atts['product_image_width'] = $atts['item_image_width'];
}
if ( 0 != $atts['item_image_height'] ) {
$atts['product_image_height'] = $atts['item_image_height'];
}
return $atts;
}
/**
* Extra Check.
*
* @param array $atts Attributes.
* @return bool
*/
function extra_check( $atts ) {
if ( '' != $atts['order_user_roles'] ) {
$user_info = get_userdata( $this->the_order->customer_user );
$user_roles = $user_info->roles;
$user_roles_to_check = explode( ',', $atts['order_user_roles'] );
foreach ( $user_roles_to_check as $user_role_to_check ) {
if ( in_array( $user_role_to_check, $user_roles ) ) {
return true;
}
}
return false;
}
return true;
}
/**
* Price shortcode.
*
* @param string $raw_price Raw price.
* @param array $atts Attributes.
* @return string
*/
private function aiow_price_shortcode( $raw_price, $atts ) {
if ( 'yes' === $atts['hide_zero_prices'] && 0 == $raw_price ) {
return '';
}
return aiow_price( $atts['price_prefix'] . $raw_price, aiow_get_order_currency( $this->the_order ), $atts['hide_currency'] );
}
/**
* Add items.
*
* @param array $items Items.
* @param array $new_item_args New items.
* @return array
*/
private function add_item( $items, $new_item_args = array() ) {
if ( empty ( $new_item_args ) ) {
return $items;
}
extract( $new_item_args );
// Create item
if ( AIOW_IS_WC_VERSION_BELOW_3 ) {
$item = array(
'is_custom' => true,
'name' => $name,
'type' => 'line_item',
'qty' => $qty,
'line_subtotal' => $line_subtotal,
'line_total' => $line_total,
'line_tax' => $line_tax,
'line_subtotal_tax' => $line_subtotal_tax,
'item_meta' => array(
'_qty' => array( $qty ),
'_line_subtotal' => array( $line_subtotal ),
'_line_total' => array( $line_total ),
'_line_tax' => array( $line_tax ),
'_line_subtotal_tax' => array( $line_subtotal_tax ),
),
);
} else {
if ( 'shipping' === $type ) {
$item = new WC_Order_Item_Shipping();
$item->set_props( array(
'method_title' => $name,
'method_id' => '',
'total' => wc_format_decimal( $line_total ),
'total_tax' => $line_tax,
'taxes' => array(
'total' => array( $line_tax ),
),
) );
} else { // ( 'discount' === $type )
$item = new WC_Order_Item_Fee();
$item->set_props( array(
'name' => $name,
'total' => wc_format_decimal( $line_total ),
'total_tax' => $line_tax,
'tax_class' => '',
'tax_status' => 'taxable',
'taxes' => array(
'total' => array( $line_tax ),
),
) );
}
}
$items[] = $item;
return $items;
}
/**
* Get tax class.
*
* @param string $tax_class Tax class name.
* @return bool
*/
function get_tax_class_name( $tax_class ) {
$tax_classes = WC_Tax::get_tax_classes();
$classes_names = array();
$classes_names[''] = __( 'Standard', 'woocommerce' );
if ( ! empty( $tax_classes ) ) {
foreach ( $tax_classes as $class ) {
$classes_names[ sanitize_title( $class ) ] = esc_html( $class );
}
}
return ( isset( $classes_names[ $tax_class ] ) ) ? $classes_names[ $tax_class ] : '';
}
/**
* Get meta info.
*
* @param string $item_id Item ID.
* @param mixed $the_product Product.
* @return string
*/
function get_meta_info( $item_id, $the_product ) {
$meta_info = '';
if ( $metadata = $this->the_order->has_meta( $item_id ) ) {
$meta_info = array();
foreach ( $metadata as $meta ) {
// Skip hidden core fields
if ( in_array( $meta['meta_key'], apply_filters( 'woocommerce_hidden_order_itemmeta', array(
'_qty',
'_tax_class',
'_product_id',
'_variation_id',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'method_id',
'cost'
) ) ) ) {
continue;
}
// Skip serialised meta
if ( is_serialized( $meta['meta_value'] ) ) {
continue;
}
// Get attribute data
if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ) ) {
$term = get_term_by( 'slug', $meta['meta_value'], wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
$meta['meta_key'] = wc_attribute_label( wc_sanitize_taxonomy_name( $meta['meta_key'] ) );
$meta['meta_value'] = isset( $term->name ) ? $term->name : $meta['meta_value'];
} else {
$meta['meta_key'] = ( is_object( $the_product ) ) ? wc_attribute_label( $meta['meta_key'], $the_product ) : $meta['meta_key'];
}
$meta_info[] = wp_kses_post( rawurldecode( $meta['meta_key'] ) ) . ': ' . wp_kses_post( rawurldecode( $meta['meta_value'] ) );
}
$meta_info = implode( ', ', $meta_info );
}
return $meta_info;
}
/**
* Order item table.
*
* @param array $atts Attibutes.
* @param string $content Content.
* @return string
*/
function aiow_order_items_table( $atts, $content = '' ) {
$the_order = $this->the_order;
// Get columns
$columns = explode( '|', $atts['columns'] );
if ( empty( $columns ) ) {
return '';
}
// Column titles and styles
$columns_titles = ( '' == $atts['columns_titles'] ) ? array() : explode( '|', $atts['columns_titles'] );
$columns_styles = ( '' == $atts['columns_styles'] ) ? array() : explode( '|', $atts['columns_styles'] );
// The Items
$the_items = array();
if ( 'yes' === $atts['refunded_items_table'] ) {
foreach ( $this->the_order->get_refunds() as $_refund ) {
$the_items = array_merge( $the_items, $_refund->get_items() );
}
} else {
$the_items = $the_order->get_items();
}
// Shipping as item
if ( '' != $atts['shipping_as_item'] && $the_order->get_total_shipping() > 0 ) {
$name = str_replace( '%shipping_method_name%', $the_order->get_shipping_method(), $atts['shipping_as_item'] );
$total_shipping_tax_excl = $the_order->get_total_shipping();
$shipping_tax = $the_order->get_shipping_tax();
$the_items = $this->add_item( $the_items, array(
'name' => $name,
'qty' => 1,
'line_subtotal' => $total_shipping_tax_excl,
'line_total' => $total_shipping_tax_excl,
'line_tax' => $shipping_tax,
'line_subtotal_tax' => $shipping_tax,
'type' => 'shipping',
) );
}
// Discount as item
if ( '' != $atts['discount_as_item'] && $the_order->get_total_discount( true ) > 0 ) {
$name = $atts['discount_as_item'];
$total_discount_tax_excl = $the_order->get_total_discount( true );
$discount_tax = $the_order->get_total_discount( false ) - $total_discount_tax_excl;
$total_discount_tax_excl *= -1;
$discount_tax *= -1;
$the_items = $this->add_item( $the_items, array(
'name' => $name,
'qty' => 1,
'line_subtotal' => $total_discount_tax_excl,
'line_total' => $total_discount_tax_excl,
'line_tax' => $discount_tax,
'line_subtotal_tax' => $discount_tax,
'type' => 'discount'
) );
}
// Items to data[]
$data = array();
$item_counter = 0;
foreach ( $the_items as $item_id => $item ) {
$item['is_custom'] = ( isset( $item['is_custom'] ) );
$the_product = ( true === $item['is_custom'] ) ? null : ( method_exists( $item, 'get_product' ) ? $item->get_product( $item ) : null );
// Check if it's not excluded by category
if ( '' != $atts['exclude_by_categories'] && $the_product ) {
if ( aiow_product_has_terms( $the_product, $atts['exclude_by_categories'], 'product_cat' ) ) {
continue;
}
}
// Check if it's not excluded by tag
if ( '' != $atts['exclude_by_tags'] && $the_product ) {
if ( aiow_product_has_terms( $the_product, $atts['exclude_by_tags'], 'product_tag' ) ) {
continue;
}
}
// Check if it's not excluded by product attribute
if ( $the_product && '' != $atts['exclude_by_attribute__name'] ) {
$product_attributes = $the_product->get_attributes();
if ( isset( $product_attributes[ $atts['exclude_by_attribute__name'] ] ) ) {
$product_attribute = $product_attributes[ $atts['exclude_by_attribute__name'] ];
if ( is_object( $product_attribute ) ) {
if ( 'WC_Product_Attribute' === get_class( $product_attribute ) && in_array( $atts['exclude_by_attribute__value'], $product_attribute->get_options() ) ) {
continue;
}
} elseif ( $atts['exclude_by_attribute__value'] === $product_attribute ) {
continue;
}
}
}
$item_counter++;
// Columns
foreach( $columns as $cell_columns ) {
$cell_columns = explode( ',', $cell_columns );
$cell_data = array();
foreach ( $cell_columns as $column ) {
$column_param = '';
if ( false !== ( $pos = strpos( $column, '=' ) ) ) {
$column_param = substr( $column, $pos + 1 );
$column = substr( $column, 0, $pos );
}
$column_cell_data = $this->get_cell( $column, $column_param, $atts, $the_order, $columns, $item_counter, $item_id, $item, $the_product );
$column_cell_data = apply_filters( 'aiow_pdf_invoicing_cell_data', $column_cell_data, array(
'column' => $column,
'column_param' => $column_param,
'item' => $item,
'item_id' => $item_id,
'item_counter' => $item_counter,
'product' => $the_product,
'order' => $this->the_order,
) );
if ( '' !== $column_cell_data ) {
$cell_data[] = $column_cell_data;
}
}
$cell_data = ( empty( $cell_data ) ? '' : implode( $atts['multicolumns_glue'], $cell_data ) );
$data[ $item_counter ][] = $cell_data;
}
}
if ( empty( $data ) ) {
return '';
}
// Sorting
if ( $atts['sort_by_column'] > 0 ) {
$this->sort_by_column = $atts['sort_by_column'];
usort( $data, array( $this, 'sort_data' ) );
}
// Final filter
$data = apply_filters( 'aiow_order_items_table_data', $data );
// Final HTML
$html = '';
$table_html_args = array(
'table_class' => $atts['table_class'],
'table_heading_type' => 'horizontal',
'columns_classes' => array(),
'columns_styles' => $columns_styles,
);
if ( '' != $atts['insert_page_break'] ) {
$page_breaks = explode ( '|', $atts['insert_page_break'] );
$data_size = count( $data );
$slice_offset = 0;
$html = '';
$slices = 0;
while ( $slice_offset < $data_size ) {
if ( 0 != $slice_offset ) {
$html .= '