Skip to content

Instantly share code, notes, and snippets.

@sajdoko
Created February 4, 2021 11:56
Show Gist options
  • Save sajdoko/cb2b364f69dd433e398920b635eb467c to your computer and use it in GitHub Desktop.
Save sajdoko/cb2b364f69dd433e398920b635eb467c to your computer and use it in GitHub Desktop.
Limit page visibility only if customer has bought specific product
/**
* Kontrollon aksesin per pruktet
*/
function sajdoko_to_login_if_not_customer() {
global $post;
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->email;
$paketat_page_prod = [1439 => 1211, 1431 => 1210, 1441 => 1214, 1440 => 1212]; //page => product
// nese faqja eshte nje nga faqet e videove
if (array_key_exists($post->ID, $paketat_page_prod)) {
// nese eshte i loguar, ne te kundert redirect tek login
if (is_user_logged_in()) {
// nese nuk e ka blere produktin, redirect te bleje produktin
if (!wc_customer_bought_product_paid($customer_email, $user_id, $paketat_page_prod[$post->ID])) {
wp_redirect(esc_url(get_permalink($paketat_page_prod[$post->ID])));
}
} else {
// redirekt tek login
wp_redirect(esc_url(wp_login_url()), 307);
}
}
}
add_action('wp', 'sajdoko_to_login_if_not_customer');
function sajdoko_shfaq_link_page_to_dashboard() {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->email;
$paketat_prod_page = [1211 => 1439, 1210 => 1431, 1214 => 1441, 1212 => 1440]; //product => page
$produktet_e_blera = sajdoko_merr_produktet_e_blera();
if (is_array($produktet_e_blera) && !empty($produktet_e_blera)) {
echo '<h3>Paketat aktive:</h3>';
echo '<ul>';
foreach ($produktet_e_blera as $key => $value) {
if (wc_customer_bought_product_paid($customer_email, $user_id, $value)) {
$link = esc_url(get_permalink($paketat_prod_page[$value]));
if ($link) {
echo "<li><a href=\"$link\">".get_the_title( $paketat_prod_page[$value] )."</a></li>";
}
}
}
echo '</ul>';
}
}
add_action('woocommerce_account_dashboard', 'sajdoko_shfaq_link_page_to_dashboard', 10);
/**
* Kontrollon nese produkti eshte blere dhe paguar
*/
function wc_customer_bought_product_paid($customer_email, $user_id, $product_id) {
global $wpdb;
$result = apply_filters('woocommerce_pre_customer_bought_product', null, $customer_email, $user_id, $product_id);
if (null !== $result) {
return $result;
}
$transient_name = 'wc_cbp_' . md5($customer_email . $user_id . WC_Cache_Helper::get_transient_version('orders'));
if (false === ($result = get_transient($transient_name))) {
$customer_data = array($user_id);
if ($user_id) {
$user = get_user_by('id', $user_id);
if (isset($user->user_email)) {
$customer_data[] = $user->user_email;
}
}
if (is_email($customer_email)) {
$customer_data[] = $customer_email;
}
$customer_data = array_map('esc_sql', array_filter(array_unique($customer_data)));
$statuses = array_map('esc_sql', wc_get_is_paid_statuses());
if (sizeof($customer_data) == 0) {
return false;
}
$result = $wpdb->get_col("
SELECT im.meta_value FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
WHERE p.post_status IN ( 'wc-" . implode("','wc-", $statuses) . "' )
AND pm.meta_key IN ( '_billing_email', '_customer_user' )
AND im.meta_key IN ( '_product_id', '_variation_id' )
AND im.meta_value != 0
AND pm.meta_value IN ( '" . implode("','", $customer_data) . "' )
");
$result = array_map('absint', $result);
set_transient($transient_name, $result, DAY_IN_SECONDS * 30);
}
return in_array(absint($product_id), $result);
}
function sajdoko_merr_produktet_e_blera() {
// Get the current user Object
$current_user = wp_get_current_user();
// Check if the user is valid
if (0 == $current_user->ID) {
return;
}
//Create $args array
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
);
// Pass the $args to get_posts() function
$customer_orders = get_posts($args);
// loop through the orders and return the IDs
if (!$customer_orders) {
return;
}
$product_ids = array();
foreach ($customer_orders as $customer_order) {
$order = wc_get_order($customer_order->ID);
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item->get_product_id();
$product_ids[] = $product_id;
}
}
return $product_ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment