| 1 |
<?php namespace TierPricingTable\Admin\ProductManagers; |
| 2 |
|
| 3 |
use TierPricingTable\PriceManager; |
| 4 |
use WP_Post; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class VariationProduct |
| 8 |
* |
| 9 |
* @package TierPricingTable\Admin\Product |
| 10 |
*/ |
| 11 |
class VariationProductManager extends ProductManagerAbstract { |
| 12 |
|
| 13 |
/** |
| 14 |
* Register hooks |
| 15 |
*/ |
| 16 |
protected function hooks() { |
| 17 |
add_action( 'woocommerce_variation_options_pricing', [ $this, 'renderPriceRules' ], 10, 3 ); |
| 18 |
add_action( 'woocommerce_save_product_variation', [ $this, 'updatePriceRules' ], 10, 3 ); |
| 19 |
|
| 20 |
if ( tpt_fs()->is__premium_only() ) { |
| 21 |
add_action( 'woocommerce_save_product_variation', [ $this, 'updatePriceRulesType__premium_only' ], 10, 3 ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Update price quantity rules for variation product |
| 27 |
* |
| 28 |
* @param int $variation_id |
| 29 |
* @param int $loop |
| 30 |
*/ |
| 31 |
public function updatePriceRules( $variation_id, $loop ) { |
| 32 |
|
| 33 |
if ( isset( $_POST['tiered_price_fixed_quantity'][ $loop ] ) ) { |
| 34 |
$amounts = $_POST['tiered_price_fixed_quantity'][ $loop ]; |
| 35 |
$prices = ! empty( $_POST['tiered_price_fixed_price'][ $loop ] ) ? $_POST['tiered_price_fixed_price'][ $loop ] : []; |
| 36 |
|
| 37 |
PriceManager::updateFixedPriceRules( $amounts, $prices, $variation_id ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( tpt_fs()->is__premium_only() ) { |
| 41 |
if ( isset( $_POST['tiered_price_percent_quantity'][ $loop ] ) ) { |
| 42 |
$amounts = $_POST['tiered_price_percent_quantity'][ $loop ]; |
| 43 |
$prices = ! empty( $_POST['tiered_price_percent_discount'][ $loop ] ) ? $_POST['tiered_price_percent_discount'][ $loop ] : []; |
| 44 |
|
| 45 |
PriceManager::updatePercentagePriceRules( $amounts, $prices, $variation_id ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
if ( isset( $_POST['_tiered_pricing_minimum'][ $loop ] ) ) { |
| 50 |
$min = intval( $_POST['_tiered_pricing_minimum'][ $loop ] ); |
| 51 |
$min = $min > 0 ? $min : 1; |
| 52 |
|
| 53 |
PriceManager::updateProductQtyMin( $variation_id, $min ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Update product pricing type |
| 61 |
* |
| 62 |
* @param int $variation_id |
| 63 |
* @param int $loop |
| 64 |
*/ |
| 65 |
public function updatePriceRulesType__premium_only( $variation_id, $loop ) { |
| 66 |
if ( ! empty( $_POST['tiered_price_rules_type'][ $loop ] ) ) { |
| 67 |
PriceManager::updatePriceRulesType( $variation_id, $_POST['tiered_price_rules_type'][ $loop ] ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Render inputs for price rules on variation |
| 73 |
* |
| 74 |
* @param int $loop |
| 75 |
* @param array $variation_data |
| 76 |
* @param WP_Post $variation |
| 77 |
*/ |
| 78 |
public function renderPriceRules( $loop, $variation_data, $variation ) { |
| 79 |
$type = PriceManager::getPricingType( $variation->ID ); |
| 80 |
|
| 81 |
$this->fileManager->includeTemplate( 'admin/add-price-rules-variation.php', [ |
| 82 |
'price_rules_fixed' => PriceManager::getFixedPriceRules( $variation->ID, 'edit' ), |
| 83 |
'price_rules_percentage' => PriceManager::getPercentagePriceRules( $variation->ID, 'edit' ), |
| 84 |
'i' => $loop, |
| 85 |
'minimum' => PriceManager::getProductQtyMin( $variation->ID, 'edit' ), |
| 86 |
'variation_data' => $variation_data, |
| 87 |
'type' => $type, |
| 88 |
'isFree' => $this->licence->isFree() |
| 89 |
] ); |
| 90 |
} |
| 91 |
} |