Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
jorpdesigns / header-cart.php
Created July 12, 2021 10:25
Shortcode to display WooCommerce cart icon
<?php
add_shortcode( 'header_cart', 'header_cart' );
function header_cart() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
ob_start();
$cart_count = WC()->cart->get_cart_contents_count();
$cart_total = WC()->cart->get_cart_total();
$cart_url = wc_get_cart_url();
?>
@jorpdesigns
jorpdesigns / custom-order-details-order.php
Created July 12, 2021 10:28
Snippet to customize order details table ordering
<?php
// This example switches Shipping & Order Total rows
add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );
function reordering_order_item_totals( $total_rows, $order, $tax_display ){
// 1. saving the values of items totals to be reordered
$shipping = $total_rows['shipping'];
$order_total = $total_rows['order_total'];
@jorpdesigns
jorpdesigns / woocommerce-category-info.php
Created July 12, 2021 13:50
Snippet to get product category info on WooCommerce archive page
<?php
// GET CATEGORY OBJECT
$categoryObject = get_queried_object();
// GET CATEGORY ID
$categoryID = get_queried_object_id();
// GET CATEGORY ID FROM NAME
$categoryID = get_cat_ID( $categoryName );
@jorpdesigns
jorpdesigns / get-woocommerce-categories.php
Created July 12, 2021 13:51
Function to get WooCommerce product categories
<?php
function get_product_categories() {
// Parameters available here: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#parameters
$args = array(
'taxonomy' => 'product_cat',
'child_of' => 5, // Get child terms of category with ID 5
'parent' => 5, // Get direct-child terms of category with ID 5
);
@jorpdesigns
jorpdesigns / hide-category-on-shop.php
Created July 12, 2021 13:53
Snippet to hide category on WooCommerce Shop page
<?php
add_filter( 'get_terms', 'get_custom_category_terms', 10, 3 );
function get_custom_category_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
$hide_category = array( 217 ); // Ids of the category you don't want to display on the shop page
if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $hide_category ) ) {
@jorpdesigns
jorpdesigns / hide-products-from-catalogue.php
Created July 12, 2021 13:54
Snippet to hide products in specified category from WooCommerce catalogue
<?php
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'category'), // replace with your category slug
@jorpdesigns
jorpdesigns / modify-catalogue-sorting-order.php
Created July 12, 2021 13:57
Snippet to modify WooCommerce catalogue sorting option order
<?php
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'date' == $orderby_value ) { // Orderby values are date, menu_order, popularity, rating, date, price, price-desc
$args['order'] = 'asc';
}
return $args;
}
@jorpdesigns
jorpdesigns / category-default-ordering.php
Created July 12, 2021 14:01
Snippet to set default ordering for specific WooCommerce product category
<?php
// orderby values are date, menu_order, popularity, rating, date, price, price-desc
add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );
function custom_default_catalog_orderby() {
if ( is_product_category( array( 'category-1-slug', 'category-2-slug' ) ) ) { // Replace with your category slugs
return 'menu_order';
} else {
return 'date';
@jorpdesigns
jorpdesigns / remove-sorting-option.php
Created July 12, 2021 14:05
Snippet to remove WooCommerce catalogue sorting option
<?php
// orderby values are date, menu_order, popularity, rating, date, price, price-desc
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby', 20 );
function custom_woocommerce_catalog_orderby( $orderby ) {
unset($orderby["price"]); // Remove price option
return $orderby;
}
@jorpdesigns
jorpdesigns / product-row-page-amount.php
Created July 12, 2021 15:24
Snippet to set number of products per page and per row on WooCommerce archives
<?php
// SETS NUMBER OF PRODUCTS DISPLAYED PER PAGE
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 9999 );
function new_loop_shop_per_page( $cols ) {
$cols = 30;
return $cols;
}