WooCommerce.php
6.15 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php namespace MeowCrew\RoleAndCustomerBasedPricing\Services\Export;
use MeowCrew\RoleAndCustomerBasedPricing\Admin\ProductPage\PricingRulesManager;
use MeowCrew\RoleAndCustomerBasedPricing\Utils\Strings;
use WC_Product;
/**
* Class WooCommerce Export
*/
class WooCommerce {
public $roleBasedRules = array();
public $customerBasedRules = array();
/**
* Export constructor.
*/
public function __construct() {
add_filter( 'woocommerce_product_export_column_names', array( $this, 'addExportColumns' ), 1, 10 );
add_filter( 'woocommerce_product_export_product_default_columns', array( $this, 'addExportColumns' ), 1, 10 );
foreach ( $this->getPluginColumns() as $key => $name ) {
add_filter( 'woocommerce_product_export_product_column_' . $key,
array( $this, 'exportColumnDispatcher' ), 10, 3 );
}
}
public function exportColumnDispatcher( $value, WC_Product $product, $column ) {
$rules = array();
if ( Strings::startsWith( $column, 'rcbp_role_based_' ) ) {
$rules = $this->getPricingRules( $product, 'role' );
} else if ( Strings::startsWith( $column, 'rcbp_customer_based_' ) ) {
$rules = $this->getPricingRules( $product, 'customer' );
}
if ( ! empty( $rules ) ) {
// Pricing type
if ( Strings::endsWith( $column, 'pricing_type' ) ) {
$pricingTypeExportString = array();
foreach ( $rules as $identifier => $rule ) {
$pricingTypeExportString[] = $identifier . ':' . $rule->getPriceType();
}
return implode( ';', $pricingTypeExportString );
}
// Regular price
if ( Strings::endsWith( $column, 'regular_price' ) ) {
$regularPriceExportString = array();
foreach ( $rules as $identifier => $rule ) {
$regularPriceExportString[] = $identifier . ':' . $rule->getRegularPrice();
}
return implode( ';', $regularPriceExportString );
}
// Sale price
if ( Strings::endsWith( $column, 'sale_price' ) ) {
$salePriceExportString = array();
foreach ( $rules as $identifier => $rule ) {
$salePriceExportString[] = $identifier . ':' . $rule->getSalePrice();
}
return implode( ';', $salePriceExportString );
}
// Discount price
if ( Strings::endsWith( $column, 'discount' ) ) {
$discountExportString = array();
foreach ( $rules as $identifier => $rule ) {
$discountExportString[] = $identifier . ':' . $rule->getDiscount();
}
return implode( ';', $discountExportString );
}
// Minimum order quantity
if ( Strings::endsWith( $column, 'minimum_quantity' ) ) {
$minimumExportString = array();
foreach ( $rules as $identifier => $rule ) {
$minimumExportString[] = $identifier . ':' . $rule->getMinimum();
}
return implode( ';', $minimumExportString );
}
// Maximum order quantity
if ( Strings::endsWith( $column, 'maximum_quantity' ) ) {
$maximumExportString = array();
foreach ( $rules as $identifier => $rule ) {
$maximumExportString[] = $identifier . ':' . $rule->getMaximum();
}
return implode( ';', $maximumExportString );
}
// Quantity step
if ( Strings::endsWith( $column, 'quantity_step' ) ) {
$stepOfExportString = array();
foreach ( $rules as $identifier => $rule ) {
$stepOfExportString[] = $identifier . ':' . $rule->getGroupOf();
}
return implode( ';', $stepOfExportString );
}
}
return $value;
}
public function getPricingRules( WC_Product $product, $basedOn = 'role' ) {
if ( 'role' === $basedOn ) {
if ( ! isset( $this->roleBasedRules[ $product->get_id() ] ) ) {
$this->roleBasedRules[ $product->get_id() ] = PricingRulesManager::getProductRoleSpecificPricingRules( $product->get_id() );
}
return $this->roleBasedRules[ $product->get_id() ];
} else if ( 'customer' === $basedOn ) {
if ( ! isset( $this->customerBasedRules[ $product->get_id() ] ) ) {
$this->customerBasedRules[ $product->get_id() ] = PricingRulesManager::getProductCustomerSpecificPricingRules( $product->get_id() );
}
return $this->customerBasedRules[ $product->get_id() ];
}
return array();
}
/**
* Add export columns
*
* @param array $columns
*
* @return array $options
*/
public function addExportColumns( $columns ) {
return array_merge( $columns, $this->getPluginColumns() );
}
public function getPluginColumns() {
$columns = array();
$columns['rcbp_role_based_pricing_type'] = __( 'Role-based pricing type', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_role_based_discount'] = __( 'Role-based discount', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_role_based_regular_price'] = __( 'Role-based regular price', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_role_based_sale_price'] = __( 'Role-based sale price', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_role_based_minimum_quantity'] = __( 'Role-based minimum quantity', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_role_based_maximum_quantity'] = __( 'Role-based maximum quantity', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_role_based_quantity_step'] = __( 'Role-based quantity step', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_pricing_type'] = __( 'Customer-based pricing type', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_discount'] = __( 'Customer-based discount', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_regular_price'] = __( 'Customer-based regular price', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_sale_price'] = __( 'Customer-based sale price', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_minimum_quantity'] = __( 'Customer-based minimum quantity', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_maximum_quantity'] = __( 'Customer-based maximum quantity', 'role-and-customer-based-pricing-for-woocommerce' );
$columns['rcbp_customer_based_quantity_step'] = __( 'Customer-based quantity step', 'role-and-customer-based-pricing-for-woocommerce' );
return $columns;
}
}