|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Bootstraps the Vendor system |
|
4
|
|
|
* |
|
5
|
|
|
* @package PrintCenter\Vendor |
|
6
|
|
|
* @since 1.0.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
// Exit if accessed directly |
|
11
|
|
|
if( ! defined( 'ABSPATH' ) ) { |
|
12
|
|
|
exit; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Product Vendors Import/Export Handler |
|
17
|
|
|
* |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class WooCommerce_Product_Vendors_Export_Handler { |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Setup class |
|
25
|
|
|
* |
|
26
|
|
|
* @access public |
|
27
|
|
|
* @since 1.0.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() { |
|
30
|
|
|
// Customer / Order CSV Export column headers/data |
|
31
|
|
|
add_filter( 'wc_customer_order_csv_export_order_headers', array( $this, 'add_vendors_to_csv_export_column_headers' ), 10, 2 ); |
|
32
|
|
|
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', array( $this, 'add_vendors_to_csv_export_column_data' ), 10, 4 ); |
|
33
|
|
|
|
|
34
|
|
|
// Customer / Order CSV Export line item data |
|
35
|
|
|
add_filter( 'wc_customer_order_csv_export_order_line_item', array( $this, 'csv_export_line_item_data' ), 10, 5 ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Adds support for Customer/Order CSV Export by adding a vendor column header |
|
41
|
|
|
* |
|
42
|
|
|
* @access public |
|
43
|
|
|
* @since 1.0.0 |
|
44
|
|
|
* @param array $headers existing array of header key/names for the CSV export |
|
45
|
|
|
* @param WC_Customer_Order_CSV_Export_Generator $csv_generator instance |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function add_vendors_to_csv_export_column_headers( $headers, $csv_generator ) { |
|
|
|
|
|
|
49
|
|
|
$headers['vendors'] = 'vendors'; |
|
50
|
|
|
|
|
51
|
|
|
return $headers; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Adds support for Customer/Order CSV Export by adding product vendor data |
|
57
|
|
|
* for each order item row |
|
58
|
|
|
* |
|
59
|
|
|
* @access public |
|
60
|
|
|
* @since 1.0.0 |
|
61
|
|
|
* @param array $order_data generated order data matching the column keys in the header |
|
62
|
|
|
* @param array $item data for the specific item |
|
63
|
|
|
* @param WC_Order $order order being exported |
|
64
|
|
|
* @param WC_Customer_Order_CSV_Export_Generator $csv_generator instance |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function add_vendors_to_csv_export_column_data( $order_data, $item, $order, $csv_generator ) { |
|
|
|
|
|
|
68
|
|
|
$vendors = array(); |
|
69
|
|
|
|
|
70
|
|
|
if ( isset( $item['id'] ) && $product_id = $item['id'] ) { |
|
71
|
|
|
if ( $product_vendors = get_product_vendors( $product_id ) ) { |
|
72
|
|
|
foreach ( (array) $product_vendors as $vendor ) { |
|
73
|
|
|
$vendors[] = $vendor->title; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$order_data['vendors'] = implode( ', ', $vendors ); |
|
79
|
|
|
|
|
80
|
|
|
return $order_data; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Filter the individual line item entry to add product id for older versions |
|
86
|
|
|
* of Customer/Order CSV Export |
|
87
|
|
|
* |
|
88
|
|
|
* @access public |
|
89
|
|
|
* @since 1.0.0 |
|
90
|
|
|
* @param array $line_item line item data in key => value format |
|
91
|
|
|
* @param array $item WC order item data |
|
92
|
|
|
* @param WC_Product $product the product |
|
93
|
|
|
* @param WC_Order $order the order |
|
94
|
|
|
* @param WC_Customer_Order_CSV_Export_Generator $csv_generator instance |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function csv_export_line_item_data( $line_item, $item, $product, $order, $csv_generator ) { |
|
98
|
|
|
if ( isset( $csv_generator->order_format ) && ( 'default_one_row_per_item' == $csv_generator->order_format || 'legacy_one_row_per_item' == $csv_generator->order_format ) ) { |
|
99
|
|
|
$line_item['id'] = $product->id; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $line_item; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.