【发布时间】:2015-05-13 14:02:08
【问题描述】:
我束手无策。我需要根据客户的订阅在客户的帐户页面上显示某些产品。示例:如果客户订阅了产品 #517,但随后切换到产品 #5910,我希望他们的电子邮件和帐户页面显示与 #5910 关联的数据 ($menu_listing)。
在他们的电子邮件中,我可以使用 $order->get_items() 来实现:
add_filter( 'woocommerce_email_order_meta', 'add_hgf_to_order');
function add_hgf_to_order( $order_id ) {
global $posts;
global $woocommerce;
$order = new WC_Order( $order_id );
$user_id = (int)$order->user_id;
$items = $order->get_items();
foreach ($items as $item) {
if ( $item['product_id' ]== 517 || $item['product_id' ]== 5938 ) {
$menu_listing = "menu_listing";
} elseif ( $item['product_id' ]== 5910 || $item['product_id' ]== 5915 ) {
$menu_listing = "menu_listing_c";
} elseif ( $item['product_id' ]== 5934 || $item['product_id' ]== 5926 ) {
$menu_listing = "menu_listing_a";
}
}
return $order_id;
}
虽然 $order->get_items() 在“woocommerce_email_order_meta”功能上正常工作,但它在客户的“我的帐户”页面上不起作用。相反,我能够使用 wc_customer_bought_product() 将数据调用到页面上,但是它没有显示客户最近的购买:
add_filter( 'woocommerce_after_my_account', 'hgf_customer_orders' );
function hgf_customer_orders() {
global $current_user;
global $posts;
global $woocommerce;
$email = $current_user->email;
if ( wc_customer_bought_product( $email, $current_user->ID, 517 ) || wc_customer_bought_product( $email, $current_user->ID, 5938) ) {
$menu_listing = "menu_listing";
} elseif ( wc_customer_bought_product( $email, $current_user->ID, 5910 ) || wc_customer_bought_product( $email, $current_user->ID, 5915) ) {
$menu_listing = "menu_listing_c";
} elseif ( wc_customer_bought_product( $email, $current_user->ID, 5934 ) || wc_customer_bought_product( $email, $current_user->ID, 5926) ) {
$menu_listing = "menu_listing_a";
} else {
return null;
}
}
它正在做什么(使用顶部的示例)显示产品 #517 而不是 #5910。所以我要么需要能够显示 wc_customer_bought_product() 的最新购买,要么让 $order->get_items() 函数在用户的“我的帐户”页面上正常工作。
谁能帮我解决这个问题?
【问题讨论】:
标签: php woocommerce