Skip to content

Instantly share code, notes, and snippets.

@smeletiou
smeletiou / Ajax_Product_Search.php
Created August 31, 2025 11:49
This adds a live AJAX product search bar (via the [woo_search] shortcode) that shows a dropdown of matching products—and, optionally, matching categories—as you type. It sends queries to admin-ajax.php, searches products by title/content and falls back to partial SKU matches, and renders up to num results with optional thumbnail, SKU, short desc…
<?
/*
*Product ajax search code
*[woo_search num="5" sku="on" description="on" price="on"]
*/
add_shortcode("woo_search", "woo_search_func");
function woo_search_func($atts)
{
$atts = shortcode_atts(
[
@smeletiou
smeletiou / Sort_By_SKU_Catalogue.php
Created August 30, 2025 21:19
This adds a “Sort by SKU” option to the shop/catalog sorting dropdown and makes it work. When selected, product queries are ordered ascending by the _sku meta value; it also registers the option so it appears in the dropdown and can be set as the default catalog sorting if desired.
<?php
//add deault sorting by sku in catalogue page
function add_sku_sorting_msr( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sku' == $orderby_value ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'asc'; // lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
$args['meta_key'] = '_sku';
@smeletiou
smeletiou / Show_SKU_shop.php
Created August 30, 2025 21:10
This adds the product SKU under each item’s title in the shop/catalog loop (shop, category, search pages) by hooking woocommerce_after_shop_loop_item_title and echoing a styled <span> with global $product->get_sku(). Note: if a product has no SKU, it will print “SKU: ” empty unless you add a check.
<?php
//show sku in shop
add_action( 'woocommerce_after_shop_loop_item_title', 'db_show_sku_msr', 10 );
function db_show_sku_msr(){
global $product;
echo '<span style="color:#000000; font-size: 18px;" class="custom_sku">SKU: ' . $product->get_sku() . '</span>';
}
@smeletiou
smeletiou / Show_SKU_To_Cart.php
Created August 30, 2025 21:10
This adds each product’s SKU under the item name on the cart and checkout pages. It hooks woocommerce_cart_item_name, gets the product from the cart line, and appends “SKU: …” if one exists; items without a SKU remain unchanged.
<?php
//show sku
function return_sku_msr( $product ){
$sku = $product->get_sku();
if ( ! empty( $sku ) ) {
return '<p>SKU: ' . $sku . '</p>';
} else {return '';}}
// This adds the SKU under cart/checkout item name
add_filter( 'woocommerce_cart_item_name', 'bbloomer_sku_cart_checkout_pages_msr', 9999, 3 );
function bbloomer_sku_cart_checkout_pages_msr( $item_name, $cart_item, $cart_item_key ) {
@smeletiou
smeletiou / Show_SKU_Email.php
Created August 30, 2025 21:07
This prints the product SKU under each line item in order emails and order detail pages (thank-you/My Account). It hooks woocommerce_order_item_meta_start and, if a product has a SKU, outputs “SKU: …” beneath the item name; products without a SKU show nothing.
<?php
function return_sku_msr( $product ){
$sku = $product->get_sku();
if ( ! empty( $sku ) ) {
return '<p>SKU: ' . $sku . '</p>';
} else {return '';}
}
//This adds SKU under order item table name EMAIL
add_action( 'woocommerce_order_item_meta_start', 'thankyou_order_email_pages_msr', 9999, 4 );
function thankyou_order_email_pages_msr( $item_id, $item, $order, $plain_text ) {
@smeletiou
smeletiou / Show_Images_On_Order_Email.php
Created August 30, 2025 21:07
This makes WooCommerce order emails include product thumbnails next to each line item by enabling show_image and setting the image size to 100×100; it only affects email item rows, not the storefront or admin pages.
<?
add_filter( 'woocommerce_email_order_items_args', 'order_with_product_images_msr', 10 );
function order_with_product_images_msr( $args ) {
$args['show_image'] = true;
$args['image_size'] = array( 100, 100 );
return $args;
}
@smeletiou
smeletiou / Show_Actual_Variation_Price.php
Created August 30, 2025 21:06
On variable product pages, this replaces the main price (.summary > p.price) with the selected variation’s price when a variation is chosen, hides the separate variation price block, and restores the original product price when the variation is cleared—so customers see a single, updated price instead of two.
<?
add_action( 'woocommerce_variable_add_to_cart', 'update_price_with_variation_price_msr' );
function update_price_with_variation_price_msr() {
global $product;
$price = $product->get_price_html();
wc_enqueue_js( "
$(document).on('found_variation', 'form.cart', function( event, variation ) {
if(variation.price_html) $('.summary > p.price').html(variation.price_html);
$('.woocommerce-variation-price').hide();
@smeletiou
smeletiou / Short_Products_Order_Email.php
Created August 30, 2025 21:04
This wraps each order line item’s product name in a link to the product page when the items are rendered (e.g., in order emails). It skips WooCommerce endpoint pages (My Account views) using is_wc_endpoint_url(), so on those pages the item name remains plain text.
<?
add_filter( 'woocommerce_order_item_name', 'add_email_order_item_permalink_msr', 10, 2 ); // Product name
function add_email_order_item_permalink_msr( $output_html, $item, $bool = false ) {
// Only email notifications
if( is_wc_endpoint_url() )
return $output_html;
$product = $item->get_product();
return '<a href="'.esc_url( $product->get_permalink() ).'">' . $output_html . '</a>';
}
@smeletiou
smeletiou / Search_By_SKU.php
Created August 30, 2025 21:04
This modifies WordPress search so typing a term in the site search will also match WooCommerce products by SKU. It looks up product IDs whose _sku contains the query (partial “LIKE” match) and injects those IDs into the main search SQL, so products with matching SKUs appear in results alongside normal title/content matches; if no SKU matches are…
<?php
function search_by_sku_msr( $search, $query_vars ) {
global $wpdb;
if(isset($query_vars->query['s']) && !empty($query_vars->query['s'])){
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
@smeletiou
smeletiou / Reorder_Account_Endpoints.php
Created August 30, 2025 21:03
This snippet replaces and reorders the WooCommerce “My Account” menu with a custom set: Account details, Orders, a custom “My previous products” endpoint (prev-prod), Addresses, and Logout. Only these items will appear (others are removed), and it assumes the prev-prod endpoint is registered elsewhere so the link works.
<?
//reorder my account page menu
add_filter( 'woocommerce_account_menu_items', 'menu_links_reorder_msr' );
function menu_links_reorder_msr( $menu_links ){
return array(
'edit-account' => __( 'Account details', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'prev-prod'=> __( 'My previous products', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' )