← All changes
|
src/Admin/ProductPage/AdvanceOptionsForVariableProduct.php
+104
-70
6.1.0
→
8.0.2
View file →
| @@ -9,27 +9,27 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @package TierPricingTable\Admin\Product |
| 11 | 11 | */ |
| 12 | 12 | class AdvanceOptionsForVariableProduct { |
| 13 | - | |
| 13 | + | |
| 14 | 14 | const PRODUCT_VARIATIONS_SEARCH_ACTION = 'woocommerce_json_search_tpt_product_variations'; |
| 15 | - | |
| 15 | + | |
| 16 | 16 | /** |
| 17 | 17 | * Register hooks |
| 18 | 18 | */ |
| 19 | 19 | public function __construct() { |
| 20 | 20 | add_action( 'wp_ajax_' . self::PRODUCT_VARIATIONS_SEARCH_ACTION, array( |
| 21 | - $this, | |
| 22 | - 'productVariationsSearchHandler', | |
| 21 | + $this, | |
| 22 | + 'productVariationsSearchHandler', | |
| 23 | 23 | ) ); |
| 24 | - | |
| 24 | + | |
| 25 | 25 | // Saving |
| 26 | 26 | add_action( 'woocommerce_process_product_meta', array( $this, 'saveOptions' ) ); |
| 27 | - | |
| 27 | + | |
| 28 | 28 | add_action( 'tiered_pricing_table/admin/advance_product_options', function ( $productId ) { |
| 29 | - | |
| 29 | + | |
| 30 | 30 | $product = wc_get_product( $productId ); |
| 31 | - | |
| 31 | + | |
| 32 | 32 | if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) { |
| 33 | 33 | return; |
| 34 | 34 | } |
| 35 | 35 | ?> |
| @@ -41,21 +41,19 @@ | ||
| 41 | 41 | ?> |
| 42 | 42 | </label> |
| 43 | 43 | |
| 44 | 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…', 'tier-pricing-table' ); | |
| 52 | - ?> | |
| 53 | - " | |
| 54 | - data-action="woocommerce_json_search_tpt_product_variations"> | |
| 55 | - | |
| 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…', 'tier-pricing-table' ); | |
| 51 | + ?>" | |
| 52 | + data-action="woocommerce_json_search_tpt_product_variations"> | |
| 53 | + | |
| 56 | 54 | <?php $default = self::getDefaultVariation( $productId, 'edit' ); ?> |
| 57 | - | |
| 55 | + | |
| 58 | 56 | <?php if ( $default ) : ?> |
| 59 | 57 | <option selected value="<?php echo esc_attr( $default->get_id() ); ?>"> |
| 60 | 58 | <?php echo esc_attr( $default->get_attribute_summary() ); ?> |
| 61 | 59 | </option> |
| @@ -60,28 +58,54 @@ | ||
| 60 | 58 | <?php echo esc_attr( $default->get_attribute_summary() ); ?> |
| 61 | 59 | </option> |
| 62 | 60 | <?php endif; ?> |
| 63 | 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> | |
| 64 | 73 | |
| 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 | + <?php | |
| 74 | 75 | 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' ), | |
| 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' ), | |
| 80 | 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 | |
| 81 | 105 | } ); |
| 82 | 106 | } |
| 83 | - | |
| 107 | + | |
| 84 | 108 | /** |
| 85 | 109 | * Save advanced options related to the variable product |
| 86 | 110 | * |
| 87 | 111 | * @param string|int $productId |
| @@ -86,27 +110,37 @@ | ||
| 86 | 110 | * |
| 87 | 111 | * @param string|int $productId |
| 88 | 112 | */ |
| 89 | 113 | public function saveOptions( $productId ) { |
| 90 | - | |
| 114 | + | |
| 91 | 115 | if ( wp_verify_nonce( true, true ) ) { |
| 92 | 116 | // as phpcs comments at Woo is not available, we have to do such a trash |
| 93 | 117 | $woo = 'Woo, please add ignoring comments to your phpcs checker'; |
| 94 | 118 | } |
| 95 | - | |
| 119 | + | |
| 96 | 120 | $data = $_POST; |
| 97 | - | |
| 121 | + | |
| 98 | 122 | $defaultVariation = ! empty( $data['_tiered_pricing_default_variation_id'] ) ? intval( $data['_tiered_pricing_default_variation_id'] ) : null; |
| 99 | 123 | $samePrices = ! empty( $data['_tiered_pricing_variable_product_same_prices'] ); |
| 100 | - | |
| 124 | + $mixAndMatch = isset( $data['_tiered_price_mix_and_match_minimum'] ) ? sanitize_text_field( $data['_tiered_price_mix_and_match_minimum'] ) : ''; | |
| 125 | + | |
| 101 | 126 | self::updateDefaultVariation( $productId, $defaultVariation ); |
| 102 | 127 | self::updateVariableProductSamePrices( $productId, $samePrices ); |
| 128 | + self::updateMixAndMatchMinimum( $productId, $mixAndMatch ); | |
| 103 | 129 | } |
| 104 | - | |
| 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 | + | |
| 105 | 139 | public static function isVariableProductSamePrices( $productId ): bool { |
| 106 | 140 | return 'yes' === get_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', true ); |
| 107 | 141 | } |
| 108 | - | |
| 142 | + | |
| 109 | 143 | public static function updateVariableProductSamePrices( $productId, bool $samePrices = false ) { |
| 110 | 144 | if ( $samePrices ) { |
| 111 | 145 | update_post_meta( $productId, '_tiered_pricing_variable_product_same_prices', 'yes' ); |
| 112 | 146 | } else { |
| @@ -112,9 +146,9 @@ | ||
| 112 | 146 | } else { |
| 113 | 147 | delete_post_meta( $productId, '_tiered_pricing_variable_product_same_prices' ); |
| 114 | 148 | } |
| 115 | 149 | } |
| 116 | - | |
| 150 | + | |
| 117 | 151 | public static function updateDefaultVariation( $variableProductId, $defaultVariationId = null ) { |
| 118 | 152 | if ( $defaultVariationId ) { |
| 119 | 153 | update_post_meta( $variableProductId, '_tiered_pricing_default_variation_id', $defaultVariationId ); |
| 120 | 154 | } else { |
| @@ -120,9 +154,9 @@ | ||
| 120 | 154 | } else { |
| 121 | 155 | delete_post_meta( $variableProductId, '_tiered_pricing_default_variation_id' ); |
| 122 | 156 | } |
| 123 | 157 | } |
| 124 | - | |
| 158 | + | |
| 125 | 159 | /** |
| 126 | 160 | * Get default variation |
| 127 | 161 | * |
| 128 | 162 | * @param int $productId |
| @@ -131,79 +165,79 @@ | ||
| 131 | 165 | * @return ?WC_Product_Variation |
| 132 | 166 | */ |
| 133 | 167 | public static function getDefaultVariation( $productId, $context = 'view' ) { |
| 134 | 168 | $defaultVariationId = get_post_meta( $productId, '_tiered_pricing_default_variation_id', true ); |
| 135 | - | |
| 169 | + | |
| 136 | 170 | $defaultVariation = $defaultVariationId ? wc_get_product( $defaultVariationId ) : false; |
| 137 | - | |
| 171 | + | |
| 138 | 172 | if ( ! ( $defaultVariation instanceof WC_Product_Variation ) ) { |
| 139 | 173 | $defaultVariation = null; |
| 140 | 174 | } |
| 141 | - | |
| 175 | + | |
| 142 | 176 | if ( 'edit' !== $context ) { |
| 143 | 177 | return apply_filters( 'tiered_pricing_table/product/default_variation', $defaultVariation, $productId ); |
| 144 | 178 | } |
| 145 | - | |
| 179 | + | |
| 146 | 180 | return $defaultVariation; |
| 147 | 181 | } |
| 148 | - | |
| 182 | + | |
| 149 | 183 | public function productVariationsSearchHandler() { |
| 150 | - | |
| 184 | + | |
| 151 | 185 | check_ajax_referer( 'search-products', 'security' ); |
| 152 | - | |
| 186 | + | |
| 153 | 187 | if ( empty( $term ) && isset( $_GET['term'] ) ) { |
| 154 | 188 | $term = (string) wc_clean( wp_unslash( $_GET['term'] ) ); |
| 155 | 189 | } |
| 156 | - | |
| 190 | + | |
| 157 | 191 | if ( empty( $term ) ) { |
| 158 | - return wp_send_json( array() ); | |
| 192 | + wp_send_json( array() ); | |
| 159 | 193 | } |
| 160 | - | |
| 194 | + | |
| 161 | 195 | $limit = 30; |
| 162 | 196 | $include = ! empty( $_GET['include'] ) ? intval( $_GET['include'] ) : false; |
| 163 | - | |
| 197 | + | |
| 164 | 198 | if ( ! $include ) { |
| 165 | - return wp_send_json( array() ); | |
| 199 | + wp_send_json( array() ); | |
| 166 | 200 | } |
| 167 | - | |
| 201 | + | |
| 168 | 202 | $product = wc_get_product( $include ); |
| 169 | - | |
| 203 | + | |
| 170 | 204 | if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) { |
| 171 | - return wp_send_json( array() ); | |
| 205 | + wp_send_json( array() ); | |
| 172 | 206 | } |
| 173 | - | |
| 207 | + | |
| 174 | 208 | $results = array(); |
| 175 | - | |
| 209 | + | |
| 176 | 210 | if ( $product instanceof WC_Product_Variable ) { |
| 177 | 211 | $variationsObjects = $product->get_available_variations( 'objects' ); |
| 178 | 212 | $variations = array(); |
| 179 | 213 | $rawVariations = array(); |
| 180 | - | |
| 214 | + | |
| 181 | 215 | foreach ( $variationsObjects as $variation ) { |
| 182 | 216 | $rawVariations[ '_' . $variation->get_id() ] = rawurldecode( wp_strip_all_tags( $variation->get_attribute_summary() ) ); |
| 183 | 217 | $variations = $rawVariations; |
| 184 | 218 | } |
| 185 | - | |
| 219 | + | |
| 186 | 220 | if ( count( $rawVariations ) > $limit ) { |
| 187 | 221 | $similarTextResults = array(); |
| 188 | - | |
| 222 | + | |
| 189 | 223 | foreach ( $variationsObjects as $variation ) { |
| 190 | 224 | $similarTextResults[ '_' . $variation->get_id() ] = similar_text( strtolower( $variation->get_attribute_summary() ), |
| 191 | - strtolower( $term ) ); | |
| 225 | + strtolower( $term ) ); | |
| 192 | 226 | } |
| 193 | - | |
| 227 | + | |
| 194 | 228 | asort( $similarTextResults ); |
| 195 | 229 | $similarTextResults = array_reverse( $similarTextResults ); |
| 196 | 230 | $similarTextResults = array_slice( $similarTextResults, 0, $limit ); |
| 197 | - | |
| 231 | + | |
| 198 | 232 | $variations = array_intersect_key( $similarTextResults, $rawVariations ); |
| 199 | - | |
| 233 | + | |
| 200 | 234 | } |
| 201 | - | |
| 235 | + | |
| 202 | 236 | foreach ( $variations as $key => $value ) { |
| 203 | 237 | $results[ str_replace( '_', '', $key ) ] = $rawVariations[ $key ]; |
| 204 | 238 | } |
| 205 | 239 | } |
| 206 | - | |
| 207 | - return wp_send_json( $results ); | |
| 240 | + | |
| 241 | + wp_send_json( $results ); | |
| 208 | 242 | } |
| 209 | 243 | } |