# tier-pricing-table/8.1.0/src/Addons/ImportExport/WoocommerceImportService.php

Tiered Pricing Table for WooCommerce, version 8.1.0. 115 lines.

- Page: https://pluginprobe.com/plugins/tier-pricing-table/8.1.0/code/src/Addons/ImportExport/WoocommerceImportService.php
- Raw: https://pluginprobe.com/plugins/tier-pricing-table/8.1.0/raw/src/Addons/ImportExport/WoocommerceImportService.php
- Modified: 2026-10-07T10:44:44+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tier-pricing-table/8.1.0/code/src/Addons/ImportExport/WoocommerceImportService.php#L10-L20`.

```php
<?php namespace TierPricingTable\Addons\ImportExport;

use TierPricingTable\TierPricingTablePlugin;
use WC_Product;

class WoocommerceImportService {
	
	/**
	 * Import constructor.
	 */
	public function __construct() {
		add_filter( 'woocommerce_csv_product_import_mapping_options', array( $this, 'addColumnsToImporter' ) );
		add_filter( 'woocommerce_csv_product_import_mapping_default_columns',
			array( $this, 'addColumnToMappingScreen' ) );
		add_filter( 'woocommerce_product_import_pre_insert_product_object', array( $this, 'processImport' ), 10, 2 );
	}
	
	/**
	 * Register the 'Tiered pricing' column in the importer.
	 */
	public function addColumnsToImporter( array $columns ): array {
		
		$columns['tiered_pricing_table'] = array(
			'name'    => __( 'Tiered Pricing', 'tier-pricing-table' ),
			'options' => array(
				'tiered_price_type'       => __( 'Tiered pricing type', 'tier-pricing-table' ),
				'tiered_price_fixed'      => __( 'Fixed pricing rules', 'tier-pricing-table' ),
				'tiered_price_percentage' => __( 'Percentage pricing rules', 'tier-pricing-table' ),
				'tiered_price_minimum'    => __( 'Minimum order quantity', 'tier-pricing-table' ),
			),
		);
		
		return apply_filters( 'tiered_pricing_table/import/woocommerce/import_columns', $columns );
	}
	
	/**
	 * Add automatic mapping support for 'Tiered pricing'.
	 */
	public function addColumnToMappingScreen( array $columns ): array {
		
		$columns['Tiered Pricing — Fixed pricing rules']      = 'tiered_price_fixed';
		$columns['Tiered Pricing — Percentage pricing rules'] = 'tiered_price_percentage';
		$columns['Tiered Pricing — Tiered pricing type']      = 'tiered_price_type';
		$columns['Tiered Pricing — Minimum order quantity']   = 'tiered_price_minimum';
		
		return apply_filters( 'tiered_pricing_table/import/woocommerce/mapping_screen_columns', $columns );
	}
	
	/**
	 * Process the data read from the CSV file.
	 */
	public function processImport( WC_Product $product, array $data ): WC_Product {
		
		if ( isset( $data['tiered_price_fixed'] ) ) {
			
			$fixed = self::decodeExport( $data['tiered_price_fixed'] );
			
			$product->update_meta_data( '_fixed_price_rules', $fixed );
		}
		
		if ( isset( $data['tiered_price_percentage'] ) ) {
			
			$percentage = self::decodeExport( $data['tiered_price_percentage'] );
			
			$product->update_meta_data( '_percentage_price_rules', $percentage );
		}
		
		if ( isset( $data['tiered_price_type'] ) ) {
			
			if ( in_array( $data['tiered_price_type'], array( 'fixed', 'percentage' ) ) ) {
				$product->update_meta_data( '_tiered_price_rules_type', $data['tiered_price_type'] );
			}
		}
		
		if ( isset( $data['tiered_price_minimum'] ) ) {
			
			$minimum = (int) $data['tiered_price_minimum'];
			
			$product->update_meta_data( '_tiered_price_minimum_qty', $minimum );
		}
		
		return $product;
	}
	
	/**
	 * Decode the export string format ("10:90,20:80") into an array of pricing rules.
	 *
	 * Each rule is "quantity:price" (or "quantity=price", which spreadsheets leave alone, while they
	 * turn "20:100" into a time). Anything else, for example a cell a spreadsheet already turned
	 * into "21:40:00", is ignored instead of being imported as a wrong tier.
	 */
	public static function decodeExport( string $data ): array {
		$rules = explode( TierPricingTablePlugin::getRulesSeparator(), $data );
		
		// ":" and "=" always work; a store may export with another separator through the filter.
		$separators = preg_quote( ':=' . TierPricingTablePlugin::getRuleSeparator(), '/' );
		
		$data = array();
		
		foreach ( $rules as $rule ) {
			if ( preg_match( '/^\s*(\d+)\s*[' . $separators . ']\s*(\d+(?:\.\d+)?)\s*$/', $rule, $matches ) ) {
				$data[ intval( $matches[1] ) ] = $matches[2];
			}
		}
		
		$data = array_filter( $data );
		
		$data = array_filter( $data, function ( $itemKey ) {
			return is_numeric( $itemKey ) && $itemKey > 1;
		}, ARRAY_FILTER_USE_KEY );
		
		return ! empty( $data ) ? $data : array();
	}
}

```
