【发布时间】:2020-06-03 12:30:36
【问题描述】:
我使用«Exchange Rates Today»插件根据汇率显示价格(开发商显然不再支持它)。但是该插件仍然对我有用:产品的成本以美元指定,在此插件的设置中,指定货币汇率并在网站上显示价格,同时考虑到汇率。
woocommerce 的按价格过滤小部件存在问题。它考虑了管理面板中指定的价格,但不过滤网站上的价格。是否有可能以某种方式赶上这里,以便小部件不会过滤站点管理面板中指定的成本,而是这个动态价格?
插件代码如下:
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;
}
【问题讨论】:
标签: php wordpress woocommerce plugins