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