| 1 |
<?php namespace TierPricingTable\Admin\ProductManagers; |
| 2 |
|
| 3 |
use TierPricingTable\TierPricingTablePlugin; |
| 4 |
use WC_Product_Variable; |
| 5 |
use WC_Product_Variation; |
| 6 |
use WP_Post; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class VariationProduct |
| 10 |
* |
| 11 |
* @package TierPricingTable\Admin\Product |
| 12 |
*/ |
| 13 |
class AdvanceOptionsForVariableProduct { |
| 14 |
|
| 15 |
const PRODUCT_VARIATIONS_SEARCH_ACTION = 'woocommerce_json_search_tpt_product_variations'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register hooks |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_action( 'wp_ajax_' . self::PRODUCT_VARIATIONS_SEARCH_ACTION, array( |
| 22 |
$this, |
| 23 |
'productVariationsSearchHandler' |
| 24 |
) ); |
| 25 |
|
| 26 |
// Saving |
| 27 |
add_action( 'woocommerce_process_product_meta', array( $this, 'saveOptions' ) ); |
| 28 |
|
| 29 |
add_filter( 'tiered_pricing_table/admin/has_advance_product_options', function ( $has, WP_Post $post ) { |
| 30 |
$product = wc_get_product( $post->ID ); |
| 31 |
|
| 32 |
if ( ! $product ) { |
| 33 |
return $has; |
| 34 |
} |
| 35 |
|
| 36 |
return $has || TierPricingTablePlugin::isVariableProductSupported( $product ); |
| 37 |
}, 10, 2 ); |
| 38 |
|
| 39 |
add_action( 'tiered_pricing_table/admin/advance_product_options', function ( $productId ) { |
| 40 |
|
| 41 |
$product = wc_get_product( $productId ); |
| 42 |
|
| 43 |
if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
?> |
| 47 |
|
| 48 |
<p class="form-field _tiered_pricing_default_variation show_if_variable"> |
| 49 |
<label for="_tiered_pricing_default_variation_id"><?php esc_html_e( 'Default variation', 'tier-pricing-table' ); ?></label> |
| 50 |
|
| 51 |
<select class="wc-product-search" style="width: 50%;" |
| 52 |
id="_tiered_pricing_default_variation_id" |
| 53 |
data-allow_clear="true" |
| 54 |
name="_tiered_pricing_default_variation_id" |
| 55 |
data-include="<?php echo esc_attr( $productId ); ?>" |
| 56 |
data-placeholder="<?php esc_attr_e( 'Search for a variation…', 'tier-pricing-table' ); ?>" |
| 57 |
data-action="woocommerce_json_search_tpt_product_variations"> |
| 58 |
|
| 59 |
<?php $default = self::getDefaultVariation( $productId, 'edit' ); ?> |
| 60 |
|
| 61 |
<?php if ( $default ) : ?> |
| 62 |
<option selected value="<?php echo esc_attr( $default->get_id() ); ?>"> |
| 63 |
<?php echo esc_attr( $default->get_attribute_summary() ); ?> |
| 64 |
</option> |
| 65 |
<?php endif; ?> |
| 66 |
</select> |
| 67 |
|
| 68 |
<span class="description" |
| 69 |
style="clear: left; display: block; margin-top: 35px; margin-left: 0"><?php esc_html_e( 'The pricing will be shown for the selected variation before the attributes are selected on the product page', 'tier-pricing-table' ); ?> |
| 70 |
</span> |
| 71 |
</p> |
| 72 |
<?php |
| 73 |
|
| 74 |
/** |
| 75 |
* @var WC_Product_Variable $product |
| 76 |
*/ |
| 77 |
|
| 78 |
woocommerce_wp_checkbox( array( |
| 79 |
'id' => '_tiered_pricing_product_has_no_rules', |
| 80 |
'wrapper_class' => 'show_if_variable', |
| 81 |
'value' => wc_bool_to_string( self::productHasNoRules( $productId, 'edit' ) ), |
| 82 |
'label' => __( 'Product does not have tiered pricing rules', 'tier-pricing-table' ), |
| 83 |
'description' => __( 'Check this if the product does not have tiered pricing rules. This will increase performance as the plugin will not check every variation for rules', 'tier-pricing-table' ), |
| 84 |
'default' => 'no', |
| 85 |
) ); |
| 86 |
} ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Save advanced options related to the variable product |
| 91 |
* |
| 92 |
* @param int $productId |
| 93 |
*/ |
| 94 |
public function saveOptions( $productId ) { |
| 95 |
|
| 96 |
$data = $_POST; |
| 97 |
|
| 98 |
$hasNoRules = ! empty( $data['_tiered_pricing_product_has_no_rules'] ); |
| 99 |
$defaultVariation = ! empty( $data['_tiered_pricing_default_variation_id'] ) ? intval( $data['_tiered_pricing_default_variation_id'] ) : null; |
| 100 |
|
| 101 |
update_post_meta( $productId, '_tiered_pricing_product_has_no_rules', wc_bool_to_string( $hasNoRules ) ); |
| 102 |
update_post_meta( $productId, '_tiered_pricing_default_variation_id', $defaultVariation ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get default variation |
| 107 |
* |
| 108 |
* @param $productId |
| 109 |
* |
| 110 |
* @return WC_Product_Variation|null |
| 111 |
*/ |
| 112 |
public static function getDefaultVariation( $productId, $context = 'view' ) { |
| 113 |
$defaultVariationId = get_post_meta( $productId, '_tiered_pricing_default_variation_id', true ); |
| 114 |
|
| 115 |
$defaultVariation = $defaultVariationId ? wc_get_product( $defaultVariationId ) : false; |
| 116 |
|
| 117 |
if ( ! ( $defaultVariation instanceof WC_Product_Variation ) ) { |
| 118 |
$defaultVariation = null; |
| 119 |
} |
| 120 |
|
| 121 |
if ( $context !== 'edit' ) { |
| 122 |
return apply_filters( 'tiered_pricing_table/product/default_variation', $defaultVariation, $productId ); |
| 123 |
} |
| 124 |
|
| 125 |
return $defaultVariation; |
| 126 |
} |
| 127 |
|
| 128 |
public static function productHasNoRules( $productId, $context = 'view' ) { |
| 129 |
$hasNoRules = get_post_meta( $productId, '_tiered_pricing_product_has_no_rules', true ) === 'yes'; |
| 130 |
|
| 131 |
if ( $context !== 'edit' ) { |
| 132 |
return apply_filters( 'tiered_pricing_table/product/product_has_no_rules', $hasNoRules, $productId ); |
| 133 |
} |
| 134 |
|
| 135 |
return $hasNoRules; |
| 136 |
} |
| 137 |
|
| 138 |
public function productVariationsSearchHandler() { |
| 139 |
|
| 140 |
check_ajax_referer( 'search-products', 'security' ); |
| 141 |
|
| 142 |
if ( empty( $term ) && isset( $_GET['term'] ) ) { |
| 143 |
$term = (string) wc_clean( wp_unslash( $_GET['term'] ) ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( empty( $term ) ) { |
| 147 |
wp_die(); |
| 148 |
} |
| 149 |
|
| 150 |
$limit = 30; |
| 151 |
$include = ! empty( $_GET['include'] ) ? intval( $_GET['include'] ) : false; |
| 152 |
|
| 153 |
if ( ! $include ) { |
| 154 |
return wp_send_json( array() ); |
| 155 |
} |
| 156 |
|
| 157 |
$product = wc_get_product( $include ); |
| 158 |
|
| 159 |
if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) { |
| 160 |
return wp_send_json( array() ); |
| 161 |
} |
| 162 |
|
| 163 |
$results = array(); |
| 164 |
|
| 165 |
if ( $product instanceof WC_Product_Variable ) { |
| 166 |
$variationsObjects = $product->get_available_variations( 'objects' ); |
| 167 |
$variations = array(); |
| 168 |
$rawVariations = array(); |
| 169 |
|
| 170 |
foreach ( $variationsObjects as $variation ) { |
| 171 |
$rawVariations[ '_' . $variation->get_id() ] = rawurldecode( wp_strip_all_tags( $variation->get_attribute_summary() ) ); |
| 172 |
$variations = $rawVariations; |
| 173 |
} |
| 174 |
|
| 175 |
if ( count( $rawVariations ) > $limit ) { |
| 176 |
$similarTextResults = array(); |
| 177 |
|
| 178 |
foreach ( $variationsObjects as $variation ) { |
| 179 |
$similarTextResults[ '_' . $variation->get_id() ] = similar_text( strtolower( $variation->get_attribute_summary() ), strtolower( $term ) ); |
| 180 |
} |
| 181 |
|
| 182 |
asort( $similarTextResults ); |
| 183 |
$similarTextResults = array_reverse( $similarTextResults ); |
| 184 |
$similarTextResults = array_slice( $similarTextResults, 0, $limit ); |
| 185 |
|
| 186 |
$variations = array_intersect_key( $similarTextResults, $rawVariations ); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
foreach ( $variations as $key => $value ) { |
| 191 |
$results[ str_replace( '_', '', $key ) ] = $rawVariations[ $key ]; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return wp_send_json( $results ); |
| 196 |
} |
| 197 |
} |