标签: WordPress插件
- 作者帖子
- 2020年5月18日 - 上午2:52 #17585
fzze
管理员1.安装插件:file manager ,这个插件可以网站后台进入文件管理,
2.从网站后台左侧的菜单栏中找到wp file manager,点击进入。进入后依次点击
->wp-content
->themes
->child-theme(这里指的是子主题的名字)
->functions.php
3.右键functions.php ,在弹出的菜单中选择Code Editor ,将会打开这个文件。
4.在文件内容的下面粘贴以下代码:
// Add a custom field for price range to product in backend
add_action( ‘woocommerce_product_options_pricing’, ‘add_field_product_options_pricing’ );
function add_field_product_options_pricing() {
global $post;echo ‘
‘;
woocommerce_wp_text_input( array(
‘id’ => ‘_max_price_for_range’,
‘label’ => __(‘Max price for range’, ‘woocommerce’).’ (‘.get_woocommerce_currency_symbol().’)’,
‘placeholder’ => __(‘Set the max price for range’, ‘woocommerce’),
‘description’ => __(‘Set the max price for range, to activate it…’, ‘woocommerce’),
‘desc_tip’ => ‘true’,
));echo ‘
‘;
}// Save product custom field to database when submitted in Backend
add_action( ‘woocommerce_process_product_meta’, ‘save_product_options_custom_fields’, 30, 1 );
function save_product_options_custom_fields( $post_id ){
// Saving custom field value
if( isset( $_POST[‘_max_price_for_range’] ) ){
update_post_meta( $post_id, ‘_max_price_for_range’, sanitize_text_field( $_POST[‘_max_price_for_range’] ) );
}
}// Frontend: display a price range when the max price is set for the product
add_filter( ‘woocommerce_get_price_html’, ‘custom_range_price_format’, 10, 2 );
function custom_range_price_format( $price, $product ) {// Only for simple product type
if( $product->is_type(‘simple’) ){
// Get the max price for range
$max_price = get_post_meta( $product->get_id(), ‘_max_price_for_range’, true );if( empty($max_price) )
return $price; // exit$active_price = wc_get_price_to_display( $product, array( ‘price’ => $product->get_price() ) );
$price = sprintf( ‘%s – %s’, wc_price($active_price), wc_price($max_price) );
}
return $price;
}
5.保存并关闭
6.清除网站的缓存,此时代码已经更新完毕
7.选择一个产品,点击编辑该产品,在页面的右上角可以看到一个Screen Options,点击之后,选择Product data
8.下拉这个页面,找到Product data板块,此时在这个板块中的general中会多出一个Max price for range ($)栏目,在其中设置Regular price ($)和Max price for range ($)就可以了 - 作者帖子
- 抱歉,回复话题必需登录。