PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
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 2.3.4 All 90 releases
tier-pricing-table / src / Addons / AdvancedQuantityOptions / DataProvider.php

DataProvider.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/AdvancedQuantityOptions/DataProvider.php

122 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\AdvancedQuantityOptions;
2
3 use TierPricingTable\Forms\Form;
4 use TierPricingTable\TierPricingTablePlugin;
5
6 class DataProvider {
7
8 protected static $cache = array();
9
10 protected static function sanitize( $type, $value ) {
11
12 switch ( $type ) {
13 case 'maximum':
14 if ( Form::isEmpty( $value ) ) {
15 return null;
16 }
17
18 return max( intval( $value ), 1 );
19 case 'group_of':
20 if ( Form::isEmpty( $value ) ) {
21 return null;
22 }
23
24 return max( intval( $value ), 2 );
25 default:
26 return null;
27 }
28 }
29
30 protected static function update( $productId, $type, $value, $role = false ) {
31 $metaKey = self::getMetaKey( $type, $role );
32
33 $value = self::sanitize( $type, $value );
34
35 if ( $metaKey ) {
36 update_post_meta( $productId, $metaKey, $value );
37 }
38 }
39
40 public static function getMetaKey( $type, $role = false ) {
41 $role = $role ? "_{$role}" : '';
42
43 switch ( $type ) {
44 case 'maximum':
45 return "{$role}" . '_tiered_pricing_' . AdvancedQuantityOptionsAddon::MAXIMUM_QUANTITY_BASE_META_KEY;
46 case 'group_of':
47 return "{$role}" . '_tiered_pricing_' . AdvancedQuantityOptionsAddon::GROUP_OF_QUANTITY_BASE_META_KEY;
48 default:
49 return false;
50 }
51 }
52
53 protected static function get( $productId, $type, $role = false, $context = 'view' ) {
54 $cacheKey = $productId . $role;
55
56 if ( isset( self::$cache[ $cacheKey ][ $type ] ) ) {
57 $value = self::$cache[ $cacheKey ][ $type ];
58 } else {
59 $metaKey = self::getMetaKey( $type, $role );
60
61 $value = get_post_meta( $productId, $metaKey, true );
62
63 // If product is a variation - check for parent level value
64 if ( Form::isEmpty( $value ) && 'edit' !== $context ) {
65 $product = wc_get_product( $productId );
66
67 if ( $product && TierPricingTablePlugin::isVariationProductSupported( $product ) ) {
68 $value = self::get( $product->get_parent_id(), $type, $role, 'edit' );
69 }
70 }
71
72 self::$cache[ $cacheKey ][ $type ] = $value;
73 }
74
75 $value = Form::isEmpty( $value ) ? null : $value;
76
77 if ( 'edit' !== $context ) {
78 return apply_filters( 'tiered_pricing_table/advanced_quantity/get_' . $type, $value, $productId, $role );
79 }
80
81 return $value;
82 }
83
84 public static function updateMaximumQuantity( $productId, $value, $role = false ) {
85 self::update( $productId, 'maximum', $value, $role );
86 }
87
88 public static function updateGroupOfQuantity( $productId, $value, $role = false ) {
89 self::update( $productId, 'group_of', $value, $role );
90 }
91
92 public static function getMaximumQuantity( $productId, $role = false, $context = 'view' ) {
93 return self::get( $productId, 'maximum', $role, $context );
94 }
95
96 public static function getGroupOfQuantity( $productId, $role = false, $context = 'view' ) {
97 return self::get( $productId, 'group_of', $role, $context );
98 }
99
100 public static function updateFromRequest(
101 $fieldToUpdate,
102 $entityId,
103 $role = null,
104 $loop = null,
105 $customPrefix = null
106 ) {
107 $base = array(
108 'maximum' => AdvancedQuantityOptionsAddon::MAXIMUM_QUANTITY_BASE_META_KEY,
109 'group_of' => AdvancedQuantityOptionsAddon::GROUP_OF_QUANTITY_BASE_META_KEY,
110 );
111
112 if ( ! isset( $base[ $fieldToUpdate ] ) ) {
113 return;
114 }
115
116 $value = Form::getFieldValue( $base[ $fieldToUpdate ], $role, $loop, $customPrefix );
117
118 self::update( $entityId, $fieldToUpdate, $value, $role );
119 }
120
121 }
122