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