| 1 |
<?php namespace TierPricingTable\Admin\ProductManagers; |
| 2 |
|
| 3 |
use TierPricingTable\PriceManager; |
| 4 |
use WC_Product; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class ProductManager |
| 8 |
* |
| 9 |
* @package TierPricingTable\Admin\Product |
| 10 |
*/ |
| 11 |
class ProductManager extends ProductManagerAbstract { |
| 12 |
|
| 13 |
/** |
| 14 |
* Register hooks |
| 15 |
*/ |
| 16 |
protected function hooks() { |
| 17 |
|
| 18 |
// Tiered Pricing Product Tab |
| 19 |
add_filter( 'woocommerce_product_data_tabs', array( $this, 'registerTieredPricingProductTab' ), 99, 1 ); |
| 20 |
add_action( 'woocommerce_product_data_panels', array( $this, 'renderTieredPricingTab' ) ); |
| 21 |
|
| 22 |
if ( tpt_fs()->is__premium_only() ) { |
| 23 |
add_action( 'woocommerce_process_product_meta', array( $this, 'saveTieredPricingTab__premium_only' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
// Simple Product |
| 27 |
add_action( 'woocommerce_product_options_pricing', array( $this, 'renderPriceRules' ) ); |
| 28 |
|
| 29 |
// Saving |
| 30 |
add_action( 'woocommerce_process_product_meta', array( $this, 'updatePriceRules' ) ); |
| 31 |
|
| 32 |
if ( tpt_fs()->is__premium_only() ) { |
| 33 |
add_action( 'woocommerce_process_product_meta', array( $this, 'updatePriceRulesType__premium_only' ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Add tiered pricing tab to woocommerce product tabs |
| 39 |
* |
| 40 |
* @param array $productTabs |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function registerTieredPricingProductTab( $productTabs ) { |
| 45 |
|
| 46 |
$productTabs['tiered-pricing-tab'] = array( |
| 47 |
'label' => __( 'Tiered Pricing', 'tier-pricing-table' ), |
| 48 |
'target' => 'tiered-pricing-data', |
| 49 |
'class' => array( 'show_if_simple', 'show_if_variable' ) |
| 50 |
); |
| 51 |
|
| 52 |
return $productTabs; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Render content for the tiered pricing tab |
| 57 |
*/ |
| 58 |
public function renderTieredPricingTab() { |
| 59 |
|
| 60 |
global $post; |
| 61 |
|
| 62 |
$min = PriceManager::getProductQtyMin( $post->ID, 'edit' ); |
| 63 |
|
| 64 |
$desc = __( 'Set if you are selling the product from qty more than 1', |
| 65 |
'tier-pricing-table' ); |
| 66 |
$tip = true; |
| 67 |
|
| 68 |
$customAttrs = array( |
| 69 |
'min' => 1, |
| 70 |
'step' => 1 |
| 71 |
); |
| 72 |
|
| 73 |
if ( $this->licence->isFree() ) { |
| 74 |
$desc = '<span style="color:red">Available only in the premium version</span>'; |
| 75 |
$tip = false; |
| 76 |
$customAttrs['disabled'] = true; |
| 77 |
} |
| 78 |
|
| 79 |
?> |
| 80 |
<div id="tiered-pricing-data" class="panel woocommerce_options_panel"> |
| 81 |
<?php |
| 82 |
woocommerce_wp_text_input( array( |
| 83 |
'id' => '_tiered_pricing_minimum', |
| 84 |
'wrapper_class' => 'show_if_simple show_if_variable', |
| 85 |
'type' => 'number', |
| 86 |
'custom_attributes' => $customAttrs, |
| 87 |
'value' => $min, |
| 88 |
'label' => __( 'Minimum quantity', 'tier-pricing-table' ), |
| 89 |
'description' => $desc, |
| 90 |
'default' => 1, |
| 91 |
'desc_tip' => $tip |
| 92 |
) ); |
| 93 |
?> |
| 94 |
|
| 95 |
<div class="hidden show_if_variable"> |
| 96 |
<?php |
| 97 |
|
| 98 |
$type = PriceManager::getPricingType( $post->ID, 'fixed', 'edit' ); |
| 99 |
|
| 100 |
$this->fileManager->includeTemplate( 'admin/add-price-rules.php', array( |
| 101 |
'price_rules_fixed' => PriceManager::getFixedPriceRules( $post->ID, 'edit' ), |
| 102 |
'price_rules_percentage' => PriceManager::getPercentagePriceRules( $post->ID, 'edit' ), |
| 103 |
'type' => $type, |
| 104 |
'prefix' => 'variable', |
| 105 |
'isFree' => $this->licence->isFree() |
| 106 |
) ); |
| 107 |
|
| 108 |
?> |
| 109 |
</div> |
| 110 |
</div> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Save tiered pricing tab data |
| 116 |
* |
| 117 |
* @param int $productId |
| 118 |
*/ |
| 119 |
public function saveTieredPricingTab__premium_only( $productId ) { |
| 120 |
|
| 121 |
if ( isset( $_POST['_tiered_pricing_minimum'] ) ) { |
| 122 |
$min = intval( $_POST['_tiered_pricing_minimum'] ); |
| 123 |
$min = $min > 0 ? $min : 1; |
| 124 |
PriceManager::updateProductQtyMin( $productId, $min ); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Update price quantity rules for simple product |
| 131 |
* |
| 132 |
* @param int $product_id |
| 133 |
*/ |
| 134 |
public function updatePriceRules( $product_id ) { |
| 135 |
|
| 136 |
$data = $_POST; |
| 137 |
|
| 138 |
$prefix = isset( $data['product-type'] ) && in_array( $data['product-type'], |
| 139 |
array( 'simple', 'variable' ) ) ? sanitize_text_field( $data['product-type'] ) : 'simple'; |
| 140 |
|
| 141 |
$fixedAmounts = isset( $data[ 'tiered_price_fixed_quantity_' . $prefix ] ) ? (array) $data[ 'tiered_price_fixed_quantity_' . $prefix ] : array(); |
| 142 |
$fixedPrices = ! empty( $data[ 'tiered_price_fixed_price_' . $prefix ] ) ? (array) $data[ 'tiered_price_fixed_price_' . $prefix ] : array(); |
| 143 |
|
| 144 |
PriceManager::updateFixedPriceRules( $fixedAmounts, $fixedPrices, $product_id ); |
| 145 |
|
| 146 |
if ( tpt_fs()->is__premium_only() ) { |
| 147 |
$percentageAmounts = isset( $data[ 'tiered_price_percent_quantity_' . $prefix ] ) ? (array) $data[ 'tiered_price_percent_quantity_' . $prefix ] : array(); |
| 148 |
$percentagePrices = ! empty( $data[ 'tiered_price_percent_discount_' . $prefix ] ) ? (array) $data[ 'tiered_price_percent_discount_' . $prefix ] : array(); |
| 149 |
PriceManager::updatePercentagePriceRules( $percentageAmounts, $percentagePrices, $product_id ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Update product pricing type |
| 155 |
* |
| 156 |
* @param int $product_id |
| 157 |
*/ |
| 158 |
public function updatePriceRulesType__premium_only( $product_id ) { |
| 159 |
|
| 160 |
$prefix = isset( $_POST['product-type'] ) && in_array( $_POST['product-type'], |
| 161 |
array( 'simple', 'variable' ) ) ? sanitize_text_field( $_POST['product-type'] ) : 'simple'; |
| 162 |
|
| 163 |
if ( isset( $_POST[ 'tiered_price_rules_type_' . $prefix ] ) ) { |
| 164 |
PriceManager::updatePriceRulesType( $product_id, |
| 165 |
sanitize_text_field( $_POST[ 'tiered_price_rules_type_' . $prefix ] ) ); |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Render inputs for price rules on a simple product |
| 172 |
* |
| 173 |
* @global WC_Product $product_object |
| 174 |
*/ |
| 175 |
public function renderPriceRules() { |
| 176 |
global $product_object; |
| 177 |
|
| 178 |
if ( $product_object instanceof WC_Product ) { |
| 179 |
$type = PriceManager::getPricingType( $product_object->get_id(), 'fixed', 'edit' ); |
| 180 |
|
| 181 |
$this->fileManager->includeTemplate( 'admin/add-price-rules.php', array( |
| 182 |
'price_rules_fixed' => PriceManager::getFixedPriceRules( $product_object->get_id(), 'edit' ), |
| 183 |
'price_rules_percentage' => PriceManager::getPercentagePriceRules( $product_object->get_id(), 'edit' ), |
| 184 |
'type' => $type, |
| 185 |
'prefix' => 'simple', |
| 186 |
'isFree' => $this->licence->isFree() |
| 187 |
) ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|