Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/e5b2c6ed00b0ccdff65d58379af8347c to your computer and use it in GitHub Desktop.
Save FrancoStino/e5b2c6ed00b0ccdff65d58379af8347c to your computer and use it in GitHub Desktop.
Insert content with shortcode if product is bought by customer into product
<?
/*
* Insert content with shortcode if product is bought by customer into product
*/
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
add_shortcode( 'p', 'wcr_shortcode' );
function wcr_shortcode( $atts, $content = null ){
global $product;
// Normalize attribute keys, lowercase
// $atts = array_change_key_case( (array) $atts, CASE_LOWER );
// Shortcode Attributes
/* $atts = shortcode_atts( array(
'pid' => ''
), $atts, 'wcr' );*/
$user = wp_get_current_user();
// start output
$html = '<div class="wcr-box">';
if ( current_user_can('administrator') || wc_customer_bought_product( $user->email, $user->ID, $product->get_id() ) ) {
// enclosing tags
if ( ! is_null($content) ) {
// secure output by executing the_content filter hook on $content
$html .= apply_filters( 'the_content', $content );
}
} else {
// User hasn't bought this product or is not an administrator
$html .= __("Please purchase the product first to see the content", "woocommerce");
}
// end box
$html .= '</div>';
// return output
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment