__( '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(); } }