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