PluginProbe
Tiered Pricing Table for WooCommerce / 8.1.0
Tiered Pricing Table for WooCommerce v8.1.0
8.1.0 8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 All 92 releases
tier-pricing-table / src / Addons / RoleBasedPricing / Import / RoleBasedPricingImport.php

RoleBasedPricingImport.php in Tiered Pricing Table for WooCommerce 8.1.0, at src/Addons/RoleBasedPricing/Import/RoleBasedPricingImport.php

178 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\RoleBasedPricing\Import;
2
3 use TierPricingTable\Addons\RoleBasedPricing\RoleBasedPricingRule;
4 use TierPricingTable\Addons\ImportExport\WoocommerceImportService;
5 use WC_Product;
6
7 class RoleBasedPricingImport {
8
9 public function __construct() {
10 add_filter( 'tiered_pricing_table/import/woocommerce/mapping_screen_columns',
11 array( $this, 'addColumnsToMapper' ) );
12 add_filter( 'tiered_pricing_table/import/woocommerce/import_columns', array( $this, 'addColumnsToImporter' ) );
13 add_filter( 'woocommerce_product_import_pre_insert_product_object', array( $this, 'processImport' ), 10, 2 );
14 }
15
16 public function processImport( WC_Product $product, array $data ): WC_Product {
17
18 $disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) );
19
20 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
21
22 if ( in_array( $WPRole, $disabled_roles, true ) ) {
23 continue;
24 }
25
26 $roleBasedRule = new RoleBasedPricingRule( $product->get_id(), $WPRole );
27 $needUpdate = false;
28
29 if ( isset( $data[ $WPRole . '_tiered_price_pricing_type' ] ) ) {
30 $roleBasedRule->setPricingType( $data[ $WPRole . '_tiered_price_pricing_type' ] );
31 $needUpdate = true;
32 }
33
34 if ( isset( $data[ $WPRole . '_tiered_price_regular_price' ] ) ) {
35 $regularPrice = floatval( $data[ $WPRole . '_tiered_price_regular_price' ] );
36
37 $roleBasedRule->setRegularPrice( $regularPrice );
38 $needUpdate = true;
39 }
40
41 if ( isset( $data[ $WPRole . '_tiered_price_sale_price' ] ) ) {
42
43 $salePrice = floatval( $data[ $WPRole . '_tiered_price_sale_price' ] );
44
45 $roleBasedRule->setSalePrice( $salePrice );
46 $needUpdate = true;
47 }
48
49 if ( isset( $data[ $WPRole . '_tiered_price_discount' ] ) ) {
50
51 $discount = floatval( $data[ $WPRole . '_tiered_price_discount' ] );
52
53 if ( $discount ) {
54 $roleBasedRule->setDiscount( min( 100, $discount ) );
55 }
56
57 $needUpdate = true;
58 }
59
60 if ( isset( $data[ $WPRole . '_tiered_price_fixed' ] ) ) {
61
62 $fixedPricingRules = WoocommerceImportService::decodeExport( $data[ $WPRole . '_tiered_price_fixed' ] );
63 $roleBasedRule->setFixedTieredPricingRules( $fixedPricingRules );
64 $needUpdate = true;
65 }
66
67 if ( isset( $data[ $WPRole . '_tiered_price_percentage' ] ) ) {
68
69 $percentagePricingRules = WoocommerceImportService::decodeExport( $data[ $WPRole . '_tiered_price_percentage' ] );
70
71 $roleBasedRule->setPercentageTieredPricingRules( $percentagePricingRules );
72 $needUpdate = true;
73 }
74
75 if ( isset( $data[ $WPRole . '_tiered_price_type' ] ) ) {
76
77 $tieredPricingType = $data[ $WPRole . '_tiered_price_type' ];
78
79 if ( in_array( $tieredPricingType, array( 'fixed', 'percentage' ) ) ) {
80 $roleBasedRule->setTieredPricingType( $tieredPricingType );
81 }
82 }
83
84 if ( isset( $data[ $WPRole . '_tiered_price_minimum' ] ) ) {
85
86 $minimum = (int) $data[ $WPRole . '_tiered_price_minimum' ];
87
88 $roleBasedRule->setMinimumOrderQuantity( $minimum );
89 $needUpdate = true;
90 }
91
92 if ( $needUpdate ) {
93 try {
94 $roleBasedRule->save();
95 } catch ( \Exception $e ) {
96 wc_get_logger()->error( 'Error saving role-based pricing rule for product ID ' . $product->get_id() . ' and role ' . $WPRole . ': ' . $e->getMessage(),
97 array( 'source' => 'tiered-pricing-table' ) );
98 }
99 }
100 }
101
102 return $product;
103 }
104
105 public function addColumnsToImporter( $columns ): array {
106
107 global $wp_roles;
108 $disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) );
109
110 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
111 if ( in_array( $WPRole, $disabled_roles, true ) ) {
112 continue;
113 }
114
115 $roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole;
116
117 $columns[ 'tpt_' . $WPRole ] = array(
118 'name' => __( 'Tiered Pricing', 'tier-pricing-table' ) . ': ' . $roleName,
119 'options' => array(
120 $WPRole . '_tiered_price_pricing_type' => $roleName . ': ' . __( 'Regular pricing type',
121 'tier-pricing-table' ),
122 $WPRole . '_tiered_price_regular_price' => $roleName . ': ' . __( 'Regular price',
123 'tier-pricing-table' ),
124 $WPRole . '_tiered_price_sale_price' => $roleName . ': ' . __( 'Sale price',
125 'tier-pricing-table' ),
126 $WPRole . '_tiered_price_discount' => $roleName . ': ' . __( 'Percentage discount',
127 'tier-pricing-table' ),
128 $WPRole . '_tiered_price_type' => $roleName . ': ' . __( 'Tiered pricing type',
129 'tier-pricing-table' ),
130 $WPRole . '_tiered_price_minimum' => $roleName . ': ' . __( 'Minimum order quantity',
131 'tier-pricing-table' ),
132 $WPRole . '_tiered_price_fixed' => $roleName . ': ' . __( 'Fixed pricing rules',
133 'tier-pricing-table' ),
134 $WPRole . '_tiered_price_percentage' => $roleName . ': ' . __( 'Percentage pricing rules',
135 'tier-pricing-table' ),
136 ),
137 );
138 }
139
140 return $columns;
141 }
142
143 public function addColumnsToMapper( array $columns ): array {
144
145 global $wp_roles;
146 $disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) );
147
148 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
149 if ( in_array( $WPRole, $disabled_roles, true ) ) {
150 continue;
151 }
152
153 $roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole;
154
155 $labels = array(
156 'Regular pricing type' => $WPRole . '_tiered_price_pricing_type',
157 'Regular price' => $WPRole . '_tiered_price_regular_price',
158 'Sale price' => $WPRole . '_tiered_price_sale_price',
159 'Percentage discount' => $WPRole . '_tiered_price_discount',
160 'Fixed pricing rules' => $WPRole . '_tiered_price_fixed',
161 'Percentage pricing rules' => $WPRole . '_tiered_price_percentage',
162 'Tiered pricing type' => $WPRole . '_tiered_price_type',
163 'Minimum order quantity' => $WPRole . '_tiered_price_minimum',
164 );
165
166 foreach ( $labels as $label => $column ) {
167 // The exporter writes "Tiered Pricing — [Role] Label". Files exported before 8.0.3 / 10.0.3 carried
168 // two (three for the pricing type) spaces before the bracket; they must keep mapping automatically.
169 foreach ( array( ' ', ' ', ' ' ) as $spacing ) {
170 $columns[ 'Tiered Pricing —' . $spacing . '[' . $roleName . '] ' . $label ] = $column;
171 }
172 }
173 }
174
175 return $columns;
176 }
177 }
178