Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / display-subcategories.php
Last active July 26, 2021 16:32
Shortcode to display subcategories under a WooCommerce parent category
<?php
// Shortcode example: [display_subcats id="55"]. Replace 55 with your parent category ID
add_shortcode('display_subcats', 'display_subcats');
function display_subcats( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'id' => ''
@jorpdesigns
jorpdesigns / check-customer-subscription.php
Created July 11, 2021 18:05
Function to check if a customer has a WooCommerce Subscription
<?php
function has_woocommerce_subscription($the_user_id, $the_product_id, $the_status) {
if ( empty($the_user_id) ) {
$the_user_id = get_current_user_id();
}
if (wcs_user_has_subscription( $the_user_id, $the_product_id, $the_status)) {
return true;
}
}
@jorpdesigns
jorpdesigns / remove-downloadable-product-link.php
Created July 11, 2021 18:00
Snippet to remove WooCommerce product link in Downloads table and display product title only