对于粘贴了短代码的Product模板,我得到了一个500的错误。
我创建了一个短代码,用于在用户购买特定产品时显示视频。
视频URL来自ACF字段
// Shortcode for Video from ACF
function wpc_elementor_shortcode( $atts ) {
global $product;
if ( ! is_user_logged_in() ) return;
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
echo get_field('video_tutorial');
}
}
add_shortcode( 'purchase_video_tutorial', 'wpc_elementor_shortcode');
在前端,它工作,但在元素构建器它显示500错误,如果短代码[purchase_video*_*tutorial]放置。
调试显示上面代码的错误:
Uncaught Error: Call to a member function get_id() on null in ..... functions.php:133
我需要添加什么到php代码来修复元素页面生成器的这个错误?
验证意味着检查你正在访问的对象是否为空,像这样:
if ( $product != null && wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
此外,短代码应该返回字符串,而不是打印出来
echo get_field('video_tutorial');
return get_field('video_tutorial');
同样,wc_customer_bought_product
,第一个参数是客户的电子邮件
$current_user = wp_get_current_user();
$customer_email = $current_user->email;
wc_customer_bought_product($customer_email, get_current_user_id(), $product->get_id() )