PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.4
Tiered Pricing Table for WooCommerce v7.1.4
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 / Admin / ProductPage / AdvanceOptionsForVariableProduct.php

AdvanceOptionsForVariableProduct.php in Tiered Pricing Table for WooCommerce 7.1.4, at src/Admin/ProductPage/AdvanceOptionsForVariableProduct.php

243 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Admin\ProductPage;
2
3 use TierPricingTable\TierPricingTablePlugin;
4 use WC_Product_Variable;
5 use WC_Product_Variation;
6
7 /**
8 * Class VariationProduct
9 *
10 * @package TierPricingTable\Admin\Product
11 */
12 class AdvanceOptionsForVariableProduct {
13
14 const PRODUCT_VARIATIONS_SEARCH_ACTION = 'woocommerce_json_search_tpt_product_variations';
15
16 /**
17 * Register hooks
18 */
19 public function __construct() {
20 add_action( 'wp_ajax_' . self::PRODUCT_VARIATIONS_SEARCH_ACTION, array(
21 $this,
22 'productVariationsSearchHandler',
23 ) );
24
25 // Saving
26 add_action( 'woocommerce_process_product_meta', array( $this, 'saveOptions' ) );
27
28 add_action( 'tiered_pricing_table/admin/advance_product_options', function ( $productId ) {
29
30 $product = wc_get_product( $productId );
31
32 if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) {
33 return;
34 }
35 ?>
36
37 <p class="form-field _tiered_pricing_default_variation show_if_variable">
38 <label for="_tiered_pricing_default_variation_id">
39 <?php
40 esc_html_e( 'Default variation', 'tier-pricing-table' );
41 ?>
42 </label>
43
44 <select class="wc-product-search" style="width: 50%;"
45 id="_tiered_pricing_default_variation_id"
46 data-allow_clear="true"
47 name="_tiered_pricing_default_variation_id"
48 data-include="<?php echo esc_attr( $productId ); ?>"
49 data-placeholder="<?php
50 esc_attr_e( 'Search variations&hellip;', 'tier-pricing-table' );
51 ?>"
52 data-action="woocommerce_json_search_tpt_product_variations">
53
54 <?php $default = self::getDefaultVariation( $productId, 'edit' ); ?>
55
56 <?php if ( $default ) : ?>
57 <option selected value="<?php echo esc_attr( $default->get_id() ); ?>">
58 <?php echo esc_attr( $default->get_attribute_summary() ); ?>
59 </option>
60 <?php endif; ?>
61 </select>
62 </p>
63 <div style="padding: 0 20px 0 162px;
64 margin-bottom: 10px;
65 color: #666;
66 margin-top: -10px;
67 font-size: 12px;">
68 <?php
69 esc_html_e( 'Display this variation\'s pricing before attributes are selected. The variation itself won\'t be pre-selected.',
70 'tier-pricing-table' );
71 ?>
72 </div>
73
74 <?php
75 woocommerce_wp_checkbox( array(
76 'id' => '_tiered_pricing_variable_product_same_prices',
77 'value' => wc_bool_to_string( self::isVariableProductSamePrices( $productId ) ),
78 'label' => __( 'Variations share pricing', 'tier-pricing-table' ),
79 'description' => __( 'Use the default variation\'s pricing for all variations to prevent reloading the pricing table.',
80 'tier-pricing-table' ),
81 ) );
82
83 woocommerce_wp_select( array(
84 'id' => '_tiered_price_mix_and_match_minimum',
85 'value' => get_post_meta( $productId, '_tiered_price_mix_and_match_minimum', true ),
86 'options' => array(
87 '' => __( 'Global Settings', 'tier-pricing-table' ),
88 'no' => __( 'Individual variation', 'tier-pricing-table' ),
89 'yes' => __( 'Variable product total (Mix & match)', 'tier-pricing-table' ),
90 ),
91 'label' => __( 'Calculate minimum quantity by', 'tier-pricing-table' ),
92 ) );
93 ?>
94 <div style="padding: 0 20px 0 162px;
95 margin-bottom: 10px;
96 color: #666;
97 margin-top: -10px;
98 font-size: 12px;">
99 <?php
100 esc_html_e( 'Determine whether the minimum quantity requirement applies to each variation separately, or to the combined total of all variations in the cart.',
101 'tier-pricing-table' );
102 ?>
103 </div>
104 <?php
105 } );
106 }
107
108 /**
109 * Save advanced options related to the variable product
110 *
111 * @param string|int $productId
112 */
113 public function saveOptions( $productId ) {
114
115 if ( wp_verify_nonce( true, true ) ) {
116 // as phpcs comments at Woo is not available, we have to do such a trash
117 $woo = 'Woo, please add ignoring comments to your phpcs checker';
118 }
119
120 $data = $_POST;
121
122 $defaultVariation = ! empty( $data['_tiered_pricing_default_variation_id'] ) ? intval( $data['_tiered_pricing_default_variation_id'] ) : null;
123 $samePrices = ! empty( $data['_tiered_pricing_variable_product_same_prices'] );
124 $mixAndMatch = isset( $data['_tiered_price_mix_and_match_minimum'] ) ? sanitize_text_field( $data['_tiered_price_mix_and_match_minimum'] ) : '';
125
126 self::updateDefaultVariation( $productId, $defaultVariation );
127 self::updateVariableProductSamePrices( $productId, $samePrices );
128 self::updateMixAndMatchMinimum( $productId, $mixAndMatch );
129 }
130
131 public static function updateMixAndMatchMinimum( $productId, $mixAndMatch ) {
132 if ( in_array( $mixAndMatch, array( 'yes', 'no' ) ) ) {
133 update_post_meta( $productId, '_tiered_price_mix_and_match_minimum', $mixAndMatch );
134 } else {
135 delete_post_meta( $productId, '_tiered_price_mix_and_match_minimum' );
136 }
137 }
138
139 public static function isVariableProductSamePrices( $productId ): bool {
140 return 'yes' === get_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', true );
141 }
142
143 public static function updateVariableProductSamePrices( $productId, bool $samePrices = false ) {
144 if ( $samePrices ) {
145 update_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', 'yes' );
146 } else {
147 delete_post_meta( $productId, '_tiered_pricing_variable_product_same_prices' );
148 }
149 }
150
151 public static function updateDefaultVariation( $variableProductId, $defaultVariationId = null ) {
152 if ( $defaultVariationId ) {
153 update_post_meta( $variableProductId, '_tiered_pricing_default_variation_id', $defaultVariationId );
154 } else {
155 delete_post_meta( $variableProductId, '_tiered_pricing_default_variation_id' );
156 }
157 }
158
159 /**
160 * Get default variation
161 *
162 * @param int $productId
163 * @param string $context
164 *
165 * @return ?WC_Product_Variation
166 */
167 public static function getDefaultVariation( $productId, $context = 'view' ) {
168 $defaultVariationId = get_post_meta( $productId, '_tiered_pricing_default_variation_id', true );
169
170 $defaultVariation = $defaultVariationId ? wc_get_product( $defaultVariationId ) : false;
171
172 if ( ! ( $defaultVariation instanceof WC_Product_Variation ) ) {
173 $defaultVariation = null;
174 }
175
176 if ( 'edit' !== $context ) {
177 return apply_filters( 'tiered_pricing_table/product/default_variation', $defaultVariation, $productId );
178 }
179
180 return $defaultVariation;
181 }
182
183 public function productVariationsSearchHandler() {
184
185 check_ajax_referer( 'search-products', 'security' );
186
187 if ( empty( $term ) && isset( $_GET['term'] ) ) {
188 $term = (string) wc_clean( wp_unslash( $_GET['term'] ) );
189 }
190
191 if ( empty( $term ) ) {
192 wp_send_json( array() );
193 }
194
195 $limit = 30;
196 $include = ! empty( $_GET['include'] ) ? intval( $_GET['include'] ) : false;
197
198 if ( ! $include ) {
199 wp_send_json( array() );
200 }
201
202 $product = wc_get_product( $include );
203
204 if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) {
205 wp_send_json( array() );
206 }
207
208 $results = array();
209
210 if ( $product instanceof WC_Product_Variable ) {
211 $variationsObjects = $product->get_available_variations( 'objects' );
212 $variations = array();
213 $rawVariations = array();
214
215 foreach ( $variationsObjects as $variation ) {
216 $rawVariations[ '_' . $variation->get_id() ] = rawurldecode( wp_strip_all_tags( $variation->get_attribute_summary() ) );
217 $variations = $rawVariations;
218 }
219
220 if ( count( $rawVariations ) > $limit ) {
221 $similarTextResults = array();
222
223 foreach ( $variationsObjects as $variation ) {
224 $similarTextResults[ '_' . $variation->get_id() ] = similar_text( strtolower( $variation->get_attribute_summary() ),
225 strtolower( $term ) );
226 }
227
228 asort( $similarTextResults );
229 $similarTextResults = array_reverse( $similarTextResults );
230 $similarTextResults = array_slice( $similarTextResults, 0, $limit );
231
232 $variations = array_intersect_key( $similarTextResults, $rawVariations );
233
234 }
235
236 foreach ( $variations as $key => $value ) {
237 $results[ str_replace( '_', '', $key ) ] = $rawVariations[ $key ];
238 }
239 }
240
241 wp_send_json( $results );
242 }
243 }