Dynami c цена и фильтр по виджету цены woocommerce - PullRequest
0 голосов
/ 19 февраля 2020

Я использую плагин « Exchange Rates Today » для отображения цен, основанных на обменном курсе (разработчик, очевидно, больше не поддерживает его). Но плагин у меня все еще работает как надо: стоимость продукта указана в $, в настройках этого плагина указан курс обмена валют и цена отображается на сайте с учетом курса.

Проблема с виджетом «Фильтровать по цене» от woocommerce. Он учитывает цену, указанную в админ-панели, но не фильтрует цену на сайте. Можно ли как-то здесь зацепиться, чтобы виджет не фильтровал стоимость, указанную в админ-панели сайта, а вот эта динамика c цена?

Код плагина ниже:

add_action ('admin_menu', 'dynamic_price_button');
//Simple product
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
add_filter( 'woocommerce_price_filter_widget_min_amount', 'custom_price', 99, 2);
add_filter( 'woocommerce_price_filter_widget_max_amount', 'custom_price', 99, 2);
// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
// Variations
add_filter('woocommerce_variation_prices_price', 'custom_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_price', 99, 3 );
add_filter( 'woocommerce_variation_prices_sale_price',    'custom_price', 99, 3  );
// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 1 );

add_action( 'admin_init', 'register_mysettings' );

function register_mysettings () {
    register_setting( 'baw-settings-group', 'kurs' );
    register_setting( 'baw-settings-group', 'valuta' );
}

function custom_price ($price) {
    $int = floatval($price);
    $kurs=get_option('kurs');
    if ($kurs!='') {
        return $int*$kurs;
    } else  return  $int;
}

function add_price_multiplier_to_variation_prices_hash($hash){
    $hash[] = get_option('kurs');
    return $hash;
}

function dynamic_price_button () {
    add_submenu_page ('woocommerce', 'Курс сегодня', 'Курс сегодня', 'manage_options', 'dynamic_price', 'setting_page');
}

function setting_page () {
?>
<div class="wrap">
<h2>Курс на сегодня</h2>
<form method="post" action="options.php">
    <?php settings_fields( 'baw-settings-group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">Курс</th>
        <td><input type="text" name="kurs" value="<?php echo get_option('kurs'); ?>" /></td>
        </tr>               
    </table>

    <p class="submit">
    <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    </p>

</form>
</div>
<?php }

add_filter( 'woocommerce_product_query_meta_query', 'filter_function_name_3276', 10, 2 );
function filter_function_name_3276( $meta_query ){
    if ( isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) ) { // WPCS: input var ok, CSRF ok.
            $kurs=get_option('kurs');
            if (isset($_GET['min_price']) && $_GET['min_price']>0){
                $meta_query['price_filter']['value'][0] =  $meta_query['price_filter']['value'][0]/$kurs;
            };
            if (isset($_GET['max_price'])){
                $meta_query['price_filter']['value'][1] =  $meta_query['price_filter']['value'][1]/$kurs;
            };
            return $meta_query;
    }
    return $meta_query;
}

1 Ответ

0 голосов
/ 02 марта 2020

woocommerce \ включает \ class-w c -запрос. php Строки 529-530:

$current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0; // WPCS: input var ok, CSRF ok.
$current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX;

Изменить на:

$kurs=get_option('kurs');

$current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) / $kurs ) : 0; 
$current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) / $kurs ) : PHP_INT_MAX;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...