How to not directly modify Woocommerce function?
Question
I have products with prices per package in eshop. But I need filtering products by prices per square meters on shop page. I created custom field with product price per square meters. I modified function price_filter_post_clauses to filter products by price per square meters. Function is located in includes/class-wc-query.php.
Original function:
public function price_filter_post_clauses( $args, $wp_query ) {
global $wpdb;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $wp_query->is_main_query() || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) {
return $args;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0;
$current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX;
// phpcs:enable WordPress.Security.NonceVerification.Recommended
/**
* Adjust if the store taxes are not displayed how they are stored.
* Kicks in when prices excluding tax are displayed including tax.
*/
if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
$tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class.
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( $tax_rates ) {
$current_min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_min_price, $tax_rates ) );
$current_max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_max_price, $tax_rates ) );
}
}
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] .= $wpdb->prepare(
' AND wc_product_meta_lookup.min_price >= %f AND wc_product_meta_lookup.max_price <= %f ',
$current_min_price,
$current_max_price
);
return $args;
}
Modified function:
public function price_filter_post_clauses( $args, $wp_query ) {
global $wpdb;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $wp_query->is_main_query() || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) {
return $args;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0;
$current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX;
// phpcs:enable WordPress.Security.NonceVerification.Recommended
/**
* Adjust if the store taxes are not displayed how they are stored.
* Kicks in when prices excluding tax are displayed including tax.
*/
if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
$tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class.
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( $tax_rates ) {
$current_min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_min_price, $tax_rates ) );
$current_max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_max_price, $tax_rates ) );
}
}
if (is_product_category()) {
$args['join'] = " LEFT JOIN {$wpdb->term_relationships} ON ($wpdb->posts.ID = {$wpdb->term_relationships}.object_id) LEFT JOIN {$wpdb->postmeta} ON $wpdb->posts.ID = {$wpdb->postmeta}.post_id ";
} else {
$args['join'] = " LEFT JOIN {$wpdb->postmeta} ON $wpdb->posts.ID = {$wpdb->postmeta}.post_id ";
}
$args['where'] .= $wpdb->prepare(
" AND {$wpdb->postmeta}.meta_key = 'cena_za_m2_po_zlave' AND ({$wpdb->postmeta}.meta_value BETWEEN %f AND %f) ",
$current_min_price,
$current_max_price
);
return $args;
}
Is it possible to modify this function without modifying it directly?
0
2 months
0 Answers
11 views
0
Leave an answer
You must login or register to add a new answer .