WordPress实现会员优惠等功能教程

做商城网站,经常会对会员设置一定的优惠,比如打八折优惠,想要实现这个功能,我们只要需要做到两点:修改会员浏览商品时的 ...

做商城网站,经常会对会员设置一定的优惠,比如打八折优惠,想要实现这个功能,我们只要需要做到两点:修改会员浏览商品时的商品价格、修改会员购物车中和结账时的商品价格。今天就分享一下WordPress实现会员优惠等功能教程。

WordPress实现会员优惠等功能教程 (https://www.wpzt.net/) WordPress基础教程 第1张

add_filter( ‘woocommerce_get_price_html’, function ( $price_html, $product ) {

// 只在前端修改

if ( is_admin() ) return $price_html;

// 只在设置了商品价格时才修改,免费产品直接返回

if ( ” === $product->get_price() ) return $price_html;

// 如果用户登录,打八折

if ( wc_current_user_has_role( ‘customer’ ) ) {

$orig_price = wc_get_price_to_display( $product );

$price_html = wc_price( $orig_price * 0.80 );

}

return $price_html;

}, 9999, 2 );

下面的代码实现了会员登录后,修改购物车中的产品价格的功能。用户添加好商品去结账时,订单价格会按照购物车中显示的商品价格计算。

add_action( ‘woocommerce_before_calculate_totals’, function ( $cart ) {

if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) return;

if ( did_action( ‘woocommerce_before_calculate_totals’ ) >= 2 ) return;

// 如果客户没有登录,不显示价格

if ( ! wc_current_user_has_role( ‘customer’ ) ) return;

// 遍历购物车项目,每个项目都打八折

foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {

$product = $cart_item[‘data’];

$price = $product->get_price();

$cart_item[‘data’]->set_price( $price * 0.80 );

}

}, 9999 );

声明 本站上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。无意侵害您的权益,请发送邮件至 柒主题 或点击右侧 私信:站长 反馈,我们将尽快处理。

给TA打赏
共{{data.count}}人
人已打赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索