0

I am currently using a shortcodes on the My Account page (WooCommerce + Wordpress) to check if a user owns that course before displaying the image and link to access the content. If I use just 1 shortcode it works beautifully. But as soon as I add a second shortcode with a different product idea it just outputs a white page.

//Displays course module image
//[checkpurchase course="" url="" img=""]
function checkpurchase_shortcode( $atts, $content = null ) {

    //set default attributes and values
    $values = shortcode_atts( array(
        'course'    => '0',
        'url'   => '#',
        'img'   => '#',
    ), $atts );     
    $content = "";

    $courseid = esc_attr($values['course']);

    //check if they purchased the course through WooCommerce
    function check_coursepurchase($courseid) {
        global $woocommerce;
        $user_id = get_current_user_id();
        $current_user= wp_get_current_user();
        $customer_email = $current_user->email;

        //if they own the product return true
        if ( wc_customer_bought_product( $customer_email, $user_id, $courseid)) {
                return true;
        } 
        return false;
    }

    //if they own the product them display access
    if( check_coursepurchase($courseid) ){
        //make icon on the my account page for the course
        return '<a href="'. esc_attr($values['url']) .'"  target="_blank"><img src="'. esc_attr($values['img']) .'" border="0" /></a>'; 
    }
}
add_shortcode( 'checkpurchase', 'checkpurchase_shortcode' );
Amelia P
  • 9
  • 1
  • you're using `nested function` and getting fatal error on the second call. look at [this question](https://stackoverflow.com/q/1631535/8053001) and close your own as duplicate – Samvel Aleqsanyan Apr 19 '18 at 20:41
  • 1
    Awesome thanks! I knew it had to be an easy thing that I was missing. – Amelia P Apr 19 '18 at 20:47

1 Answers1

0

It works now that I stopped nesting functions. Thanks!

//check if they purchased the course through WooCommerce
function check_coursepurchase($courseid) {
    global $woocommerce;
    $user_id = get_current_user_id();
    $current_user= wp_get_current_user();
    $customer_email = $current_user->email;

    //if they own the product return true
    if ( wc_customer_bought_product( $customer_email, $user_id, $courseid)) {
        return true;
    } 
    return false;
}

//Displays course module image
//[checkpurchase course="" url="" img=""]
function checkpurchase_shortcode( $atts, $content = null ) {

    //set default attributes and values
    $values = shortcode_atts( array(
        'course'    => '0',
        'url'   => '#',
        'img'   => '#',
    ), $atts );     
    $content = "";

    $courseid = esc_attr($values['course']);

    //if they own the product them display access
    if( check_coursepurchase($courseid) ){
        //make icon on the my account page for the course
        $content = '<a href="'. esc_attr($values['url']) .'"  target="_blank"><img src="'. esc_attr($values['img']) .'" border="0" /></a>';
        return do_shortcode($content);
    }
}
add_shortcode( 'checkpurchase', 'checkpurchase_shortcode' );
Amelia P
  • 9
  • 1