-
Notifications
You must be signed in to change notification settings - Fork 118
/
add-line-item-product-addons-xml-tag.php
105 lines (83 loc) · 2.89 KB
/
add-line-item-product-addons-xml-tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php // only copy this line if needed
/**
* Adds Product Add-ons separately in a new XML tag for each line item
*
* Format:
* <OrderLineItems>
* <OrderLineItem>
* <AddOn>
* <Name>
* <Value>
* <Price>
* </AddOn>
* </OrderLineItem>
* </OrderLineItems>
*/
/**
* Adds Product Addons to the Line Item XML if available
*
* REQUIRES v2.0+ of XML Export; use `wc_customer_order_xml_export_suite_order_export_line_item_format` filter for earlier versions
*
* @param array $item_data line item XML data to write
* @param \WC_Order $order
* @param array|\WC_Order_Item $item the line item order data or object (WC 3.0+)
* @return array - modified line item XML data to write
*/
function sv_wc_xml_export_line_item_addons( $item_data, $order, $item ) {
$product = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : $order->get_product_from_item( $item );
// bail if this line item isn't a product
if ( ! ( $product && $product->exists() ) ) {
return $item_data;
}
$addons = [];
// get the possible add-ons for this line item to check if they're in the order
if ( is_callable( 'WC_Product_Addons_Helper::get_product_addons' ) ) {
$addons = WC_Product_Addons_Helper::get_product_addons( $product->get_id() );
} elseif( is_callable( 'get_product_addons' ) ) {
$addons = get_product_addons( $product->get_id() );
}
$product_addons = sv_wc_xml_export_get_line_item_addons( $item, $addons );
if ( ! empty( $product_addons ) ) {
$item_data['AddOn'] = $product_addons;
}
return $item_data;
}
add_filter( 'wc_customer_order_export_xml_order_line_item', 'sv_wc_xml_export_line_item_addons', 10, 3 );
/**
* Gets Product Add-ons for a line item
*
* @param array|\WC_Order_Item $item line item data
* @param array $addons possible addons for this line item
* @return array - product addons ordered for the line item
*/
function sv_wc_xml_export_get_line_item_addons( $item, $addons ) {
$product_addons = array();
foreach ( $addons as $addon ) {
// sanity check
if ( ! is_array( $addon ) || empty ( $addon ) ) {
return $product_addons;
}
if ( $item instanceof \WC_Order_Item ) {
$metadata = wp_list_pluck( $item->get_formatted_meta_data(), 'value', 'key' );
} else {
$metadata = $item;
}
// loop line item data
foreach ( $metadata as $key => $value ) {
// check if the beginning of the meta key matches the add-on name
if ( $addon['name'] == substr( $key, 0, strlen( $addon['name'] ) ) ) {
// this way, the length will be 0 without a trailing paren to get a "false" $price
$price = substr( $key, strrpos( $key, '(' ), strrpos( $key, ')' ) );
if ( $price ) {
preg_match( '#\((.*?)\)#', $price, $match );
}
$product_addons[] = array(
'Name' => $addon['name'],
'Value' => $value,
'Price' => $price ? html_entity_decode( $match[1] ) : ' - ',
);
}
}
}
return $product_addons;
}