PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Forms / TieredPricingRulesForm.php

TieredPricingRulesForm.php in Tiered Pricing Table for WooCommerce 8.0.2, at src/Forms/TieredPricingRulesForm.php

94 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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