| 1 |
<?php namespace TierPricingTable\Forms; |
| 2 |
|
| 3 |
use TierPricingTable\Core\ServiceContainer; |
| 4 |
|
| 5 |
class TieredPricingRulesForm { |
| 6 |
|
| 7 |
public static function render( |
| 8 |
$entityId, |
| 9 |
$role = null, |
| 10 |
$loop = null, |
| 11 |
$tieredPricingType = 'fixed', |
| 12 |
$percentageRule = array(), |
| 13 |
$fixedRules = array(), |
| 14 |
$customPrefix = null |
| 15 |
) { |
| 16 |
ServiceContainer::getInstance()->getFileManager()->includeTemplate( 'admin/components/tiered-pricing-rules-form.php', |
| 17 |
array( |
| 18 |
'role' => $role, |
| 19 |
'loop' => $loop, |
| 20 |
'tiered_pricing_type' => $tieredPricingType, |
| 21 |
'percentage_rules' => $percentageRule, |
| 22 |
'fixed_rules' => $fixedRules, |
| 23 |
'custom_prefix' => $customPrefix, |
| 24 |
'entity_id' => $entityId, |
| 25 |
) ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function getDataFromRequest( $role = null, $loop = null, $customPrefix = '', $request = null, $entityId = null ): array { |
| 29 |
|
| 30 |
if ( wp_verify_nonce( true, true ) ) { |
| 31 |
// as phpcs comments at Woo is not available, we have to do such a trash |
| 32 |
$woo = 'Woo, please add ignoring comments to your phpcs checker'; |
| 33 |
} |
| 34 |
|
| 35 |
$request = $request ? $request : $_POST; |
| 36 |
$data = array(); |
| 37 |
|
| 38 |
$fields = array( |
| 39 |
'type' => function ( $pricingType ) { |
| 40 |
return in_array( $pricingType, array( 'fixed', 'percentage' ) ) ? $pricingType : 'fixed'; |
| 41 |
}, |
| 42 |
'percentage_quantities' => function ( $quantities ) { |
| 43 |
return is_array( $quantities ) ? $quantities : array(); |
| 44 |
}, |
| 45 |
'percentage_discounts' => function ( $discounts ) { |
| 46 |
return is_array( $discounts ) ? $discounts : array(); |
| 47 |
}, |
| 48 |
'fixed_quantities' => function ( $quantities ) { |
| 49 |
return is_array( $quantities ) ? $quantities : array(); |
| 50 |
}, |
| 51 |
'fixed_prices' => function ( $prices ) { |
| 52 |
return is_array( $prices ) ? $prices : array(); |
| 53 |
}, |
| 54 |
); |
| 55 |
|
| 56 |
foreach ( $fields as $fieldKey => $sanitizeFunction ) { |
| 57 |
$data[ $fieldKey ] = Form::getFieldValue( $fieldKey, $role, $loop, $customPrefix, $request ); |
| 58 |
$data[ $fieldKey ] = call_user_func( $sanitizeFunction, $data[ $fieldKey ] ); |
| 59 |
} |
| 60 |
|
| 61 |
self::buildRules( $data ); |
| 62 |
|
| 63 |
do_action( 'tiered_pricing_table/admin/components/tiered_pricing_rules_form/get_from_request', $entityId, $role, |
| 64 |
$loop, $customPrefix, $data, $request ); |
| 65 |
|
| 66 |
return $data; |
| 67 |
} |
| 68 |
|
| 69 |
public static function buildRules( array &$data ) { |
| 70 |
|
| 71 |
$fixedRules = array(); |
| 72 |
|
| 73 |
foreach ( $data['fixed_quantities'] as $key => $amount ) { |
| 74 |
if ( ! empty( $amount ) && ! empty( $data['fixed_prices'][ $key ] ) && ! key_exists( $amount, |
| 75 |
$fixedRules ) ) { |
| 76 |
$fixedRules[ $amount ] = wc_format_decimal( $data['fixed_prices'][ $key ] ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$data['fixed_tiered_pricing_rules'] = $fixedRules; |
| 81 |
|
| 82 |
$percentageRules = array(); |
| 83 |
|
| 84 |
foreach ( $data['percentage_quantities'] as $key => $amount ) { |
| 85 |
if ( ! empty( $amount ) && ! empty( $data['percentage_discounts'][ $key ] ) && ! array_key_exists( $amount, |
| 86 |
$percentageRules ) && $data['percentage_discounts'][ $key ] < 100 ) { |
| 87 |
$percentageRules[ $amount ] = $data['percentage_discounts'][ $key ]; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$data['percentage_tiered_pricing_rules'] = $percentageRules; |
| 92 |
} |
| 93 |
} |
| 94 |
|