欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress实现会员优惠等功能教程

作者 : 自学哈 本文共939个字,预计阅读时间需要3分钟 2022-11-26 共151人阅读
也想出现在这里? 联系我们

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

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

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress实现会员优惠等功能教程
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号