| 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 sell the product only from a specific quantity that is more than 1', 'tier-pricing-table' ); |
| 58 |
$tip = true; |
| 59 |
$customAttrs = array( |
| 60 |
'min' => 1, |
| 61 |
'step' => 1, |
| 62 |
); |
| 63 |
|
| 64 |
if ( !tpt_fs()->is_premium() ) { |
| 65 |
$desc = '<span style="color:red"> ' . __( 'Available only in the premium version', 'tier-pricing-table' ) . ' </span> <a target="_blank" |
| 66 |
href="' . esc_url( tpt_fs_activation_url() ) . '">' . __( 'Upgrade', 'tier-pricing-table' ) . '</a>'; |
| 67 |
$tip = false; |
| 68 |
$customAttrs['disabled'] = true; |
| 69 |
} |
| 70 |
|
| 71 |
?> |
| 72 |
<div id="tiered-pricing-data" class="panel woocommerce_options_panel"> |
| 73 |
<?php |
| 74 |
do_action( 'tiered_pricing_table/admin/pricing_tab_begin', $post->ID ); |
| 75 |
woocommerce_wp_text_input( array( |
| 76 |
'id' => '_tiered_pricing_minimum', |
| 77 |
'wrapper_class' => 'show_if_simple show_if_variable', |
| 78 |
'type' => 'number', |
| 79 |
'custom_attributes' => $customAttrs, |
| 80 |
'value' => $min, |
| 81 |
'label' => __( 'Minimum quantity', 'tier-pricing-table' ), |
| 82 |
'description' => $desc, |
| 83 |
'default' => 1, |
| 84 |
'desc_tip' => $tip, |
| 85 |
) ); |
| 86 |
?> |
| 87 |
|
| 88 |
<div class="hidden show_if_variable"> |
| 89 |
<?php |
| 90 |
$type = PriceManager::getPricingType( $post->ID, 'fixed', 'edit' ); |
| 91 |
$this->getContainer()->getFileManager()->includeTemplate( 'admin/add-price-rules.php', array( |
| 92 |
'price_rules_fixed' => PriceManager::getFixedPriceRules( $post->ID, 'edit' ), |
| 93 |
'price_rules_percentage' => PriceManager::getPercentagePriceRules( $post->ID, 'edit' ), |
| 94 |
'type' => $type, |
| 95 |
'prefix' => 'variable', |
| 96 |
'isFree' => !tpt_fs()->is_premium(), |
| 97 |
) ); |
| 98 |
?> |
| 99 |
</div> |
| 100 |
|
| 101 |
<?php |
| 102 |
woocommerce_wp_select( array( |
| 103 |
'id' => '_tiered_pricing_template', |
| 104 |
'value' => self::getProductTemplate( $post->ID ), |
| 105 |
'options' => array( |
| 106 |
'default' => __( 'Default', 'tier-pricing-table' ), |
| 107 |
'table' => __( 'Table', 'tier-pricing-table' ), |
| 108 |
'blocks' => __( 'Blocks', 'tier-pricing-table' ), |
| 109 |
'options' => __( 'Options', 'tier-pricing-table' ), |
| 110 |
'dropdown' => __( 'Dropdown', 'tier-pricing-table' ), |
| 111 |
'tooltip' => __( 'Tooltip', 'tier-pricing-table' ), |
| 112 |
), |
| 113 |
'label' => __( 'Layout', 'tier-pricing-table' ), |
| 114 |
'description' => __( 'Specify a tiered pricing layout for the product. Leave the default to use the default layout from the settings', 'tier-pricing-table' ), |
| 115 |
'default' => 'default', |
| 116 |
'desc_tip' => true, |
| 117 |
) ); |
| 118 |
?> |
| 119 |
|
| 120 |
<?php |
| 121 |
do_action( 'tiered_pricing_table/admin/before_advance_product_options', $post->ID ); |
| 122 |
?> |
| 123 |
|
| 124 |
<?php |
| 125 |
|
| 126 |
if ( apply_filters( 'tiered_pricing_table/admin/has_advance_product_options', false, $post ) ) { |
| 127 |
?> |
| 128 |
<div class="tiered_pricing_tab_product_advance_options"> |
| 129 |
<div class="tiered_pricing_tab_product_advance_options__header"> |
| 130 |
<h4> |
| 131 |
<?php |
| 132 |
esc_html_e( 'Advanced settings', 'tier-pricing-table' ); |
| 133 |
?> |
| 134 |
</h4> |
| 135 |
<div> |
| 136 |
<span class="tiered_pricing_arrow_down">â–¼</span> |
| 137 |
<span class="tiered_pricing_arrow_up">â–²</span> |
| 138 |
</div> |
| 139 |
</div> |
| 140 |
|
| 141 |
<div class="tiered_pricing_tab_product_advance_options__content"> |
| 142 |
<?php |
| 143 |
do_action( 'tiered_pricing_table/admin/advance_product_options', $post->ID ); |
| 144 |
?> |
| 145 |
</div> |
| 146 |
</div> |
| 147 |
<?php |
| 148 |
} |
| 149 |
|
| 150 |
?> |
| 151 |
|
| 152 |
<?php |
| 153 |
do_action( 'tiered_pricing_table/admin/pricing_tab_end', $post->ID ); |
| 154 |
?> |
| 155 |
|
| 156 |
</div> |
| 157 |
<?php |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Update price quantity rules for simple product |
| 162 |
* |
| 163 |
* @param int $productId |
| 164 |
*/ |
| 165 |
public function updatePriceRules( $productId ) |
| 166 |
{ |
| 167 |
$data = $_POST; |
| 168 |
$prefix = ( isset( $data['product-type'] ) && in_array( $data['product-type'], array( 'simple', 'variable' ) ) ? sanitize_text_field( $data['product-type'] ) : 'simple' ); |
| 169 |
$fixedAmounts = ( isset( $data['tiered_price_fixed_quantity_' . $prefix] ) ? (array) $data['tiered_price_fixed_quantity_' . $prefix] : array() ); |
| 170 |
$fixedPrices = ( !empty($data['tiered_price_fixed_price_' . $prefix]) ? (array) $data['tiered_price_fixed_price_' . $prefix] : array() ); |
| 171 |
PriceManager::updateFixedPriceRules( $fixedAmounts, $fixedPrices, $productId ); |
| 172 |
self::updateProductTemplate( $productId, ( isset( $data['_tiered_pricing_template'] ) ? $data['_tiered_pricing_template'] : 'default' ) ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Render inputs for price rules on a simple product |
| 177 |
* |
| 178 |
* @global WC_Product $product_object |
| 179 |
*/ |
| 180 |
public function renderPriceRules() |
| 181 |
{ |
| 182 |
global $product_object ; |
| 183 |
|
| 184 |
if ( $product_object instanceof WC_Product ) { |
| 185 |
$type = PriceManager::getPricingType( $product_object->get_id(), 'fixed', 'edit' ); |
| 186 |
$this->getContainer()->getFileManager()->includeTemplate( 'admin/add-price-rules.php', array( |
| 187 |
'price_rules_fixed' => PriceManager::getFixedPriceRules( $product_object->get_id(), 'edit' ), |
| 188 |
'price_rules_percentage' => PriceManager::getPercentagePriceRules( $product_object->get_id(), 'edit' ), |
| 189 |
'type' => $type, |
| 190 |
'prefix' => 'simple', |
| 191 |
'isFree' => !tpt_fs()->is_premium(), |
| 192 |
) ); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
public static function getProductTemplate( $productId ) |
| 198 |
{ |
| 199 |
$template = get_post_meta( $productId, '_tiered_pricing_template', true ); |
| 200 |
return ( in_array( $template, array( |
| 201 |
'default', |
| 202 |
'table', |
| 203 |
'blocks', |
| 204 |
'dropdown', |
| 205 |
'tooltip', |
| 206 |
'options' |
| 207 |
) ) ? $template : 'default' ); |
| 208 |
} |
| 209 |
|
| 210 |
public static function updateProductTemplate( $productId, $template ) |
| 211 |
{ |
| 212 |
$template = ( in_array( $template, array( |
| 213 |
'default', |
| 214 |
'table', |
| 215 |
'blocks', |
| 216 |
'dropdown', |
| 217 |
'tooltip', |
| 218 |
'options' |
| 219 |
) ) ? $template : 'default' ); |
| 220 |
update_post_meta( $productId, '_tiered_pricing_template', $template ); |
| 221 |
} |
| 222 |
|
| 223 |
} |