Display notification in Woocommerce when a user checkouts a product.
Here is the code
1. We need get the order count
<?php
global $woocommerce;
$order_count = wc_get_customer_order_count( $current_user->ID );
if($order_count>=1) { ?>
<div class="quantity"><?php echo $order_count; ?></div>
<?php } ?>
2. Alright!!
We got our order count, but we should allow user to show his notifications only when logged in. So we have to restrict user.
<?php if ( is_user_logged_in() ) { // If user logged in ?>
<a href="URL">Show Notifications</a> // "URL" Add your Notifications Page URL
<?php }
else { // If user not logged in , We will Navigate him to My account page ?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>">Show Notifications</a>
<?php } ?>
3. Finally!!
We have to display all the order details in the notifications page.
<ul class="notifications">
<?php
$customer_orders = get_posts( array(
'numberposts' => -1, // No of notifications to display
'meta_key' => '_customer_user', // Meta Key to get customer details
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
foreach($customer_orders as $customer_order)
{
echo "<li>"; ?>
<a href="<?php bloginfo('url'); ?>/my-account/view-order/<?php echo $customer_order->ID; ?>"> <?php echo $customer_order->post_title; ?> </a>
<?php
if($customer_order->post_status == "wc-cancelled"){$orderclass = "alert-danger";}else {$orderclass = "alert-info";} // We can add some styles by adding a dynamic class based on the order status, "$orderclass" is the class varaiable ?>
<span class="<?php echo $orderclass; ?>"> <?php echo $customer_order->post_status; ?> </span>
<?php echo "</li>"; } ?>
</ul>
4. Done!!
No comments:
Post a Comment