Woocommerce - Display Notifications


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!!


WooCommerce - Display Cart Itmes Count with Cost.

If you wanted to display Cart items and total cost for individual user, use this code below

<?php
global $woocommerce;
// cart quantity
$qty = $woocommerce->cart->get_cart_contents_count();
// cart total
$total = $woocommerce->cart->get_cart_total();
// cart url
$cart_url = $woocommerce->cart->get_cart_url();
?>

<?php
// To Display Cart Quantity
if($qty>=1) { ?>
<div class="quantinty"><?php echo $qty; ?></div>
<?php } ?>

<?php
//To Display Total Cost
if($qty>=1)
echo '<span> '.$total.'</span>'; else { ?>
<span>Cart is Empty</span>
<?php } ?>

Wordpress Gzip Compression :: Page Speed

Here i have some basic information about Gzip Compression  for a website, This is a method which is used to reduce 65%  page size of your website.

Main advantage of Gzip Compression is:

Browsers checks for the Gzip availability in your website, if enabled server sends the encoded Gzip content to browser for rendering, which makes our website load faster then loading a normal webpage.

Add this code in your .htaccess file.

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

Wordpress Output Compression : Page speed

While cheeking your page speed in google insights you may come across an error "Enable Output Compression".

Here is the simple code to fix this error, add this code in your .htaccess file.

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>


Difference between Responsive Web Design (RWD) and Adaptive Web Design (AWD)



Responsive Web Design (RWD)


The key elements that make up a Responsive website are CSS3, media queries, the @media rule, and fluid grids that use percentages to create a flexible foundation. It is also fundamental to use flexible images, flexible videos, fluid type and EM’s instead of pixels. All of these key points allow the responsive website to modify its layout to suit the device.


Responsive design is client-side which means the page is sent to the device browser (the client), and the browser then modifies the appearance of the page in relation to the size of the browser window.

The definition of a responsive website is that it will fluidly alter its composition to improve the accessibility of content depending on the screen size of the browser window.



Adaptive Web Design (AWD)



Adaptive web design uses predefined layouts that have been carefully constructed for a variety of screen sizes. A particular layout is activated when the screen size of the device viewing the website is detected and matched with a style sheet.


Adaptive design is predominantly server side. This means that the web server does all of the work of detecting the various devices and loading the correct style sheet depending on the attributes of the device. As well as the layout changing depending on the size of the screen they can also change depending on conditions like whether the device has a retina display or not. The server can detect this and display high quality images for retina display devices like iPads and lower quality images for standard-definition displays.

The downsides to AWD are that initial construction is very costly as you have a lot of layouts to design. Also to maintain and update all of these layouts can be very time consuming which will therefore cost a lot of money.