Bootstrap Templates Bundle

Sunday, April 19, 2015

How to show number of items in cart and total in WordPress Header or Menu Section



Add this code to you Functions.php file 


//check if woocommerce plugin is activated
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
function is_woocommerce_activated() {
if ( class_exists( 'woocommerce' ) ) {
return true;
}
else {
return false;
}
}
}


//showing cart header menu item
function andro_cart_menu_item() {
if(is_woocommerce_activated()){
?>

<li class="border"><a class="cart collapsed" data-target=".shopping-bag" href="javascript:;">
<span class="cart-icon"><?php esc_attr_e('Cart','andro');?></span>

<?php
global $woocommerce;
$my_cart_count = $woocommerce->cart->cart_contents_count;

echo '<span class="cart-number-box';
if ($my_cart_count > 0) {
echo ' active';
}
echo '">';echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
echo '</span>';
?>
</a></li>
<?php }
}

function andro_cart_shopping_bag(){
if(is_woocommerce_activated()){
?>
<div class="shopping-bag">
<?php
if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) {
the_widget( 'WC_Widget_Cart', 'title=' );
} else {
the_widget( 'WooCommerce_Widget_Cart', 'title=' );
} ?>
</div>
<?php
}
}

add_filter( 'add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );

if ( ! function_exists( 'woocommerce_header_add_to_cart_fragment' ) ) {
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>

<?php
echo '<span class="cart-number-box';
$my_cart_count = $woocommerce->cart->cart_contents_count;
if ($my_cart_count > 0) {
echo ' active';
}
echo '">';
?>
<?php
echo sprintf( _n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count );
?></span>


<?php

$fragments['span.cart-number-box'] = ob_get_clean();

return $fragments;
} // End woocommerce_header_add_to_cart_fragment()
}



After that add this code to your header.php 

To show the cart count:
<?php echo sprintf (_n( '%d item', '%d items', $woocommerce->cart->cart_contents_count ), $woocommerce->cart->cart_contents_count ); ?> - <?php echo $woocommerce->cart->get_cart_total(); ?>

or 

<?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>


To show the cart bag or cart Widget: 

<?php  if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>


Sample Header: 

<div class="top-cart">
<div id="cart" class="btn-group btn-block">
<button type="button" data-toggle="dropdown" data-loading-text="Loading..." class="btn btn-inverse btn-block btn-lg dropdown-toggle">
<span class="top-cart-contain">
<span class="title-cart"></span>
<span id="cart-total">

<?php echo sprintf (_n( '%d item', '%d items', $woocommerce->cart->cart_contents_count ), $woocommerce->cart->cart_contents_count ); ?> - <?php echo $woocommerce->cart->get_cart_total(); ?>

<?php if (class_exists('Woocommerce')) {andro_cart_menu_item();} ?>
</span>
<i class="fa fa-caret-down"></i>
</span>
</button>


<ul class="dropdown-menu pull-right">
<li class="arrow-cart"></li>
<li>
<?php  if (class_exists('Woocommerce')) {andro_cart_shopping_bag();} ?>
</li>
</ul>
</div>
</div>

Monday, April 6, 2015

Add a custom tab to woocommerce single product page

Add this below code to your functions.php file. Then chnage the line that shows

echo '<p>Here\'s your new product tab.</p>';

You can create a meta field for single product and echo that field here.

echo '<p>Your Meta box output code here</p>';




add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {

// Adds the new tab

$tabs['test_tab'] = array(
'title' => __( 'New Product Tab', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);

return $tabs;

}
function woo_new_product_tab_content() {

// The new tab content

echo '<h2>New Product Tab</h2>';
echo '<p>Here\'s your new product tab.</p>';

}




#Use the following snippet to change the tab order

add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {

$tabs['reviews']['priority'] = 5; // Reviews first
$tabs['description']['priority'] = 10; // Description second
$tabs['additional_information']['priority'] = 15; // Additional information third

return $tabs;
}

Saturday, April 4, 2015

How to Override woocommerce widgets

Here, I tried to show how to override WooCommerce Price Filter widget.

At first Add this below code to your functions.php file.  copy the  class-wc-widget-price-filter.php   file from wp-content\plugins\woocommerce\includes\widgets  to your theme file and include that by
include_once  or include get_template_directory()


/*-------------------------------
 Override woocommerce product categories widgets
---------------------------------*/

add_action( 'widgets_init', 'override_woocommerce_widgets', 15 );

function override_woocommerce_widgets() {
  // Ensure our parent class exists to avoid fatal error

    if ( class_exists( 'WC_Widget_Price_Filter' ) ) {
    unregister_widget( 'WC_Widget_Price_Filter' );

    include_once( 'inc/custom-widgets/fengo-class-wc-widget-price-filter.php' ); 
//or 
//include get_template_directory() . '/inc/custom-widgets/fengo-class-wc-widget-price-filter.php';

    register_widget( 'Fengo_WC_Widget_Price_Filter' );
  }    

}

Wednesday, March 25, 2015

Add search option to wordpress theme



You can place a search box in your wordpress theme by using
<?php get_search_form(); ?>

You can also add

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

After that change the id,class or style according to your need

Thursday, March 19, 2015

How to add TGM Plugin to WordPress Theme



TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference pre-packaged plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.

http://tgmpluginactivation.com/

Tuesday, February 3, 2015

Display the number of Products Sold in WooCommerce

//To display the number of units sold for a product, add the following snippet of code to your theme’s functions.php file

<?php
add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 );
function wc_product_sold_count() {
global $product;
$units_sold = get_post_meta( $product->id, 'total_sales', true );
echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}
?>

//or Where You want to show

<?php echo get_post_meta($post->ID, 'total_sales', true); ?> 



Monday, February 2, 2015

php if else sample code

This code is for those, who are facing problem with php if else


Sample 1: 


<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>

Sample 2: 


<?php
if(condition) { ?> 
html code to run if condition is true
<?php } ?>


Sample 3: 


<?php
     $var=1;
     if($var == 2) {
          echo “The if statement is true”;
         // or any html code to run if condition is true
     } else {
          echo “The if statement is not true”;
        // or any html code to run if condition is true
     }

?>


Sample 4:  

 The if…elseif…else Statement



<?php 
 $var=1; 
 $var2=1; 
 if($var == 2) { 
 echo “The if statement is true”; 
 } 
elseif($var == $var2) { 
 echo “The second if statement is true”; 
 } 
else { 
 echo “None of the first if statement where true”; 
 } 
?>


Sample 5:

<?php 
if ( $number_three == 3 ) {
echo "The if statement evaluated to true";
} else {
echo "The if statement evaluated to false";
}
?>

Wokiee React eCommerce Template