WooCommerce: Search SKU and Product Title

Amend your search query with the following.

$query_withMeta_search = new WP_Query(
    array(
        "post_type" => "product",
        "s" => $search_query,
        "meta_query" => array(
                "key" => "_sku",
                "value" => $search_query,
                "compare" => "LIKE"
        )
    )
);

WooCommerce: Order a Free Sample of product on the product page

Need to offer a sample version of the product your selling? Then this guide will help you offer your woocommere store just that. It adds a button on the product page near the ‘Add to Cart’ button and will add the sample to the cart.

How it works

You will create a product called ‘Sample Product’ and offer it on every product in your store.

// 1. Display Free Sample Add to Cart 
// Note: change "4926" with Free Sample ID
 
add_action( 'woocommerce_single_product_summary', 'bbloomer_add_free_sample_add_cart', 35 );
 
function bbloomer_add_free_sample_add_cart() {
?>
	<form class="cart" method="post" enctype='multipart/form-data' action="">
		<button type="submit" name="add-to-cart" value="4926" class="button">Order Sample</button>
		<input type="hidden" name="free_sample" value="<?php the_ID(); ?>">
	</form>
<?php
}

// -------------------------
// 2. Add the custom field to $cart_item
 
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_store_free_sample_id', 10, 2 );
 
function bbloomer_store_free_sample_id( $cart_item, $product_id ) {
	if( isset( $_POST['free_sample'] ) ) {
	    $cart_item['free_sample'] = $_POST['free_sample'];
	}
	return $cart_item; 
}
 
// -------------------------
// 3. Preserve the custom field in the session
 
add_filter( 'woocommerce_get_cart_item_from_session', 'bbloomer_get_cart_items_from_session', 10, 2 );

function bbloomer_get_cart_items_from_session( $cart_item, $values ) {
	if ( isset( $values['free_sample'] ) ){
		$cart_item['free_sample'] = $values['free_sample'];
	}
	return $cart_item;
}
 
// -------------------------
// 4. Concatenate "Free Sample" with product name (CART & CHECKOUT)
 
add_filter( 'woocommerce_cart_item_name', 'bbloomer_alter_cart_item_name', 10, 3 );
 
function bbloomer_alter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
	if ( $product_name == "Free Sample" ) {
		$product = wc_get_product( $cart_item["free_sample"] );
		$product_name .=  " (" . $product->get_name() . ")";
	}
	return $product_name;
}
 
// -------------------------
// 5. Add "Free Sample" product name to order meta
// Note: this will show on thank you page, emails and orders
 
add_action('woocommerce_add_order_item_meta','bbloomer_save_posted_field_into_order', 10, 2);
 
function bbloomer_save_posted_field_into_order( $itemID, $values ) {
    if ( !empty( $values['free_sample'] )) {
        $product = wc_get_product( $values['free_sample'] );
        $product_name .=  " (" . $product->get_name() . ")";
        wc_add_order_item_meta( $itemID, 'Free sample for', $product_name );
    }
}

// -----------------------
// 6. Display Meta Data Sample on Cart

function iconic_display_engraving_text_cart( $item_data, $cart_item ) {

    if ( empty( $cart_item['free_sample'] ) ) {
        return $item_data;
    }

    $product = wc_get_product( wc_clean($cart_item['free_sample']) );

	$sample_title = $product->get_title();
 
    $item_data[] = array(
        'key'     => __( 'Sample for ' ),
        'value'   => $sample_title,
    );
 
    return $item_data;
}
 
add_filter( 'woocommerce_get_item_data', 'iconic_display_engraving_text_cart', 10, 2 );

WooCommerce: How to Bulk Edit Primary Category (Yoast Plugin Feature) using Pimwick’s PW Bulk Edit

Hi Guys,

This is how you bulk edit. Change the view to ‘Primary Categories’ as below. This will get rid of all the other columns we don’t need whilst bulk editing the primary category.

To apply a bulk edit to all your search results, clicked on the ‘Primary Category’ column heading and click ‘Edit All Checked Products’.

Then select what primary category you want to apply to all of them within the next window. Then click apply. Then click ‘save changes’

WooCommerce: Change the price formatting

Check out these two snippets. One for altering the price and the other for altering the price’s html format.

function return_custom_price($price, $product) {
    global $post, $blog_id;
    $price = get_post_meta($post->ID, '_regular_price');
    $post_id = $post->ID;
    $price = ($price[0]*2.5);
    return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

And this for price’s html format.

function cw_change_product_price_display( $price, $product ) {

	if ( has_term( 'fabrics', 'product_cat' ) ) {
       $price .= ' <span style="color: #ababab; font-weight: 100; font-size: 0.8em;"> per roll</span>';
	} elseif ( has_term( 'sofas', 'product_cat' ) ) {
       $price .= ' <span style="color: #ababab; font-weight: 100;">per sofa</span>';
	} else {
       echo 'test';
	}
    
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

WooCommerce: How to set Per Product discounts.

This is a guide to help you set trade discounts using the Dynamic Discount plugin. There are 2 types of trade discounts Category based, and per Product.

Category Discounts

Step 1: Go to Dynamic Pricing in the woo commerce menu.

Step 2: Click on Category tab. Enable the category using the switch on the left, then set the percentage.

Click on Category tab
enable the category and set the percentage discount

Per Product Discount

Step 1: Go to a product

Step 2: Click on the dynamic pricing tab, as seen below

Step 3: Click ‘Add Pricing Group’

Step 4: Enter 0 in the minimum amount, * in the maximum amount then select percentage and then enter the percentage amount , as seen below

Don’t forget to click update, or the changes wont be saved !

WooCommerce Dynamic Pricing: Product discount to override Category Discount

If you want product discounts to over ride the category discounts, then add the following snippet into your theme’s functions.php file.

When you enter the product bulk discount, you must add ‘0’ in minimum quantity, and ‘*’ in maximum quantity, then the discount information.

add_filter( 'woocommerce_dynamic_pricing_is_cumulative', '__return_false' );
add_filter( 'wc_dynamic_pricing_load_modules', 'custom_sort_dynamic_pricing_modules' );

function custom_sort_dynamic_pricing_modules( $modules ) {
   $module = $modules['simple_membership'];
   unset( $modules['simple_membership'] );
   $sorted = array(
      'simple_membership' => $module
   );
   return array_merge( $sorted, $modules );
}

WooCommerce: Get children of a grouped product

Here is a simple example.

/**
 * Test creating a new grouped product.
 *
 * @since 2.7.0
 */
function test_grouped_product_create()
{
    $simple_product = WC_Helper_Product::create_simple_product();
    $product = new WC_Product_Grouped();
    $product->set_children(array($simple_product->get_id()));
    $product->set_name('My Grouped Product');
    $product->save();
    $read_product = new WC_Product_Grouped($product->get_id());
    $this->assertEquals('My Grouped Product', $read_product->get_name());
    $this->assertEquals(array($simple_product->get_id()), $read_product->get_children());
}

 

WooCommerce: Exclude products from a particular category on the shop page

You need to add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

/**
* Exclude products from a particular category on the shop page
*/
function custom_pre_get_posts_query( $q ) {

$tax_query = (array) $q->get( 'tax_query' );

$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);

$q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );