| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Addons\CustomColumns\Columns; |
| 4 |
|
| 5 |
use TierPricingTable\Addons\CustomColumns\Schema; |
| 6 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 7 |
use TierPricingTable\Addons\RoleBasedPricing\RoleBasedPricingRule; |
| 8 |
use TierPricingTable\PricingRule; |
| 9 |
use TierPricingTable\Forms\Form; |
| 10 |
use TierPricingTable\TierPricingTablePlugin; |
| 11 |
class CustomDataColumn extends AbstractCustomColumn { |
| 12 |
const TYPE = 'custom'; |
| 13 |
|
| 14 |
public function getType() : string { |
| 15 |
return self::TYPE; |
| 16 |
} |
| 17 |
|
| 18 |
protected function hooks() { |
| 19 |
parent::hooks(); |
| 20 |
$this->adminHooks(); |
| 21 |
} |
| 22 |
|
| 23 |
public function isValid() : bool { |
| 24 |
$valid = parent::isValid(); |
| 25 |
if ( !isset( $this->data['data_type'] ) ) { |
| 26 |
$valid = false; |
| 27 |
} |
| 28 |
if ( !in_array( $this->data['data_type'], array_keys( Schema::getAvailableDataTypes() ) ) ) { |
| 29 |
$valid = false; |
| 30 |
} |
| 31 |
return $valid; |
| 32 |
} |
| 33 |
|
| 34 |
public function adminHooks() { |
| 35 |
add_action( |
| 36 |
'tiered_pricing_table/role_based_rules/delete_role_rule', |
| 37 |
function ( $productId, $role ) { |
| 38 |
delete_post_meta( $productId, $this->getMetaKey( $role ) ); |
| 39 |
}, |
| 40 |
10, |
| 41 |
2 |
| 42 |
); |
| 43 |
add_action( |
| 44 |
'tiered_pricing_table/admin/tiered_pricing_rules_form/after_pricing_type', |
| 45 |
array($this, 'renderFirstRowField'), |
| 46 |
10, |
| 47 |
7 |
| 48 |
); |
| 49 |
add_action( |
| 50 |
'tiered_pricing_table/admin/tiered_pricing_rules_form/inputs', |
| 51 |
array($this, 'renderInputField'), |
| 52 |
10, |
| 53 |
7 |
| 54 |
); |
| 55 |
// Each custom data field adds 25% of width for container |
| 56 |
add_filter( 'tiered_pricing_table/admin/tiered_pricing_rules_form/inputs_width', function ( $defaultWidth ) { |
| 57 |
$defaultWidth += 25; |
| 58 |
return min( 100, $defaultWidth ); |
| 59 |
} ); |
| 60 |
} |
| 61 |
|
| 62 |
public function savingHooks() { |
| 63 |
add_action( |
| 64 |
'tiered_pricing_table/admin/components/tiered_pricing_rules_form/get_from_request', |
| 65 |
array($this, 'saveField'), |
| 66 |
10, |
| 67 |
6 |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
public function pricingRuleAdjustmentHooks() { |
| 72 |
add_filter( |
| 73 |
'tiered_pricing_table/price/pricing_rule', |
| 74 |
array($this, 'addCustomColumnDataToPricingRule'), |
| 75 |
1, |
| 76 |
2 |
| 77 |
); |
| 78 |
add_filter( |
| 79 |
'tiered_pricing_table/role_based_pricing/after_adjusting_pricing_rule', |
| 80 |
function ( PricingRule $pricingRule, RoleBasedPricingRule $roleBasedPricingRule ) { |
| 81 |
return $this->addCustomColumnDataToPricingRule( $pricingRule, $pricingRule->getProductId(), $roleBasedPricingRule->getRole() ); |
| 82 |
}, |
| 83 |
2, |
| 84 |
3 |
| 85 |
); |
| 86 |
add_filter( |
| 87 |
'tiered_pricing_table/global_pricing/after_adjusting_pricing_rule', |
| 88 |
function ( PricingRule $pricingRule, GlobalPricingRule $globalPricingRule ) { |
| 89 |
return $this->addCustomColumnDataToPricingRule( |
| 90 |
$pricingRule, |
| 91 |
$globalPricingRule->getId(), |
| 92 |
null, |
| 93 |
false |
| 94 |
); |
| 95 |
}, |
| 96 |
3, |
| 97 |
3 |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
public function addCustomColumnDataToPricingRule( |
| 102 |
PricingRule $pricingRule, |
| 103 |
$entityId, |
| 104 |
$role = null, |
| 105 |
$isProductRule = true |
| 106 |
) : PricingRule { |
| 107 |
$percentageValues = $this->getValue( $entityId, 'percentage', $role ); |
| 108 |
$fixedValues = $this->getValue( $entityId, 'fixed', $role ); |
| 109 |
// Try to get variable product level value if values are empty. |
| 110 |
if ( empty( $percentageValues ) || empty( $fixedValues ) ) { |
| 111 |
if ( $isProductRule ) { |
| 112 |
$product = wc_get_product( $entityId ); |
| 113 |
if ( !$product ) { |
| 114 |
return $pricingRule; |
| 115 |
} |
| 116 |
if ( $product->get_parent_id() ) { |
| 117 |
$percentageValues = ( $percentageValues ? $percentageValues : $this->getValue( $product->get_parent_id(), 'percentage', $role ) ); |
| 118 |
$fixedValues = ( $fixedValues ? $fixedValues : $this->getValue( $product->get_parent_id(), 'fixed', $role ) ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
$pricingRule->customColumnsData[$this->getSlug()]['percentage'] = $percentageValues; |
| 123 |
$pricingRule->customColumnsData[$this->getSlug()]['fixed'] = $fixedValues; |
| 124 |
return $pricingRule; |
| 125 |
} |
| 126 |
|
| 127 |
public function renderInputField( |
| 128 |
$entityId, |
| 129 |
$amount, |
| 130 |
$role, |
| 131 |
$loop, |
| 132 |
$custom_prefix, |
| 133 |
$type |
| 134 |
) { |
| 135 |
$value = $this->getValueForAmount( |
| 136 |
$amount, |
| 137 |
$entityId, |
| 138 |
$role, |
| 139 |
$loop, |
| 140 |
$custom_prefix, |
| 141 |
$type |
| 142 |
); |
| 143 |
$name = Form::getFieldName( |
| 144 |
$this->getSlug( $type ), |
| 145 |
$role, |
| 146 |
$loop, |
| 147 |
$custom_prefix |
| 148 |
); |
| 149 |
$class = ''; |
| 150 |
if ( $this->getDataType() === 'price' ) { |
| 151 |
$class = 'wc_input_price'; |
| 152 |
$value = wc_format_localized_price( $value ); |
| 153 |
} |
| 154 |
?> |
| 155 |
<input |
| 156 |
<?php |
| 157 |
echo esc_attr( ( tpt_fs()->can_use_premium_code() ? '' : 'disabled' ) ); |
| 158 |
?> |
| 159 |
type="<?php |
| 160 |
echo ( esc_attr( $this->getDataType() === 'number' ) ? 'number' : 'text' ); |
| 161 |
?>" |
| 162 |
class="<?php |
| 163 |
echo esc_attr( $class ); |
| 164 |
?>" |
| 165 |
value="<?php |
| 166 |
echo esc_attr( $value ); |
| 167 |
?>" |
| 168 |
placeholder="<?php |
| 169 |
echo esc_attr( $this->getName() ); |
| 170 |
?>" |
| 171 |
name="<?php |
| 172 |
echo esc_attr( $name ); |
| 173 |
?>[]" |
| 174 |
> |
| 175 |
<?php |
| 176 |
} |
| 177 |
|
| 178 |
public function renderFirstRowField( |
| 179 |
$entityId, |
| 180 |
$role, |
| 181 |
$loop, |
| 182 |
$custom_prefix |
| 183 |
) { |
| 184 |
$class = ''; |
| 185 |
$value = $this->getFirstRowValue( $entityId, 'fixed', $role ); |
| 186 |
if ( $this->getDataType() === 'price' ) { |
| 187 |
$class = 'wc_input_price'; |
| 188 |
$value = wc_format_localized_price( $value ); |
| 189 |
} |
| 190 |
woocommerce_wp_text_input( array( |
| 191 |
'custom_attributes' => ( tpt_fs()->can_use_premium_code() ? array() : array( |
| 192 |
'disabled' => 'disabled', |
| 193 |
) ), |
| 194 |
'id' => Form::getFieldName( |
| 195 |
$this->getSlug( 'first_row' ), |
| 196 |
$role, |
| 197 |
$loop, |
| 198 |
$custom_prefix |
| 199 |
), |
| 200 |
'label' => $this->getName() . ' ' . __( '(first row)', 'tier-pricing-table' ), |
| 201 |
'value' => $value, |
| 202 |
'class' => $class, |
| 203 |
'placeholder' => $this->getName(), |
| 204 |
'wrapper_class' => ( is_null( $loop ) ? 'tiered-pricing-form-block' : 'tiered-pricing-form-variation-block' ), |
| 205 |
) ); |
| 206 |
if ( !tpt_fs()->can_use_premium_code() ) { |
| 207 |
?> |
| 208 |
<div style="padding: 0 20px 0 162px!important; color:red;"> |
| 209 |
<?php |
| 210 |
esc_html_e( 'Custom columns are available only in the premium version.', 'tier-pricing-table' ); |
| 211 |
?> |
| 212 |
<a target="_blank" href="<?php |
| 213 |
echo esc_attr( tpt_fs()->get_upgrade_url() ); |
| 214 |
?>"> |
| 215 |
<?php |
| 216 |
esc_html_e( 'Upgrade you plan', 'tier-pricing-table' ); |
| 217 |
?> |
| 218 |
</a> |
| 219 |
</div> |
| 220 |
<?php |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public function getValue( $entityId, $type, $role = null ) : array { |
| 225 |
$value = (array) get_post_meta( $entityId, $this->getMetaKey( $role ), true ); |
| 226 |
return ( !empty( $value[$type] ) ? (array) $value[$type] : array() ); |
| 227 |
} |
| 228 |
|
| 229 |
public function getValueForAmount( |
| 230 |
$amount, |
| 231 |
$entityId, |
| 232 |
$role, |
| 233 |
$loop, |
| 234 |
$custom_prefix, |
| 235 |
$type |
| 236 |
) { |
| 237 |
if ( !$amount ) { |
| 238 |
return null; |
| 239 |
} |
| 240 |
$amount = intval( $amount ); |
| 241 |
if ( $amount < 2 ) { |
| 242 |
return null; |
| 243 |
} |
| 244 |
$value = $this->getValue( $entityId, $type, $role ); |
| 245 |
if ( array_key_exists( $amount, $value ) ) { |
| 246 |
return $value[$amount]; |
| 247 |
} |
| 248 |
return null; |
| 249 |
} |
| 250 |
|
| 251 |
public function getFirstRowValue( $entityId, $type, $role ) { |
| 252 |
$value = $this->getValue( $entityId, $type, $role ); |
| 253 |
if ( array_key_exists( 'first_row', $value ) ) { |
| 254 |
return $value['first_row']; |
| 255 |
} |
| 256 |
return null; |
| 257 |
} |
| 258 |
|
| 259 |
public function saveField( |
| 260 |
$entityId, |
| 261 |
$role, |
| 262 |
$loop, |
| 263 |
$customPrefix, |
| 264 |
$data, |
| 265 |
$request |
| 266 |
) { |
| 267 |
$_value = array( |
| 268 |
'fixed' => array(), |
| 269 |
'percentage' => array(), |
| 270 |
); |
| 271 |
foreach ( array('fixed', 'percentage') as $pricingType ) { |
| 272 |
$value = Form::getFieldValue( |
| 273 |
$this->getSlug( $pricingType ), |
| 274 |
$role, |
| 275 |
$loop, |
| 276 |
$customPrefix, |
| 277 |
$request |
| 278 |
); |
| 279 |
$value = ( is_array( $value ) ? $value : array() ); |
| 280 |
$amounts = ( 'fixed' === $pricingType ? $data['fixed_quantities'] : $data['percentage_quantities'] ); |
| 281 |
foreach ( $value as $key => $itemValue ) { |
| 282 |
if ( !empty( $amounts[$key] ) ) { |
| 283 |
$_value[$pricingType][$amounts[$key]] = $this->sanitizeValue( $itemValue ); |
| 284 |
} |
| 285 |
} |
| 286 |
$firstRowValue = Form::getFieldValue( |
| 287 |
$this->getSlug( 'first_row' ), |
| 288 |
$role, |
| 289 |
$loop, |
| 290 |
$customPrefix, |
| 291 |
$request |
| 292 |
); |
| 293 |
if ( !Form::isEmpty( $firstRowValue ) ) { |
| 294 |
$_value[$pricingType]['first_row'] = $this->sanitizeValue( $firstRowValue ); |
| 295 |
} |
| 296 |
} |
| 297 |
update_post_meta( $entityId, $this->getMetaKey( $role ), $_value ); |
| 298 |
} |
| 299 |
|
| 300 |
protected function _getSingleRowValue( PricingRule $pricingRule, $currentTierQuantity = null ) { |
| 301 |
$key = ( $currentTierQuantity ? $currentTierQuantity : 'first_row' ); |
| 302 |
$value = ''; |
| 303 |
if ( !empty( $pricingRule->customColumnsData[$this->getSlug()][$pricingRule->getType()] ) ) { |
| 304 |
$data = $pricingRule->customColumnsData[$this->getSlug()][$pricingRule->getType()]; |
| 305 |
if ( array_key_exists( $key, $data ) ) { |
| 306 |
$value = $data[$key]; |
| 307 |
} |
| 308 |
} |
| 309 |
if ( !Form::isEmpty( $value ) ) { |
| 310 |
if ( $this->getDataType() === 'price' ) { |
| 311 |
$value = wc_price( wc_get_price_to_display( wc_get_product( $pricingRule->getProductId() ), array( |
| 312 |
'qty' => 1, |
| 313 |
'price' => $value, |
| 314 |
) ) ); |
| 315 |
} |
| 316 |
} |
| 317 |
return apply_filters( |
| 318 |
'tiered_pricing_table/custom_columns/value', |
| 319 |
$value, |
| 320 |
$pricingRule, |
| 321 |
$currentTierQuantity, |
| 322 |
$this |
| 323 |
); |
| 324 |
} |
| 325 |
|
| 326 |
public function getDataType() : string { |
| 327 |
return $this->data['data_type'] ?? 'price'; |
| 328 |
} |
| 329 |
|
| 330 |
protected function sanitizeValue( $value ) { |
| 331 |
$value = ( Form::isEmpty( $value ) ? null : $value ); |
| 332 |
if ( 'text' === $this->getDataType() ) { |
| 333 |
return sanitize_text_field( $value ); |
| 334 |
} elseif ( 'number' === $this->getDataType() ) { |
| 335 |
return floatval( $value ); |
| 336 |
} elseif ( 'price' === $this->getDataType() ) { |
| 337 |
return wc_format_decimal( $value ); |
| 338 |
} |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
public function getMetaKey( $role = null ) : string { |
| 343 |
$rolePrefix = ( $role ? '_' . $role : '' ); |
| 344 |
return $rolePrefix . '_tiered_price_custom_column_' . $this->getSlug(); |
| 345 |
} |
| 346 |
|
| 347 |
} |
| 348 |
|