Skip to content

Instantly share code, notes, and snippets.

@nazrul-kabir
Last active February 26, 2021 09:37
Show Gist options
  • Save nazrul-kabir/064731f054fd162d8ef4885324497351 to your computer and use it in GitHub Desktop.
Save nazrul-kabir/064731f054fd162d8ef4885324497351 to your computer and use it in GitHub Desktop.
WooCommerce: Add New “Tab” @ My Account Page
// How Use => Add the below function in your theme's functions.php file
// Output => https://snipboard.io/dDeMmK.jpg
/**
* @snippet WooCommerce Add New Tab NOTIFICATION @ My Account
* @author Nazrul Kabir(www.nazrulkabir.com)
* @compatible WooCommerce 3.5.7
*/
// ------------------
// 1. Register new endpoint to use for My Account page
// Note: Resave Permalinks or it will give 404 error
function myaccount_add_notification_endpoint() {
add_rewrite_endpoint( 'notification', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'myaccount_add_notification_endpoint' );
// ------------------
// 2. Add new query var
function myaccount_add_notification_query_vars( $vars ) {
$vars[] = 'notification';
return $vars;
}
add_filter( 'query_vars', 'myaccount_add_notification_query_vars', 0 );
// ------------------
// 3. Insert the new endpoint into the My Account menu
function myaccount_add_notification_link_my_account( $items ) {
$items['notification'] = 'Notification';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'myaccount_add_notification_link_my_account' );
// ------------------
// 4. Add content to the new endpoint
function myaccount_add_notification_content() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.parkup.app/bazaar/public/api/all_notifications");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if (!$output){
$error = curl_error($ch);
$info = curl_getinfo($ch);
die("cURL request failed, error = {$error}; info = " . print_r($info, true));
}
$data = '';
if(curl_errno($ch)){
echo 'error:' . curl_error($ch);
} else {
$data = json_decode($output, true);
}
echo '<h3>Notification List</h3>';
$notifications = '';
foreach( $data as $event ) {
$notifications .= '<div class="YOUR_CLASS_HERE" style="border:2px solid red;margin-bottom: 10px;padding: 1%;">';
$notifications .= '<h6>'.$event['title'].'</h6>';
$notifications .= '<p>'.$event['message'].'<br>';
$notifications .= '<mark class="order-date">'.$event['sending_time'].'</mark></p>';
if($event['full_image'] != '') {
$notifications .= '<img src="' . $event['full_image'] . '" class="attachment-full size-full lazyloaded" width="300" alt="'.$event['title'].'"<br>';
}
$notifications .= '</div>';
}
echo $notifications;
}
add_action( 'woocommerce_account_notification_endpoint', 'myaccount_add_notification_content' );
// Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment