PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
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 2.3.4 All 90 releases
tier-pricing-table / src / Addons / ImportExport / WoocommerceImportService.php

WoocommerceImportService.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/ImportExport/WoocommerceImportService.php

112 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\ImportExport;
2
3 use TierPricingTable\TierPricingTablePlugin;
4 use WC_Product;
5
6 class WoocommerceImportService {
7
8 /**
9 * Import constructor.
10 */
11 public function __construct() {
12 add_filter( 'woocommerce_csv_product_import_mapping_options', array( $this, 'addColumnsToImporter' ) );
13 add_filter( 'woocommerce_csv_product_import_mapping_default_columns',
14 array( $this, 'addColumnToMappingScreen' ) );
15 add_filter( 'woocommerce_product_import_pre_insert_product_object', array( $this, 'processImport' ), 10, 2 );
16 }
17
18 /**
19 * Register the 'Tiered pricing' column in the importer.
20 */
21 public function addColumnsToImporter( array $columns ): array {
22
23 $columns['tiered_pricing_table'] = array(
24 'name' => __( 'Tiered Pricing', 'tier-pricing-table' ),
25 'options' => array(
26 'tiered_price_type' => __( 'Tiered pricing type', 'tier-pricing-table' ),
27 'tiered_price_fixed' => __( 'Fixed pricing rules', 'tier-pricing-table' ),
28 'tiered_price_percentage' => __( 'Percentage pricing rules', 'tier-pricing-table' ),
29 'tiered_price_minimum' => __( 'Minimum order quantity', 'tier-pricing-table' ),
30 ),
31 );
32
33 return apply_filters( 'tiered_pricing_table/import/woocommerce/import_columns', $columns );
34 }
35
36 /**
37 * Add automatic mapping support for 'Tiered pricing'.
38 */
39 public function addColumnToMappingScreen( array $columns ): array {
40
41 $columns['Tiered Pricing — Fixed pricing rules'] = 'tiered_price_fixed';
42 $columns['Tiered Pricing — Percentage pricing rules'] = 'tiered_price_percentage';
43 $columns['Tiered Pricing — Tiered pricing type'] = 'tiered_price_type';
44 $columns['Tiered Pricing — Minimum order quantity'] = 'tiered_price_minimum';
45
46 return apply_filters( 'tiered_pricing_table/import/woocommerce/mapping_screen_columns', $columns );
47 }
48
49 /**
50 * Process the data read from the CSV file.
51 */
52 public function processImport( WC_Product $product, array $data ): WC_Product {
53
54 if ( isset( $data['tiered_price_fixed'] ) ) {
55
56 $fixed = self::decodeExport( $data['tiered_price_fixed'] );
57
58 $product->update_meta_data( '_fixed_price_rules', $fixed );
59 }
60
61 if ( isset( $data['tiered_price_percentage'] ) ) {
62
63 $percentage = self::decodeExport( $data['tiered_price_percentage'] );
64
65 $product->update_meta_data( '_percentage_price_rules', $percentage );
66 }
67
68 if ( isset( $data['tiered_price_type'] ) ) {
69
70 if ( in_array( $data['tiered_price_type'], array( 'fixed', 'percentage' ) ) ) {
71 $product->update_meta_data( '_tiered_price_rules_type', $data['tiered_price_type'] );
72 }
73 }
74
75 if ( isset( $data['tiered_price_minimum'] ) ) {
76
77 $minimum = (int) $data['tiered_price_minimum'];
78
79 $product->update_meta_data( '_tiered_price_minimum_qty', $minimum );
80 }
81
82 return $product;
83 }
84
85 /**
86 * Decode export string format to array of pricing rules
87 */
88 public static function decodeExport( string $data ): array {
89 $rules = explode( TierPricingTablePlugin::getRulesSeparator(), $data );
90
91 $data = array();
92
93 if ( $rules ) {
94 foreach ( $rules as $rule ) {
95 $rule = explode( ':', $rule );
96
97 if ( isset( $rule[0] ) && isset( $rule[1] ) ) {
98 $data[ intval( $rule[0] ) ] = $rule[1];
99 }
100 }
101 }
102
103 $data = array_filter( $data );
104
105 $data = array_filter( $data, function ( $itemKey ) {
106 return is_numeric( $itemKey ) && $itemKey > 1;
107 }, ARRAY_FILTER_USE_KEY );
108
109 return ! empty( $data ) ? $data : array();
110 }
111 }
112