我想在结账页面上显示一个新的表格,显示用户在ACF Wordpress WooCommerce产品上节省了多少钱



我在一个WordPress网站上工作,用户可以在那里购买会员资格,我想显示如果用户是会员,他可以节省多少钱。我正在使用ACF。

我的问题是如何在产品旁边的结账页面上显示ACF字段???

WordPress一般和WooCommerce都有一个叫做"钩子";以帮助我们添加自定义数据。

本文是WooCommerce Hooks 的介绍和示例

这是WooCommerce结账页面上挂钩的虚拟版本。

因此,根据您的需要,您可以使用woocommerce_review_order_before_cart_contents挂钩,将此代码添加到主题中的functions.php文件中:

一般添加到签出页面

add_action( 'woocommerce_review_order_before_cart_contents', 'add_member_info' );
function add_member_info() {
echo '<div class=”member-message”>Become a member will save $100</div>';
}

从产品中的自定义字段获取数据

I。在单个产品中显示

function cw_change_product_price_display( $price, $product ) {
$my_field = get_field('member_save', $product->ID);
$price .= " | Member save $$my_field";
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display', 10, 2);
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display', 10, 2);

II。在签出页面中显示

  1. 将产品的自定义数据添加到Card
add_filter( 'woocommerce_add_cart_item_data', function ( $cartItemData, $productId, $variationId ) {
$member_saved = get_field('member_save', $productId);
$cartItemData['myCustomData'] = $member_saved;
return $cartItemData;
}, 10, 3 );
add_filter( 'woocommerce_get_cart_item_from_session', function ( $cartItemData, $cartItemSessionData, $cartItemKey ) {
if ( isset( $cartItemSessionData['myCustomData'] ) ) {
$cartItemData['myCustomData'] = $cartItemSessionData['myCustomData'];
}
return $cartItemData;
}, 10, 3 );
  1. 在购物车和结账页面中显示数据
add_filter( 'woocommerce_get_item_data', function ( $data, $cartItem ) {
if ( isset( $cartItem['myCustomData'] ) ) {
$data[] = array(
'name' => 'Member saved',
'value' => $cartItem['myCustomData']
);
}
return $data;
}, 10, 2 );

最新更新