| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Admin\ProductPage; |
| 4 |
|
| 5 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 6 |
use TierPricingTable\PriceManager; |
| 7 |
use TierPricingTable\Forms\MinimumOrderQuantityForm; |
| 8 |
use TierPricingTable\Forms\TieredPricingRulesForm; |
| 9 |
use TierPricingTable\TierPricingTablePlugin; |
| 10 |
/** |
| 11 |
* Class ProductManager |
| 12 |
* |
| 13 |
* @package TierPricingTable\Admin\Product |
| 14 |
*/ |
| 15 |
class TieredPricingTab { |
| 16 |
use ServiceContainerTrait; |
| 17 |
public function __construct() { |
| 18 |
// Tiered Pricing Product Tab |
| 19 |
add_filter( |
| 20 |
'woocommerce_product_data_tabs', |
| 21 |
array($this, 'register'), |
| 22 |
99, |
| 23 |
1 |
| 24 |
); |
| 25 |
add_action( 'woocommerce_product_data_panels', array($this, 'render') ); |
| 26 |
add_action( 'woocommerce_process_product_meta', array($this, 'save') ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add tiered pricing tab to woocommerce product tabs |
| 31 |
* |
| 32 |
* @param array $productTabs |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public function register( $productTabs ) : array { |
| 37 |
$productTabs['tiered-pricing-tab'] = array( |
| 38 |
'label' => __( 'Tiered Pricing', 'tier-pricing-table' ), |
| 39 |
'target' => 'tiered-pricing-data', |
| 40 |
'class' => (function () { |
| 41 |
$types = array_merge( TierPricingTablePlugin::getSupportedSimpleProductTypes(), TierPricingTablePlugin::getSupportedVariableProductTypes() ); |
| 42 |
return array_map( function ( $type ) { |
| 43 |
return 'show_if_' . $type; |
| 44 |
}, $types ); |
| 45 |
})(), |
| 46 |
); |
| 47 |
return $productTabs; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Render content for the tiered pricing tab |
| 52 |
*/ |
| 53 |
public function render() { |
| 54 |
global $post; |
| 55 |
?> |
| 56 |
<div id="tiered-pricing-data" class="panel woocommerce_options_panel"> |
| 57 |
|
| 58 |
<?php |
| 59 |
if ( !tpt_fs()->can_use_premium_code() ) { |
| 60 |
//$this->renderUpgradeNotice(); |
| 61 |
} |
| 62 |
if ( tpt_fs()->can_use_premium_code() && !tpt_fs()->is_premium() ) { |
| 63 |
$this->getContainer()->getFileManager()->includeTemplate( 'admin/banners/free-version-used-premium-available.php', array( |
| 64 |
'is_product' => true, |
| 65 |
) ); |
| 66 |
} |
| 67 |
do_action( 'tiered_pricing_table/admin/pricing_tab_begin', $post->ID ); |
| 68 |
?> |
| 69 |
|
| 70 |
<div class="hidden show_if_variable options_group"> |
| 71 |
<?php |
| 72 |
$type = PriceManager::getPricingType( $post->ID, 'fixed', 'edit' ); |
| 73 |
$percentageRules = PriceManager::getPercentagePriceRules( $post->ID, 'edit' ); |
| 74 |
$fixedRules = PriceManager::getFixedPriceRules( $post->ID, 'edit' ); |
| 75 |
TieredPricingRulesForm::render( |
| 76 |
$post->ID, |
| 77 |
null, |
| 78 |
null, |
| 79 |
$type, |
| 80 |
$percentageRules, |
| 81 |
$fixedRules, |
| 82 |
'_variable' |
| 83 |
); |
| 84 |
?> |
| 85 |
</div> |
| 86 |
|
| 87 |
<div class="options_group"> |
| 88 |
<?php |
| 89 |
$min = PriceManager::getProductQtyMin( $post->ID, 'edit' ); |
| 90 |
MinimumOrderQuantityForm::render( null, null, $min ); |
| 91 |
do_action( 'tiered_pricing_table/admin/after_minimum_order_quantity_field', $post->ID, null ); |
| 92 |
?> |
| 93 |
</div> |
| 94 |
|
| 95 |
<?php |
| 96 |
do_action( 'tiered_pricing_table/admin/before_advance_product_options', $post->ID ); |
| 97 |
?> |
| 98 |
|
| 99 |
<div class="tiered_pricing_tab_product_advance_options"> |
| 100 |
<div class="tiered_pricing_tab_product_advance_options__header"> |
| 101 |
<h4> |
| 102 |
<span class="dashicons dashicons-admin-settings"></span> |
| 103 |
<?php |
| 104 |
esc_html_e( 'Additional Options', 'tier-pricing-table' ); |
| 105 |
?> |
| 106 |
</h4> |
| 107 |
<div> |
| 108 |
<span class="tiered_pricing_arrow_down">â–¼</span> |
| 109 |
<span class="tiered_pricing_arrow_up">â–²</span> |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
|
| 113 |
<div class="tiered_pricing_tab_product_advance_options__content"> |
| 114 |
|
| 115 |
<?php |
| 116 |
$availableTemplates = TierPricingTablePlugin::getAvailablePricingLayouts(); |
| 117 |
$availableTemplates = array_merge( array( |
| 118 |
'default' => __( 'Default' ), |
| 119 |
), $availableTemplates ); |
| 120 |
woocommerce_wp_select( array( |
| 121 |
'id' => '_tiered_pricing_template', |
| 122 |
'value' => self::getProductTemplate( $post->ID ), |
| 123 |
'options' => $availableTemplates, |
| 124 |
'label' => __( 'Pricing layout', 'tier-pricing-table' ), |
| 125 |
'description' => __( 'Override the global layout for this specific product.', 'tier-pricing-table' ), |
| 126 |
'default' => 'default', |
| 127 |
'desc_tip' => true, |
| 128 |
) ); |
| 129 |
?> |
| 130 |
|
| 131 |
<p class="form-field"> |
| 132 |
<label> |
| 133 |
<?php |
| 134 |
esc_html_e( 'Unit label', 'tier-pricing-table' ); |
| 135 |
?> |
| 136 |
</label> |
| 137 |
|
| 138 |
<?php |
| 139 |
$productBaseUnitName = self::getProductBaseUnitName( $post->ID ); |
| 140 |
?> |
| 141 |
|
| 142 |
<input type="text" |
| 143 |
value="<?php |
| 144 |
echo esc_attr( $productBaseUnitName['singular'] ); |
| 145 |
?>" |
| 146 |
placeholder="<?php |
| 147 |
esc_attr_e( 'Singular (e.g. piece)', 'tier-pricing-table' ); |
| 148 |
?>" |
| 149 |
style="width: 24%; margin-right: 20px;" |
| 150 |
name="_tiered_pricing_base_unit_name[singular]"> |
| 151 |
|
| 152 |
<input type="text" |
| 153 |
value="<?php |
| 154 |
echo esc_attr( $productBaseUnitName['plural'] ); |
| 155 |
?>" |
| 156 |
placeholder="<?php |
| 157 |
esc_attr_e( 'Plural (e.g. pieces)', 'tier-pricing-table' ); |
| 158 |
?>" |
| 159 |
style="width: 24%" |
| 160 |
name="_tiered_pricing_base_unit_name[plural]"> |
| 161 |
</p> |
| 162 |
|
| 163 |
<div style=" padding: 0 20px 0px 162px; |
| 164 |
margin-bottom: 10px; |
| 165 |
color: #666; |
| 166 |
margin-top: -10px; |
| 167 |
font-size: 12px;"> |
| 168 |
<?php |
| 169 |
esc_html_e( 'Displayed next to quantities. Leave empty to inherit global settings.', 'tier-pricing-table' ); |
| 170 |
?> |
| 171 |
</div> |
| 172 |
|
| 173 |
<?php |
| 174 |
do_action( 'tiered_pricing_table/admin/advance_product_options', $post->ID ); |
| 175 |
?> |
| 176 |
</div> |
| 177 |
</div> |
| 178 |
|
| 179 |
<?php |
| 180 |
do_action( 'tiered_pricing_table/admin/pricing_tab_end', $post->ID ); |
| 181 |
?> |
| 182 |
</div> |
| 183 |
<?php |
| 184 |
} |
| 185 |
|
| 186 |
protected function renderUpgradeNotice() { |
| 187 |
?> |
| 188 |
<div |
| 189 |
style="display:flex; align-items:center; justify-content: space-between; background: #fafafa; padding: 10px; margin: 0 0 20px;border-bottom: 1px solid #eee;"> |
| 190 |
<div> |
| 191 |
<span style="color: red;"> |
| 192 |
🚀 |
| 193 |
<?php |
| 194 |
esc_html_e( 'Upgrade your plan to unlock all great features.', 'tier-pricing-table' ); |
| 195 |
?> |
| 196 |
|
| 197 |
</span> |
| 198 |
</div> |
| 199 |
|
| 200 |
<div> |
| 201 |
<a target="_blank" class="button button-primary" |
| 202 |
href="<?php |
| 203 |
echo esc_attr( tpt_fs()->get_upgrade_url() ); |
| 204 |
?>"> |
| 205 |
<?php |
| 206 |
esc_html_e( 'Upgrade', 'tier-pricing-table' ); |
| 207 |
?> |
| 208 |
</a> |
| 209 |
</div> |
| 210 |
|
| 211 |
</div> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Save tiered pricing tab data |
| 217 |
* |
| 218 |
* @param $productId |
| 219 |
*/ |
| 220 |
public function save( $productId ) { |
| 221 |
if ( wp_verify_nonce( true, true ) ) { |
| 222 |
// as phpcs comments at Woo is not available, we have to do such a trash |
| 223 |
$woo = 'Woo, please add ignoring comments to your phpcs checker'; |
| 224 |
} |
| 225 |
$template = ( isset( $_POST['_tiered_pricing_template'] ) ? sanitize_text_field( $_POST['_tiered_pricing_template'] ) : 'default' ); |
| 226 |
self::updateProductTemplate( $productId, sanitize_text_field( $template ) ); |
| 227 |
$baseUnitName = ( isset( $_POST['_tiered_pricing_base_unit_name'] ) ? array_map( 'sanitize_text_field', $_POST['_tiered_pricing_base_unit_name'] ) : array() ); |
| 228 |
self::updateProductBaseUnitName( $productId, $baseUnitName ); |
| 229 |
} |
| 230 |
|
| 231 |
public static function getProductTemplate( $productId ) { |
| 232 |
$template = get_post_meta( $productId, '_tiered_pricing_template', true ); |
| 233 |
$availableTemplates = TierPricingTablePlugin::getAvailablePricingLayouts(); |
| 234 |
return ( array_key_exists( $template, $availableTemplates ) ? $template : 'default' ); |
| 235 |
} |
| 236 |
|
| 237 |
public static function updateProductTemplate( $productId, $template ) { |
| 238 |
$availableTemplates = TierPricingTablePlugin::getAvailablePricingLayouts(); |
| 239 |
$template = ( array_key_exists( $template, $availableTemplates ) ? $template : 'default' ); |
| 240 |
if ( 'default' !== $template ) { |
| 241 |
update_post_meta( $productId, '_tiered_pricing_template', $template ); |
| 242 |
} else { |
| 243 |
delete_post_meta( $productId, '_tiered_pricing_template' ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
public static function getProductBaseUnitName( $productId ) : array { |
| 248 |
$_baseUnitName = (array) get_post_meta( $productId, '_tiered_pricing_base_unit_name', true ); |
| 249 |
$baseUnitName['singular'] = $_baseUnitName['singular'] ?? null; |
| 250 |
$baseUnitName['plural'] = $_baseUnitName['plural'] ?? null; |
| 251 |
return $baseUnitName; |
| 252 |
} |
| 253 |
|
| 254 |
public static function updateProductBaseUnitName( $productId, array $unitNames ) { |
| 255 |
$baseUnitName['singular'] = $unitNames['singular'] ?? null; |
| 256 |
$baseUnitName['plural'] = $unitNames['plural'] ?? null; |
| 257 |
if ( empty( $baseUnitName['singular'] ) && empty( $baseUnitName['plural'] ) ) { |
| 258 |
delete_post_meta( $productId, '_tiered_pricing_base_unit_name' ); |
| 259 |
} else { |
| 260 |
update_post_meta( $productId, '_tiered_pricing_base_unit_name', $baseUnitName ); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
} |
| 265 |
|