我知道还有另一个类似的问题,但是我的问题看起来很奇怪。我正在运行PHP脚本,并继续遇到以下错误:
注意:未定义的变量:d: my-webserver instantwp_4.3.1.1 iwpserver iwpserver htdocs wordpress wp-content themes themes mytheme mytheme functions.php 580
php
第580行看起来像这样:
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) echo '<div class="user-bought">♥ Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?</div>';
在这里完整代码:
add_action ( 'woocommerce_after_shop_loop_item',
'user_logged_in_product_already_bought', 30);
function user_logged_in_product_already_bought() {
if ( is_user_logged_in() ) {
global $product;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) echo '<div class="user-bought">♥ Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?
</div>';
}
}
?>
是否可以快速解决这些通知?非常感谢任何帮助
谢谢
在WooCommerce 3 中获得此功能的正确方法是:
add_action ( 'woocommerce_after_shop_loop_item', 'user_logged_in_product_already_bought', 30 );
function user_logged_in_product_already_bought() {
if ( ! is_user_logged_in() ) return;
global $product;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) )
echo '<div class="user-bought">♥ Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?</div>';
}
(在第580行中缺少$product->get_id()
,而不是未定义的$product_id
)
或使用global $post;
应该是:
add_action ( 'woocommerce_after_shop_loop_item', 'user_logged_in_product_already_bought', 30 );
function user_logged_in_product_already_bought() {
if ( ! is_user_logged_in() ) return;
global $post;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $post->ID ) )
echo '<div class="user-bought">♥ Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?</div>';
}
代码在您的活动子主题(或活动主题)或任何插件文件中的function.php文件中。
测试并有效。