WooCommerce snippets in custom theme development
WooCommerce snippets in custom theme development
Here is the list of woocommerce snippets that you may find useful during WordPress theme development:
Declare WooCommerce support
add_action( 'after_setup_theme', 'woocommerce_support' ); function woocommerce_support() { add_theme_support( 'woocommerce' ); }
Check if on a page which uses WooCommerce templates
is_woocommerce()
Returns true if on a page which uses WooCommerce templates (cart and checkout are standard pages with shortcodes and thus are not included). All conditional tags here…
Remove the 'shop' title on the main shop page
add_filter( 'woocommerce_show_page_title', '__return_false' );
Overriding Templates via a Theme
You can edit these files in an upgrade-safe way using overrides. Copy it into a directory within your theme named /woocommerce, keeping the same file structure but removing the woocommerce/templates/ subdirectory.
If your theme has a woocommerce.php file, you will be unable to override the woocommerce/archive-product.php custom template in your theme, as woocommerce.php has priority over other template files.
Remove breadcrumbs
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
Get WooCommerce Page IDs
// Get The Page ID You Need get_option( 'woocommerce_shop_page_id' ); get_option( 'woocommerce_cart_page_id' ); get_option( 'woocommerce_checkout_page_id' ); get_option( 'woocommerce_pay_page_id' ); get_option( 'woocommerce_thanks_page_id' ); get_option( 'woocommerce_myaccount_page_id' ); get_option( 'woocommerce_edit_address_page_id' ); get_option( 'woocommerce_view_order_page_id' ); get_option( 'woocommerce_terms_page_id' ); // An Example function get_shop_featured_image() { if( is_shop() ) { $shop = get_option( 'woocommerce_shop_page_id' ); if( has_post_thumbnail( $shop ) ) { echo get_the_post_thumbnail( $shop ); } } }
Remove the product count functionality
This snippet will remove the product count functionality site-wide.
function woocommerce_result_count() { return; }
Remove the Catalog Ordering dropdown
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
List all Product categories with description
$taxonomy = 'product_cat'; $orderby = 'name'; $show_count = 0; // 1 for yes, 0 for no $pad_counts = 0; // 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 ); $all_categories = get_categories( $args ); foreach ($all_categories as $cat) { if($cat->category_parent == 0) { $category_id = $cat->term_id; echo $cat->name; echo $cat->description; } }
List all Products from specific Category
<?php $args = array( 'post_type' => 'product', 'posts_per_page' => -1 , 'product_cat' => $cat->slug , 'orderby' => 'ASC' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <div class="product"> <?php woocommerce_show_product_sale_flash( $post, $product ); ?> <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'full'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?> <h4><?php the_title(); ?></h4> <h5><?php the_content(); ?></h5> <span class="price"><?php echo $product->get_price_html(); ?></span> <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?> <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">View</a> </div> <?php endwhile; ?> <?php wp_reset_query(); ?>
Change (Add to Cart) text on Shop page
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); function woo_archive_custom_cart_button_text() { return __( 'buy now', 'woocommerce' ); }
Remove link on Featured Image (Single Product Page)
function custom_unlink_single_product_image( $html, $post_id ) {
return get_the_post_thumbnail( $post_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
}
add_filter('woocommerce_single_product_image_html', 'custom_unlink_single_product_image', 10, 2);
No comments