显示会员在WooCommerce中向非会员折扣价格

问题描述 投票:2回答:1

在WooCommerce中,我有3个会员级别(白银,黄金和白金),我对更高的会员等级采用了更高的折扣率。

我想向每个人展示4种不同的价格(非会员,银牌,金牌和白金),以便他们知道如果他们加入会员,他们可以节省多少钱。例如:

  • 常规价格:100美元
  • 银牌会员:90美元
  • 金牌会员:80美元
  • 白金会员:70美元

我试过the code below

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' );

但不幸的是它并没有真正起作用......任何建议都将受到高度赞赏。

php wordpress woocommerce price woocommerce-memberships
1个回答
1
投票

有一个明显的错误:

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() {
    global $product;

    if ( ! wc_memberships_is_user_active_member( get_current_user_id(), 'test' ) ) {

        $discount     = wc_get_price_to_display( $product, array('price' => $product->get_meta('member_price') ) );
        $price        = wc_get_price_to_display( $product );

        $silver_price = $price - $discount;

        echo '<span class="silver-price">' . __('Member price') . ': ' . wc_price( $silver_price ) . '</span>';
    }
}

member_price数据库表下检查此产品的wp_postmeta元键上是否确实存在自定义字段元值。

代码位于活动子主题(或活动主题)的function.php文件中。它应该更好地工作。

© www.soinside.com 2019 - 2024. All rights reserved.