PluginProbe
Tiered Pricing Table for WooCommerce / 6.5.0
Tiered Pricing Table for WooCommerce v6.5.0
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 6.5.0, at src/Admin/ProductPage/AdvanceOptionsForVariableProduct.php

210 lines 6.6 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 0px 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 }
84
85 /**
86 * Save advanced options related to the variable product
87 *
88 * @param string|int $productId
89 */
90 public function saveOptions( $productId ) {
91
92 if ( wp_verify_nonce( true, true ) ) {
93 // as phpcs comments at Woo is not available, we have to do such a trash
94 $woo = 'Woo, please add ignoring comments to your phpcs checker';
95 }
96
97 $data = $_POST;
98
99 $defaultVariation = ! empty( $data['_tiered_pricing_default_variation_id'] ) ? intval( $data['_tiered_pricing_default_variation_id'] ) : null;
100 $samePrices = ! empty( $data['_tiered_pricing_variable_product_same_prices'] );
101
102 self::updateDefaultVariation( $productId, $defaultVariation );
103 self::updateVariableProductSamePrices( $productId, $samePrices );
104 }
105
106 public static function isVariableProductSamePrices( $productId ): bool {
107 return 'yes' === get_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', true );
108 }
109
110 public static function updateVariableProductSamePrices( $productId, bool $samePrices = false ) {
111 if ( $samePrices ) {
112 update_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', 'yes' );
113 } else {
114 delete_post_meta( $productId, '_tiered_pricing_variable_product_same_prices' );
115 }
116 }
117
118 public static function updateDefaultVariation( $variableProductId, $defaultVariationId = null ) {
119 if ( $defaultVariationId ) {
120 update_post_meta( $variableProductId, '_tiered_pricing_default_variation_id', $defaultVariationId );
121 } else {
122 delete_post_meta( $variableProductId, '_tiered_pricing_default_variation_id' );
123 }
124 }
125
126 /**
127 * Get default variation
128 *
129 * @param int $productId
130 * @param string $context
131 *
132 * @return ?WC_Product_Variation
133 */
134 public static function getDefaultVariation( $productId, $context = 'view' ) {
135 $defaultVariationId = get_post_meta( $productId, '_tiered_pricing_default_variation_id', true );
136
137 $defaultVariation = $defaultVariationId ? wc_get_product( $defaultVariationId ) : false;
138
139 if ( ! ( $defaultVariation instanceof WC_Product_Variation ) ) {
140 $defaultVariation = null;
141 }
142
143 if ( 'edit' !== $context ) {
144 return apply_filters( 'tiered_pricing_table/product/default_variation', $defaultVariation, $productId );
145 }
146
147 return $defaultVariation;
148 }
149
150 public function productVariationsSearchHandler() {
151
152 check_ajax_referer( 'search-products', 'security' );
153
154 if ( empty( $term ) && isset( $_GET['term'] ) ) {
155 $term = (string) wc_clean( wp_unslash( $_GET['term'] ) );
156 }
157
158 if ( empty( $term ) ) {
159 return wp_send_json( array() );
160 }
161
162 $limit = 30;
163 $include = ! empty( $_GET['include'] ) ? intval( $_GET['include'] ) : false;
164
165 if ( ! $include ) {
166 return wp_send_json( array() );
167 }
168
169 $product = wc_get_product( $include );
170
171 if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) {
172 return wp_send_json( array() );
173 }
174
175 $results = array();
176
177 if ( $product instanceof WC_Product_Variable ) {
178 $variationsObjects = $product->get_available_variations( 'objects' );
179 $variations = array();
180 $rawVariations = array();
181
182 foreach ( $variationsObjects as $variation ) {
183 $rawVariations[ '_' . $variation->get_id() ] = rawurldecode( wp_strip_all_tags( $variation->get_attribute_summary() ) );
184 $variations = $rawVariations;
185 }
186
187 if ( count( $rawVariations ) > $limit ) {
188 $similarTextResults = array();
189
190 foreach ( $variationsObjects as $variation ) {
191 $similarTextResults[ '_' . $variation->get_id() ] = similar_text( strtolower( $variation->get_attribute_summary() ),
192 strtolower( $term ) );
193 }
194
195 asort( $similarTextResults );
196 $similarTextResults = array_reverse( $similarTextResults );
197 $similarTextResults = array_slice( $similarTextResults, 0, $limit );
198
199 $variations = array_intersect_key( $similarTextResults, $rawVariations );
200
201 }
202
203 foreach ( $variations as $key => $value ) {
204 $results[ str_replace( '_', '', $key ) ] = $rawVariations[ $key ];
205 }
206 }
207
208 return wp_send_json( $results );
209 }
210 }