PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.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 2.3.3 All 91 releases
tier-pricing-table / src / Addons / RoleBasedPricing / Import / RoleBasedPricingImport.php

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

159 lines 5.9 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\Services\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 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
19
20 $roleBasedRule = new RoleBasedPricingRule( $product->get_id(), $WPRole );
21 $needUpdate = false;
22
23 if ( isset( $data[ $WPRole . '_tiered_price_pricing_type' ] ) ) {
24 $roleBasedRule->setPricingType( $data[ $WPRole . '_tiered_price_pricing_type' ] );
25 $needUpdate = true;
26 }
27
28 if ( isset( $data[ $WPRole . '_tiered_price_regular_price' ] ) ) {
29 $regularPrice = floatval( $data[ $WPRole . '_tiered_price_regular_price' ] );
30
31 $roleBasedRule->setRegularPrice( $regularPrice );
32 $needUpdate = true;
33 }
34
35 if ( isset( $data[ $WPRole . '_tiered_price_sale_price' ] ) ) {
36
37 $salePrice = floatval( $data[ $WPRole . '_tiered_price_sale_price' ] );
38
39 $roleBasedRule->setSalePrice( $salePrice );
40 $needUpdate = true;
41 }
42
43 if ( isset( $data[ $WPRole . '_tiered_price_discount' ] ) ) {
44
45 $discount = floatval( $data[ $WPRole . '_tiered_price_discount' ] );
46
47 if ( $discount ) {
48 $roleBasedRule->setDiscount( min( 100, $discount ) );
49 }
50
51 $needUpdate = true;
52 }
53
54 if ( isset( $data[ $WPRole . '_tiered_price_fixed' ] ) ) {
55
56 $fixedPricingRules = WoocommerceImportService::decodeExport( $data[ $WPRole . '_tiered_price_fixed' ] );
57 $roleBasedRule->setFixedTieredPricingRules( $fixedPricingRules );
58 $needUpdate = true;
59 }
60
61 if ( isset( $data[ $WPRole . '_tiered_price_percentage' ] ) ) {
62
63 $percentagePricingRules = WoocommerceImportService::decodeExport( $data[ $WPRole . '_tiered_price_percentage' ] );
64
65 $roleBasedRule->setPercentageTieredPricingRules( $percentagePricingRules );
66 $needUpdate = true;
67 }
68
69 if ( isset( $data[ $WPRole . '_tiered_price_type' ] ) ) {
70
71 $tieredPricingType = $data[ $WPRole . '_tiered_price_type' ];
72
73 if ( in_array( $tieredPricingType, array( 'fixed', 'percentage' ) ) ) {
74 $roleBasedRule->setTieredPricingType( $tieredPricingType );
75 }
76 }
77
78 if ( isset( $data[ $WPRole . '_tiered_price_minimum' ] ) ) {
79
80 $minimum = (int) $data[ $WPRole . '_tiered_price_minimum' ];
81
82 $roleBasedRule->setMinimumOrderQuantity( $minimum );
83 $needUpdate = true;
84 }
85
86 if ( $needUpdate ) {
87 try {
88 $roleBasedRule->save();
89 } catch ( \Exception $e ) {
90 wc_get_logger()->error( 'Error saving role-based pricing rule for product ID ' . $product->get_id() . ' and role ' . $WPRole . ': ' . $e->getMessage(),
91 array( 'source' => 'tiered-pricing-table' ) );
92 }
93 }
94 }
95
96 return $product;
97 }
98
99 public function addColumnsToImporter( $columns ): array {
100
101 global $wp_roles;
102
103 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
104 $roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole;
105
106 $columns[ 'tpt_' . $WPRole ] = array(
107 'name' => __( 'Tiered Pricing', 'tier-pricing-table' ) . ': ' . $roleName,
108 'options' => array(
109 $WPRole . '_tiered_price_pricing_type' => $roleName . ': ' . __( 'Regular pricing type',
110 'tier-pricing-table' ),
111 $WPRole . '_tiered_price_regular_price' => $roleName . ': ' . __( 'Regular price',
112 'tier-pricing-table' ),
113 $WPRole . '_tiered_price_sale_price' => $roleName . ': ' . __( 'Sale price',
114 'tier-pricing-table' ),
115 $WPRole . '_tiered_price_discount' => $roleName . ': ' . __( 'Percentage discount',
116 'tier-pricing-table' ),
117 $WPRole . '_tiered_price_type' => $roleName . ': ' . __( 'Tiered pricing type',
118 'tier-pricing-table' ),
119 $WPRole . '_tiered_price_minimum' => $roleName . ': ' . __( 'Minimum order quantity',
120 'tier-pricing-table' ),
121 $WPRole . '_tiered_price_fixed' => $roleName . ': ' . __( 'Fixed pricing rules',
122 'tier-pricing-table' ),
123 $WPRole . '_tiered_price_percentage' => $roleName . ': ' . __( 'Percentage pricing rules',
124 'tier-pricing-table' ),
125 ),
126 );
127 }
128
129 return $columns;
130 }
131
132 public function addColumnsToMapper( array $columns ): array {
133
134 global $wp_roles;
135
136 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
137 $roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole;
138
139 $columns[ 'Tiered Pricing — [' . $roleName . '] Regular pricing type' ] = $WPRole . '_tiered_price_pricing_type';
140
141 $columns[ 'Tiered Pricing — [' . $roleName . '] Regular price' ] = $WPRole . '_tiered_price_regular_price';
142
143 $columns[ 'Tiered Pricing — [' . $roleName . '] Sale price' ] = $WPRole . '_tiered_price_sale_price';
144
145 $columns[ 'Tiered Pricing — [' . $roleName . '] Percentage discount' ] = $WPRole . '_tiered_price_discount';
146
147 $columns[ 'Tiered Pricing — [' . $roleName . '] Fixed pricing rules' ] = $WPRole . '_tiered_price_fixed';
148
149 $columns[ 'Tiered Pricing — [' . $roleName . '] Percentage pricing rules' ] = $WPRole . '_tiered_price_percentage';
150
151 $columns[ 'Tiered Pricing — [' . $roleName . '] Tiered pricing type' ] = $WPRole . '_tiered_price_type';
152
153 $columns[ 'Tiered Pricing — [' . $roleName . '] Minimum order quantity' ] = $WPRole . '_tiered_price_minimum';
154 }
155
156 return $columns;
157 }
158 }
159