向WooCommerce中的非会员显示会员折扣价

在WooCommerce中,我有3个会员等级(银,金和白金),并且为更高的会员等级应用了更高的折扣率。

我想向所有人显示4种不同的价格(非会员,白银,黄金和白金),这样他们知道加入会员后可以在每种产品上节省多少。例如:

  • 常规价格:US $ 100

  • 银卡会员:$ 90

  • 金卡会员:$ 80

  • 白金会员:70美元

我尝试了下面的代码

function bsc_wc_memberships_members_only_product_price() {

    global $product;


    $user = wp_get_current_user();


    if ( !wc_memberships_is_user_active_member($user->ID, 'test') ) {


        $id = $product->get_id();

        $discount = get_post_meta( $id, 'member_price', true );

        $price = $product->get_price();


        echo 'Member price: $'.$total = $price - $discount;

    }


}

add_action( 'woocommerce_before_add_to_cart_button', 'bsc_wc_memberships_members_only_product_price' );

不幸的是,它并没有真正起作用……任何建议将不胜感激。


holdtom
浏览 161回答 1
1回答

慕盖茨4494581

有一个明显的错误:echo 'Member price: $'.$total = $price - $discount;而是应该只是:echo 'Member price: $'. $price - $discount;甚至更好:echo 'Member price: '. wc_price( $price - $discount );但是随着价格的显示,您需要使用一些不同和更完整的东西,例如:add_action( 'woocommerce_before_add_to_cart_button', 'bsc_wc_memberships_members_only_product_price' );function bsc_wc_memberships_members_only_product_price() {&nbsp; &nbsp; global $product;&nbsp; &nbsp; if ( ! wc_memberships_is_user_active_member( get_current_user_id(), 'test' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $discount&nbsp; &nbsp; &nbsp;= wc_get_price_to_display( $product, array('price' => $product->get_meta('member_price') ) );&nbsp; &nbsp; &nbsp; &nbsp; $price&nbsp; &nbsp; &nbsp; &nbsp; = wc_get_price_to_display( $product );&nbsp; &nbsp; &nbsp; &nbsp; $silver_price = $price - $discount;&nbsp; &nbsp; &nbsp; &nbsp; echo '<span class="silver-price">' . __('Member price') . ': ' . wc_price( $silver_price ) . '</span>';&nbsp; &nbsp; }}检查数据库表member_price下该产品的元键上是否确实存在自定义字段元值wp_postmeta。代码进入您的活动子主题(或活动主题)的function.php文件中。它应该更好地工作。
打开App,查看更多内容
随时随地看视频慕课网APP