| 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 the export string format ("10:90,20:80") into an array of pricing rules. |
| 87 |
* |
| 88 |
* Each rule is "quantity:price" (or "quantity=price", which spreadsheets leave alone, while they |
| 89 |
* turn "20:100" into a time). Anything else, for example a cell a spreadsheet already turned |
| 90 |
* into "21:40:00", is ignored instead of being imported as a wrong tier. |
| 91 |
*/ |
| 92 |
public static function decodeExport( string $data ): array { |
| 93 |
$rules = explode( TierPricingTablePlugin::getRulesSeparator(), $data ); |
| 94 |
|
| 95 |
// ":" and "=" always work; a store may export with another separator through the filter. |
| 96 |
$separators = preg_quote( ':=' . TierPricingTablePlugin::getRuleSeparator(), '/' ); |
| 97 |
|
| 98 |
$data = array(); |
| 99 |
|
| 100 |
foreach ( $rules as $rule ) { |
| 101 |
if ( preg_match( '/^\s*(\d+)\s*[' . $separators . ']\s*(\d+(?:\.\d+)?)\s*$/', $rule, $matches ) ) { |
| 102 |
$data[ intval( $matches[1] ) ] = $matches[2]; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$data = array_filter( $data ); |
| 107 |
|
| 108 |
$data = array_filter( $data, function ( $itemKey ) { |
| 109 |
return is_numeric( $itemKey ) && $itemKey > 1; |
| 110 |
}, ARRAY_FILTER_USE_KEY ); |
| 111 |
|
| 112 |
return ! empty( $data ) ? $data : array(); |
| 113 |
} |
| 114 |
} |
| 115 |
|