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

209 lines 6.7 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="
50 <?php
51 esc_attr_e( 'Search for a variation&hellip;', 'tier-pricing-table' );
52 ?>
53 "
54 data-action="woocommerce_json_search_tpt_product_variations">
55
56 <?php $default = self::getDefaultVariation( $productId, 'edit' ); ?>
57
58 <?php if ( $default ) : ?>
59 <option selected value="<?php echo esc_attr( $default->get_id() ); ?>">
60 <?php echo esc_attr( $default->get_attribute_summary() ); ?>
61 </option>
62 <?php endif; ?>
63 </select>
64
65 <span class="description" style="clear: left; display: block; margin-top: 35px; margin-left: 0">
66 <?php
67 esc_html_e( 'The pricing for the selected variation will be shown before the attributes are selected on the product page. Attributes will not be pre-selected.',
68 'tier-pricing-table' );
69 ?>
70 </span>
71 </p>
72
73 <?php
74 woocommerce_wp_checkbox( array(
75 'id' => '_tiered_pricing_variable_product_same_prices',
76 'value' => wc_bool_to_string( self::isVariableProductSamePrices( $productId ) ),
77 'label' => __( 'Variations have the same prices', 'tier-pricing-table' ),
78 'description' => __( 'Do not load prices or reset the pricing table when selecting attributes. The default variation has the same prices as all variations. Selecting default variation is required.',
79 'tier-pricing-table' ),
80 ) );
81 } );
82 }
83
84 /**
85 * Save advanced options related to the variable product
86 *
87 * @param string|int $productId
88 */
89 public function saveOptions( $productId ) {
90
91 if ( wp_verify_nonce( true, true ) ) {
92 // as phpcs comments at Woo is not available, we have to do such a trash
93 $woo = 'Woo, please add ignoring comments to your phpcs checker';
94 }
95
96 $data = $_POST;
97
98 $defaultVariation = ! empty( $data['_tiered_pricing_default_variation_id'] ) ? intval( $data['_tiered_pricing_default_variation_id'] ) : null;
99 $samePrices = ! empty( $data['_tiered_pricing_variable_product_same_prices'] );
100
101 self::updateDefaultVariation( $productId, $defaultVariation );
102 self::updateVariableProductSamePrices( $productId, $samePrices );
103 }
104
105 public static function isVariableProductSamePrices( $productId ): bool {
106 return 'yes' === get_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', true );
107 }
108
109 public static function updateVariableProductSamePrices( $productId, bool $samePrices = false ) {
110 if ( $samePrices ) {
111 update_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', 'yes' );
112 } else {
113 delete_post_meta( $productId, '_tiered_pricing_variable_product_same_prices' );
114 }
115 }
116
117 public static function updateDefaultVariation( $variableProductId, $defaultVariationId = null ) {
118 if ( $defaultVariationId ) {
119 update_post_meta( $variableProductId, '_tiered_pricing_default_variation_id', $defaultVariationId );
120 } else {
121 delete_post_meta( $variableProductId, '_tiered_pricing_default_variation_id' );
122 }
123 }
124
125 /**
126 * Get default variation
127 *
128 * @param int $productId
129 * @param string $context
130 *
131 * @return ?WC_Product_Variation
132 */
133 public static function getDefaultVariation( $productId, $context = 'view' ) {
134 $defaultVariationId = get_post_meta( $productId, '_tiered_pricing_default_variation_id', true );
135
136 $defaultVariation = $defaultVariationId ? wc_get_product( $defaultVariationId ) : false;
137
138 if ( ! ( $defaultVariation instanceof WC_Product_Variation ) ) {
139 $defaultVariation = null;
140 }
141
142 if ( 'edit' !== $context ) {
143 return apply_filters( 'tiered_pricing_table/product/default_variation', $defaultVariation, $productId );
144 }
145
146 return $defaultVariation;
147 }
148
149 public function productVariationsSearchHandler() {
150
151 check_ajax_referer( 'search-products', 'security' );
152
153 if ( empty( $term ) && isset( $_GET['term'] ) ) {
154 $term = (string) wc_clean( wp_unslash( $_GET['term'] ) );
155 }
156
157 if ( empty( $term ) ) {
158 return wp_send_json( array() );
159 }
160
161 $limit = 30;
162 $include = ! empty( $_GET['include'] ) ? intval( $_GET['include'] ) : false;
163
164 if ( ! $include ) {
165 return wp_send_json( array() );
166 }
167
168 $product = wc_get_product( $include );
169
170 if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) {
171 return wp_send_json( array() );
172 }
173
174 $results = array();
175
176 if ( $product instanceof WC_Product_Variable ) {
177 $variationsObjects = $product->get_available_variations( 'objects' );
178 $variations = array();
179 $rawVariations = array();
180
181 foreach ( $variationsObjects as $variation ) {
182 $rawVariations[ '_' . $variation->get_id() ] = rawurldecode( wp_strip_all_tags( $variation->get_attribute_summary() ) );
183 $variations = $rawVariations;
184 }
185
186 if ( count( $rawVariations ) > $limit ) {
187 $similarTextResults = array();
188
189 foreach ( $variationsObjects as $variation ) {
190 $similarTextResults[ '_' . $variation->get_id() ] = similar_text( strtolower( $variation->get_attribute_summary() ),
191 strtolower( $term ) );
192 }
193
194 asort( $similarTextResults );
195 $similarTextResults = array_reverse( $similarTextResults );
196 $similarTextResults = array_slice( $similarTextResults, 0, $limit );
197
198 $variations = array_intersect_key( $similarTextResults, $rawVariations );
199
200 }
201
202 foreach ( $variations as $key => $value ) {
203 $results[ str_replace( '_', '', $key ) ] = $rawVariations[ $key ];
204 }
205 }
206
207 return wp_send_json( $results );
208 }
209 }