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 / Admin / Import / Woocommerce.php

Woocommerce.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Admin/Import/Woocommerce.php

143 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Admin\Import;
4
5 use TierPricingTable\TierPricingTablePlugin ;
6 use WC_Product ;
7 class Woocommerce
8 {
9 /**
10 * Import constructor.
11 */
12 public function __construct()
13 {
14 add_filter( 'woocommerce_csv_product_import_mapping_options', array( $this, 'addColumnsToImporter' ) );
15 add_filter( 'woocommerce_csv_product_import_mapping_default_columns', array( $this, 'addColumnToMappingScreen' ) );
16 add_filter(
17 'woocommerce_product_import_pre_insert_product_object',
18 array( $this, 'processImport' ),
19 10,
20 2
21 );
22 }
23
24 /**
25 * Register the 'Tiered pricing' column in the importer.
26 *
27 * @param array $options
28 *
29 * @return array $options
30 */
31 public function addColumnsToImporter( $options )
32 {
33 $options['tiered_price_type'] = __( 'Tiered pricing type', 'tier-pricing-table' );
34 $options['tiered_price_minimum'] = __( 'Tiered pricing minimum product quantity', 'tier-pricing-table' );
35 $options['tiered_price_fixed'] = __( 'Fixed Tiered prices', 'tier-pricing-table' );
36 $options['tiered_price_percentage'] = __( 'Percentage Tiered Prices', 'tier-pricing-table' );
37 return $options;
38 }
39
40 /**
41 * Add automatic mapping support for 'Tiered pricing'.
42 *
43 * @param array $columns
44 *
45 * @return array $columns
46 */
47 public function addColumnToMappingScreen( $columns )
48 {
49 $columns[__( 'Fixed Tiered prices', 'tier-pricing-table' )] = 'tiered_price_fixed';
50 $columns[__( 'Percentage Tiered prices', 'tier-pricing-table' )] = 'tiered_price_percentage';
51 $columns[__( 'Tiered pricing type', 'tier-pricing-table' )] = 'tiered_price_type';
52 $columns[__( 'Tiered pricing minimum product quantity', 'tier-pricing-table' )] = 'tiered_price_minimum';
53 return $columns;
54 }
55
56 /**
57 * Process the data read from the CSV file.
58 *
59 * @param WC_Product $product - Product being imported or updated.
60 * @param array $data - CSV data read for the product.
61 *
62 * @return WC_Product $object
63 */
64 public function processImport( $product, $data )
65 {
66
67 if ( isset( $data['tiered_price_fixed'] ) ) {
68 $fixed = $this->decodeExport( $data['tiered_price_fixed'] );
69 $product->update_meta_data( '_fixed_price_rules', $fixed );
70 }
71
72
73 if ( isset( $data['tiered_price_percentage'] ) ) {
74 $percentage = $this->decodeExport( $data['tiered_price_percentage'] );
75 $product->update_meta_data( '_percentage_price_rules', $percentage );
76 }
77
78 if ( isset( $data['tiered_price_type'] ) ) {
79 if ( in_array( $data['tiered_price_type'], array( 'fixed', 'percentage' ) ) ) {
80 $product->update_meta_data( '_tiered_price_rules_type', $data['tiered_price_type'] );
81 }
82 }
83
84 if ( isset( $data['tiered_price_minimum'] ) ) {
85 $minimum = (int) $data['tiered_price_minimum'];
86 $product->update_meta_data( '_tiered_price_minimum_qty', $minimum );
87 }
88
89 foreach ( wp_roles()->roles as $WPRole => $role_data ) {
90
91 if ( isset( $data[$WPRole . '_tiered_price_fixed'] ) ) {
92 $fixed = $this->decodeExport( $data[$WPRole . '_tiered_price_fixed'] );
93 $product->update_meta_data( '_' . $WPRole . '_fixed_price_rules', $fixed );
94 }
95
96
97 if ( isset( $data[$WPRole . '_tiered_price_percentage'] ) ) {
98 $percentage = $this->decodeExport( $data[$WPRole . '_tiered_price_percentage'] );
99 $product->update_meta_data( '_' . $WPRole . '_percentage_price_rules', $percentage );
100 }
101
102 if ( isset( $data[$WPRole . '_tiered_price_type'] ) ) {
103 if ( in_array( $data[$WPRole . '_tiered_price_type'], array( 'fixed', 'percentage' ) ) ) {
104 $product->update_meta_data( '_' . $WPRole . '_tiered_price_rules_type', $data[$WPRole . '_tiered_price_type'] );
105 }
106 }
107
108 if ( isset( $data[$WPRole . '_tiered_price_minimum'] ) ) {
109 $minimum = (int) $data[$WPRole . '_tiered_price_minimum'];
110 $product->update_meta_data( '_' . $WPRole . '_tiered_price_minimum_qty', $minimum );
111 }
112
113 }
114 return $product;
115 }
116
117 /**
118 * Decode export file format to array
119 *
120 * @param string $data
121 *
122 * @return array
123 */
124 protected function decodeExport( $data )
125 {
126 $rules = explode( TierPricingTablePlugin::getRulesSeparator(), $data );
127 $data = array();
128 if ( $rules ) {
129 foreach ( $rules as $rule ) {
130 $rule = explode( ':', $rule );
131 if ( isset( $rule[0] ) && isset( $rule[1] ) ) {
132 $data[intval( $rule[0] )] = $rule[1];
133 }
134 }
135 }
136 $data = array_filter( $data );
137 $data = array_filter( $data, function ( $itemKey ) {
138 return is_numeric( $itemKey ) && $itemKey > 1;
139 }, ARRAY_FILTER_USE_KEY );
140 return ( !empty($data) ? $data : array() );
141 }
142
143 }