Skip to content

Instantly share code, notes, and snippets.

@hirejordansmith
Last active July 3, 2017 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hirejordansmith/3bb0023325842daad4e8 to your computer and use it in GitHub Desktop.
Save hirejordansmith/3bb0023325842daad4e8 to your computer and use it in GitHub Desktop.
Useful WooCommerce Snippets
<?php
add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
$cart_count = sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
$cart_total = $woocommerce->cart->get_cart_total();
if ($args->theme_location == 'primary') {
$items .= "<li><i class='fa fa-shopping-cart'></i><a class='cart-contents' href='$cart_url'> $cart_total - $cart_count</a></li>";
}
return $items;
}
?>
<?php
function hjs_woocommerce_categories() {
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 1; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
); ?>
<?php $all_categories = get_categories( $args );
//print_rr($all_categories);
?>
<ul>
<?php
foreach ($all_categories as $cat) {
//print_rr($cat);
if($cat->category_parent == 0) {
$category_id = $cat->term_id; ?>
<li><a href="<?php echo get_term_link($cat->slug, 'product_cat'); ?>"><?php echo $cat->name; ?></a> <span class="product-items"> - <?php echo $cat->count; ?> Items</span></li>
<?php
/* $args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
} */ ?>
<?php }
} ?>
</ul>
<?php }
?>
<?php
function custom_sidebar_shopping_cart() {
global $woocommerce;
$cart_total = $woocommerce->cart->get_cart_total();
$cart_url = $woocommerce->cart->get_cart_url();
//print_rr($cart_total);
//var_dump($cart_total);
if ( sizeof( $woocommerce->cart->cart_contents ) == 0 ) { ?>
<div class="cart-contents empty">
<div class="cart-wrap">
<i class="fa fa-shopping-cart"></i>
<span class="no-items">You don't have anything in your cart yet.</span>
</div>
<div class="shop-button-wrap">
<a class="shop-button" href="<?php echo home_url(); ?>/shop" title="<?php _e('View your shopping cart', 'woothemes'); ?>"> Keep Shopping <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<?php } else { ?>
<div class="cart-contents">
<div class="cart-wrap">
<span class="left-side">
<i class="fa fa-shopping-cart"></i>
<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?>
</span>
<span class="right-side"><?php echo $cart_total; ?></span>
</div>
<div class="shop-button-wrap">
<a class="shop-button checkout" href="<?php echo $cart_url; ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"> Checkout <i class="fa fa-arrow-circle-right"></i></a>
</div>
</div>
<?php }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment