| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Admin\Export; |
| 4 |
|
| 5 |
use TierPricingTable\PriceManager ; |
| 6 |
use WC_Product ; |
| 7 |
/** |
| 8 |
* Class WooCommerce Export |
| 9 |
*/ |
| 10 |
class Woocommerce |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Export constructor. |
| 14 |
*/ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
add_filter( |
| 18 |
'woocommerce_product_export_column_names', |
| 19 |
[ $this, 'addExportColumn' ], |
| 20 |
1, |
| 21 |
10 |
| 22 |
); |
| 23 |
add_filter( |
| 24 |
'woocommerce_product_export_product_default_columns', |
| 25 |
[ $this, 'addExportColumn' ], |
| 26 |
1, |
| 27 |
10 |
| 28 |
); |
| 29 |
add_filter( |
| 30 |
'woocommerce_product_export_product_column_tiered_price_fixed', |
| 31 |
[ $this, 'addExportFixedData' ], |
| 32 |
10, |
| 33 |
2 |
| 34 |
); |
| 35 |
add_filter( |
| 36 |
'woocommerce_product_export_product_column_tiered_price_percentage', |
| 37 |
[ $this, 'addExportPercentageData' ], |
| 38 |
10, |
| 39 |
2 |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register the 'Fixed tier price' column in the exporter. |
| 45 |
* |
| 46 |
* @param array $columns |
| 47 |
* |
| 48 |
* @return array $options |
| 49 |
*/ |
| 50 |
public function addExportColumn( $columns ) |
| 51 |
{ |
| 52 |
$columns['tiered_price_fixed'] = __( 'Fixed Tiered Prices', 'tier-pricing-table' ); |
| 53 |
return $columns; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Provide the data to be exported for one item in the column. |
| 58 |
* |
| 59 |
* @param WC_Product $product |
| 60 |
* @param string $type |
| 61 |
* |
| 62 |
* @return mixed $value |
| 63 |
*/ |
| 64 |
public function addExportData( $product, $type = 'fixed' ) |
| 65 |
{ |
| 66 |
|
| 67 |
if ( $type == 'percentage' ) { |
| 68 |
$tiered_pricing = PriceManager::getPercentagePriceRules( $product->get_id() ); |
| 69 |
} else { |
| 70 |
$tiered_pricing = PriceManager::getFixedPriceRules( $product->get_id() ); |
| 71 |
} |
| 72 |
|
| 73 |
$str = ''; |
| 74 |
foreach ( $tiered_pricing as $quantity => $price ) { |
| 75 |
$str .= $quantity . ":" . $price . ","; |
| 76 |
} |
| 77 |
return ( mb_strlen( $str ) > 0 ? trim( $str, ',' ) : null ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Export fixed pricing rules |
| 82 |
* |
| 83 |
* @param mixed $value |
| 84 |
* @param WC_product $product |
| 85 |
* |
| 86 |
* @return mixed |
| 87 |
*/ |
| 88 |
public function addExportFixedData( $value, $product ) |
| 89 |
{ |
| 90 |
return $this->addExportData( $product, 'fixed' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Export percentage pricing rules |
| 95 |
* |
| 96 |
* @param mixed $value |
| 97 |
* @param WC_product $product |
| 98 |
* |
| 99 |
* @return mixed |
| 100 |
*/ |
| 101 |
public function addExportPercentageData( $value, $product ) |
| 102 |
{ |
| 103 |
return $this->addExportData( $product, 'percentage' ); |
| 104 |
} |
| 105 |
|
| 106 |
} |