24 Easy Woocommerce Hacks For Each Retailer

Let’s hack your Woocommerce store a bit. That’s, on this submit I’ll present you 24 easy Woocommerce hacks that can save your money and time.

Now, with a view to make this work, add your chosen snippets proven to your little one theme’s features.php file or higher but, use a snippet supervisor like Code Snippets.

There may be additionally a WpCodeBox plugin, which is my favourite code snippets supervisor for WordPress. This can be a premium plugin and for those who’re , then seize WPCodeBox with a pleasant 20% low cost right here (SAVE 20% Coupon WPSH20).

Video: 24 Easy Woocommerce Hacks For Each Retailer

On this video I’ll present you how you can add all these snippets and the way will they have an effect on your website.

Methods to activate Woocommerce catalog mode and add enquiry type on single product pages?

This snipper right here under prompts catalog mode and shows a product enquiry type on single product web page

add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS

remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS

// Add an enquiry type on Woocommerce single product web page
add_action( 'woocommerce_single_product_summary', 'single_product_message', 20 );
 
perform single_product_message() {
    echo '<p class="woocommerce-message">Ship us an enquiry </p>';
	echo do_shortcode(''); // Your contact type shortcode goes right here. 
}

Methods to Add conditional Woocommerce product class web page messages

With the assistance of this code snippet right here you may add a message to all Woocommerce product class pages.

<span function="button" tabindex="0" data-code="// Customized message on Woocommerce store web page and all class pages
add_action( 'woocommerce_before_main_content', 'shop_message', 20 );
perform shop_message() {
echo '<p class="woocommerce-message">Free delivery for all orders

// Customized message on Woocommerce store web page and all class pages
add_action( 'woocommerce_before_main_content', 'shop_message', 20 );
perform shop_message() {
echo '<p class="woocommerce-message">Free delivery for all orders</p>'; // Change your message right here
}

Methods to add message to particular Woocommerce class web page?

If you want to add a message to the scpecific Woocommerce product class web page (Storage catogory, for instance) then use this code as a substitute.

<span function="button" tabindex="0" data-code="// Message for particular class
add_action( 'woocommerce_before_main_content', 'category_message', 20 );
perform category_message() {
if ( is_product_category( 'storage' ) ) { // Change your vategory slug right here
echo '<p class="woocommerce-message">Estimated delivery time: 2-3 weeks

// Message for particular class
add_action( 'woocommerce_before_main_content', 'category_message', 20 );
perform category_message() {
 if ( is_product_category( 'storage' ) ) { // Change your vategory slug right here
echo '<p class="woocommerce-message">Estimated delivery time: 2-3 weeks</p>'; // Change your message right here
}
}

Methods to add one message to particular Woocommerce class web page and one other message to all different class pages?

Now lets add a one message to the Sotrage class web page and one other message to all different classes.

<span function="button" tabindex="0" data-code="// One message for particular class (storage) and one other for all different classes
add_action( 'woocommerce_before_shop_loop', 'conditional_message', 1 );
perform conditional_message() {
if ( is_product_category( 'storage' ) ) { // Change your vategory slug right here
echo '<p class="woocommerce-message">Estimated supply time: 2-3 weeks</p>';// Change your Storage class message right here
} else {
echo '<p class="woocommerce-message">Estimated supply time: 1 week

// One message for particular class (storage) and one other for all different classes
add_action( 'woocommerce_before_shop_loop', 'conditional_message', 1 );
perform conditional_message() {
if ( is_product_category( 'storage' ) ) { // Change your vategory slug right here
echo '<p class="woocommerce-message">Estimated supply time: 2-3 weeks</p>';// Change your Storage class message right here 
} else {
echo '<p class="woocommerce-message">Estimated supply time: 1 week</p>'; // Change your shop-wide message right here
}
}

Methods to take away class product depend in product archives?

As you most likely have seen the class loop has a product depend proven subsequent to the class identify. This code will disguise Woocommerce product depend in product archives.

add_filter( 'woocommerce_subcategory_count_html', '__return_false' );

Methods to take away class product depend in product archives?

As you most likely have seen the class loop has a product depend proven subsequent to the class identify. This code will disguise Woocommerce product depend in product archives.

add_filter( 'woocommerce_subcategory_count_html', '__return_false' );

Methods to Show the Low cost Share on the Woocommerce Sale Badge?

By default Woocommerce reveals a badge with the “Sale” textual content. When you would kike to point out a reduction proportion as a substitute, then use this code right here under.

<span function="button" tabindex="0" data-code="add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );

perform sale_badge_percentage() {
international $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'easy' ) ) {
$max_percentage = ( ( $product->get_regular_price() – $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} elseif ( $product->is_type( 'variable' ) ) {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$value = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $value != 0 && ! empty( $sale ) ) $proportion = ( $value – $sale ) / $value * 100;
if ( $proportion > $max_percentage ) {
$max_percentage = $proportion;
}
}
}
if ( $max_percentage > 0 ) echo "<span class='onsale'>-" . spherical($max_percentage) . "%

add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );
 
perform sale_badge_percentage() {
   international $product;
   if ( ! $product->is_on_sale() ) return;
   if ( $product->is_type( 'easy' ) ) {
      $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
   } elseif ( $product->is_type( 'variable' ) ) {
      $max_percentage = 0;
      foreach ( $product->get_children() as $child_id ) {
         $variation = wc_get_product( $child_id );
         $value = $variation->get_regular_price();
         $sale = $variation->get_sale_price();
         if ( $value != 0 && ! empty( $sale ) ) $proportion = ( $value - $sale ) / $value * 100;
         if ( $proportion > $max_percentage ) {
            $max_percentage = $proportion;
         }
      }
   }
   if ( $max_percentage > 0 ) echo "<span class='onsale'>-" . spherical($max_percentage) . "%</span>"; // If you need to point out -40% off then add textual content after % signal
}

Methods to Show “NEW” Badge for Woocommerce Latest Merchandise which can be lower than 30 days outdated?

Step 1: Add the code snippet right here under

<span function="button" tabindex="0" data-code="// New badge for current merchandise
add_action( 'woocommerce_before_shop_loop_item_title', 'new_badge', 3 );

perform new_badge() {
international $product;
$newness_days = 30; // Variety of days the badge is proven
$created = strtotime( $product->get_date_created() );
if ( ( time() – ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
echo '<span class="new-badge onsale">' . esc_html__( 'NEW', 'woocommerce' ) . '

// New badge for current merchandise
add_action( 'woocommerce_before_shop_loop_item_title', 'new_badge', 3 );
          
perform new_badge() {
   international $product;
   $newness_days = 30; // Variety of days the badge is proven
   $created = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
      echo '<span class="new-badge onsale">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
   }
}

Step 2: Customise your badge

Listen thought that the CSS proven right here under might have tweaking and it rely in your theme. So, tweak it accordingly.

// “NEW” Badge for Woocommerce Latest Merchandise which can be lower than 30 days outdated
.woocommerce ul.merchandise li.product .new-badge.onsale {
	background: #ffcc00;
	high: 50px;
	z-index: 10;
	left: 0px;
	colour: #000;
	font-weight: 700;
	text-transform: uppercase;
	font-size: 0.9em;
	border-radius: 0px;
	min-width: 60px;
	padding-left: 19px !essential;
}
span.new-badge.onsale:after {
	border: 5px stable #ffcc00;
border-color: clear clear #ffcc00 #ffcc00;
	border-width: 9px 6px;
	place: absolute;
	proper: -10px;
	backside: 0;
	content material: '';
}
span.new-badge.onsale:earlier than {
border: 5px stable #ffcc00;
    border-color: #ffcc00 clear clear #ffcc00;
    border-width: 9px 6px;
    place: absolute;
    proper: -10px;
    high: 0;
    content material: '';
}

If you need to point out it as a vertical ribbon then add this piece of CSS.

span.new-badge.onsale {
min-width: 60px;
-moz-transform: rotate(90deg);
    -webkit-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    rework: rotate(90deg);
}

Methods to show {custom} product badges with checking a checkbox?

Subsequent, let’s add a checkbox at your single product edit web page. When you test this then the textual content “Free delivery” is proven on the one product web page under the title. See these screenshots right here under, and also you’ll see the way it may even show it for the badge we’re going to create within the subsequent chapter.

How to display custom product badges on WooCommerce?
How to display custom product badges on WooCommerce?

If you need to vary the textual content proven within the badge, then simply modify it in line 34. Additionally, change the textual content in traces 7 and 9 accordingly.

Now, let’s show {custom} product badges with checking a checkbox.

<span function="button" tabindex="0" data-code="// Step 1: Add checkbox

add_action( 'woocommerce_product_options_general_product_data', 'checkbox_badge' );

perform checkbox_badge() {
woocommerce_wp_checkbox( array(
'id' => 'checkbox_badge',
'class' => '',
'label' => 'Show free delivery badge'
)
);
}

// Step 2: Save checkbox choice

add_action( 'save_post', 'save_checkbox_badge_selection' );

perform save_checkbox_badge_selection( $product_id ) {
if ( outlined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['checkbox_badge'] ) ) {
update_post_meta( $product_id, 'checkbox_badge', $_POST['checkbox_badge'] );
} else delete_post_meta( $product_id, 'checkbox_badge' );
}

// Step 3: Show {custom} badge at single product web page

add_action( 'woocommerce_single_product_summary', 'display_checkbox_badge', 7 );

perform display_checkbox_badge() {
international $product;
if ( get_post_meta( $product->get_id(), 'checkbox_badge', true ) ) {
echo '
<div class="custom-badge-1">Free delivery

// Step 1: Add checkbox 

add_action( 'woocommerce_product_options_general_product_data', 'checkbox_badge' );        
  
perform checkbox_badge() {           
woocommerce_wp_checkbox( array( 
'id' => 'checkbox_badge', 
'class' => '', 
'label' => 'Show free delivery badge'
) 
);      
}

// Step 2: Save checkbox choice
  
add_action( 'save_post', 'save_checkbox_badge_selection' );
  
perform save_checkbox_badge_selection( $product_id ) {
    if ( outlined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['checkbox_badge'] ) ) {
            update_post_meta( $product_id, 'checkbox_badge', $_POST['checkbox_badge'] );
    } else delete_post_meta( $product_id, 'checkbox_badge' );
}

// Step 3: Show {custom} badge at single product web page
  
add_action( 'woocommerce_single_product_summary', 'display_checkbox_badge', 7 );
  
perform display_checkbox_badge() {
    international $product;     
    if ( get_post_meta( $product->get_id(), 'checkbox_badge', true ) ) {
        echo '
<div class="custom-badge-1">Free delivery</div>'; // Change this textual content if wanted
    }
}

Customized CSS for the product badge.

/* Show {custom} product badges with checking a checkbox */
.custom-badge-1 {
  font-size: 13px;
	colour: #fff;
	font-weight: 600;
	background: #E54C60;
	border: 2px stable #E54C60;
	text-align: middle;
	padding: 7px;
	show: inline;
}
.entry-summary .value {
	margin-top: 1em;
}

Methods to show Woocommerce inventory quantity and inventory standing on Woocommerce archive pages?

Generally there’s a must show Woocommerce inventory quantity and inventory standing on Woocommerce arhcive pages. Subsequently, this snippet will aid you out.

It’ll show Woocommerce inventory amoutn and inventory statuses like this:

  • 25 in inventory
  • Out of inventory
  • In inventory
  • Accessible on backorder
  • For variable merchandise it could be “In inventory (some gadgets)”
<span function="button" tabindex="0" data-code="//* Enqueue scripts
add_action( 'wp_enqueue_scripts', 'wpsh_stock_status_archive', 11 );
perform wpsh_stock_status_archive() {

// Activate clip kinds
wp_enqueue_style( 'wpsh-stock-status-archive-style',
plugins_url( 'clip-style.css', __FILE__ ), array(),
'1.0.0'
);
}

//* Add inventory standing to archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_add_stock_status_archive', 3 );
perform wpsh_add_stock_status_archive() {

international $product;
$availability = $append = null;

// Add standing for single merchandise
if( $product->is_type( 'easy' ) ) {

$availability = $product->get_availability();
$class = $availability[ 'class' ];
$output = $availability[ 'availability' ];
}

// Add standing for variable merchandise
elseif( $product->is_type( 'variable' ) ) {

$standing = array();

// Get standing class for every variation
foreach ( $product->get_children() as $child_id ) {

$variation = $product->get_child( $child_id );
$availability = $variation->get_availability();

// Abandon if inventory administration is disabled on any variation
if( ! array_filter( $availability ) )
return;

$standing[] = $availability[ 'class' ];
}

/**
* Compile closing output and sophistication primarily based on
* availability courses set by WooCommerce
*/
if( in_array( 'in-stock', $standing ) ) {
$output = __( 'In inventory', 'wp-clips' );
$class = 'in-stock';
}
elseif( in_array( 'available-on-backorder', $standing ) ) {
$output = __( 'Accessible on backorder', 'wp-clips' );
$class = 'available-on-backorder';
}
elseif( in_array( 'out-of-stock', $standing ) ) {
$output = __( 'Out of inventory', 'wp-clips' );
$class = 'out-of-stock';
}

// Append output if some gadgets out of inventory or out there on backorder
if( ( in_array( 'available-on-backorder', $standing ) && $class == 'in-stock' ) ||
( in_array( 'out-of-stock', $standing ) && $class != 'out-of-stock' ) )
$append = ' ' . __( '(some gadgets)', 'wp-clips' );
}

// Output provided that set
if( isset( $availability ) ){
echo '<span class="archive-stock ' . esc_attr( $class ) . '">' . esc_html( $output ) . esc_html( $append ) . '

//* Enqueue scripts
add_action( 'wp_enqueue_scripts', 'wpsh_stock_status_archive', 11 );
perform wpsh_stock_status_archive() {

	// Activate clip kinds
	wp_enqueue_style(  'wpsh-stock-status-archive-style', 
						plugins_url( 'clip-style.css', __FILE__ ), array(),
						'1.0.0' 
	);
}

//* Add inventory standing to archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_add_stock_status_archive', 3 );
perform wpsh_add_stock_status_archive() {

    international $product;
	$availability = $append = null;

	// Add standing for single merchandise
	if( $product->is_type( 'easy' ) ) {

		$availability = $product->get_availability();
		$class = $availability[ 'class' ];
		$output = $availability[ 'availability' ];
	}

	// Add standing for variable merchandise
	elseif( $product->is_type( 'variable' ) ) {

		$standing = array();

		// Get standing class for every variation
		foreach ( $product->get_children() as $child_id ) {
			
				$variation = $product->get_child( $child_id );
				$availability = $variation->get_availability();
				
				// Abandon if inventory administration is disabled on any variation
				if( ! array_filter( $availability ) )
					return;

				$standing[] = $availability[ 'class' ];
		}

		/**
		 * Compile closing output and sophistication primarily based on
		 * availability courses set by WooCommerce
		 */
		if( in_array( 'in-stock', $standing ) ) {
			$output = __( 'In inventory', 'wp-clips' );
			$class = 'in-stock';
		}
		elseif( in_array( 'available-on-backorder', $standing ) ) {
			$output = __( 'Accessible on backorder', 'wp-clips' );
			$class = 'available-on-backorder';
		}
		elseif( in_array( 'out-of-stock', $standing ) ) {
			$output = __( 'Out of inventory', 'wp-clips' );
			$class = 'out-of-stock';
		}

		// Append output if some gadgets out of inventory or out there on backorder
		if( ( in_array( 'available-on-backorder', $standing ) && $class == 'in-stock' ) ||
			( in_array( 'out-of-stock', $standing ) && $class != 'out-of-stock' ) )
			$append = ' ' . __( '(some gadgets)', 'wp-clips' );
	}

	// Output provided that set 
	if( isset( $availability ) ){
		echo '<span class="archive-stock ' . esc_attr( $class ) . '">' . esc_html( $output ) . esc_html( $append ) . '</span>';	
	}
}

As with the opposite snippets you may customise it with this piece of CSS.

/* Show Woocommerce inventory quantity and inventory standing on Woocommerce archive pages */
.archive-stock  {
	font-size: 13px;
	margin: 5px 0px 10px 0px;
}

Methods to change Woocommerce add to cart button textual content if product is in backorder?

IThere are people who find themselves confused concerning the backorders and completely different Woocommerce inventory statuses. This result in to drawback the place custome didn’t understand that the product was out there on backorder (meaning out of inventory) and ordered it realizing that it’ll ship quickly.

Subsequently, you could need to change Woocommerce add to cart button textual content just for backorder merchandise. Now, for those who mix it with the {custom} area (proven above) then the top consequence will appear to be this:

How to change Woocommerce add to cart button text if product is in backorder?

Seems good, isn’t it? So, use this code snippet.

// adjustments Woocommerce Single product web page add to cart button solely textual content if product is in backorder
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_ninja_change_backorder_button', 10, 2 );
perform wc_ninja_change_backorder_button( $textual content, $product ){
	if ( $product->is_on_backorder( 1 ) ) {
		$textual content = __( 'Pre-order', 'woocommerce' );
	}
	return $textual content;
}

// adjustments Woocommerce class web page add to cart button solely textual content if product is in backorder
add_filter( 'woocommerce_product_add_to_cart_text', 'wc_ninja_change_backorder_button1', 10, 2 );
perform wc_ninja_change_backorder_button1( $textual content, $product ){
    if ( $product->is_on_backorder( 1 ) ) {
        $textual content = __( 'Pre-order', 'woocommerce' );
    }
    return $textual content;
  }

Methods to create and show {custom} product area?

First, let’s create a brand new Woocommerce single product {custom} area and let’s output it within the desired place. The top consequence will appear to be the one right here under on the screenshot.

Woocommerce Single Product Page Hacks

So, simply seize this code and use it accordingly.

<span function="button" tabindex="0" data-code="// Add {custom} area to Woocommerce backend below Normal tab
add_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field' );
perform wpsh_add_text_field() {
woocommerce_wp_text_input( array(
'id' => '_shipping_time',
'label' => __( 'Transport information', 'woocommerce' ),
'description' => __( 'This can be a {custom} area, you may write right here something you need.', 'woocommerce' ),
'desc_tip' => 'true',
'sort' => 'textual content'
) );
}

// Save {custom} area values
add_action( 'woocommerce_admin_process_product_object', 'wpsh_save_field', 10, 1 );
perform wpsh_save_field( $product ) {
if ( isset( $_POST['_shipping_time'] ) ) {
$product->update_meta_data( '_shipping_time', sanitize_text_field( $_POST['_shipping_time'] ) );
}
}

// Show this practice area on Woocommerce single product pages
add_action( 'woocommerce_product_meta_end', 'wpsh_display_on_single_product_page', 10 );
perform wpsh_display_on_single_product_page() {
international $product;

// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$textual content = $product->get_meta( '_shipping_time' );
// NOT empty
if ( ! empty ( $textual content ) ) {
echo '<div class="woocommerce-message">Estimated delivery time: ' . $textual content . '</div>';
}
}
}
// Show this practice area on Woocommerce archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_display_on_archive_page', 10 );
perform wpsh_display_on_archive_page() {
international $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$textual content = $product->get_meta( '_shipping_time' );
// NOT empty
if ( ! empty ( $textual content ) ) {
echo '<div class="custom-text">Estimated delivery time: ' . $textual content . '

// Add {custom} area to Woocommerce backend below Normal tab
add_action( 'woocommerce_product_options_general_product_data', 'wpsh_add_text_field' );
perform wpsh_add_text_field() {
    woocommerce_wp_text_input( array(
        'id'            => '_shipping_time',
        'label'         => __( 'Transport information', 'woocommerce' ),
        'description'   => __( 'This can be a {custom} area, you may write right here something you need.', 'woocommerce' ),
        'desc_tip'      => 'true',
        'sort'          => 'textual content'
    ) );
}


// Save {custom} area values
add_action( 'woocommerce_admin_process_product_object', 'wpsh_save_field', 10, 1 );
perform wpsh_save_field( $product ) {
    if ( isset( $_POST['_shipping_time'] ) ) {        
        $product->update_meta_data( '_shipping_time', sanitize_text_field( $_POST['_shipping_time'] ) );
    }
}

// Show this practice area on Woocommerce single product pages
add_action( 'woocommerce_product_meta_end', 'wpsh_display_on_single_product_page', 10 );
perform wpsh_display_on_single_product_page() {
    international $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get meta
        $textual content = $product->get_meta( '_shipping_time' );
        // NOT empty
        if ( ! empty ( $textual content ) ) {
            echo '<div class="woocommerce-message">Estimated delivery time: ' . $textual content . '</div>';
        }
    }
}
// Show this practice area on Woocommerce archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_display_on_archive_page', 10 );
perform wpsh_display_on_archive_page() {
    international $product;
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get meta
        $textual content = $product->get_meta( '_shipping_time' );
        // NOT empty
        if ( ! empty ( $textual content ) ) {
            echo '<div class="custom-text">Estimated delivery time: ' . $textual content . '</div>';
        }
    }
}

Methods to create {custom} tab?

This snippet right here under creates new {custom} Woocommerce single product tab and it comprises textual content and phone type. Simply change the content material as you want and also you’re good to go.

add_filter( 'woocommerce_product_tabs', 'custom_tab' );
perform custom_tab( $tabs ) {	
	// Provides the brand new tab
	$tabs['form'] = array(
		'title' 	=> __( 'Ship us an enquiry', 'woocommerce' ),
		'precedence' 	=> 30,
		'callback' 	=> 'custom_tab_content'
	);
	return $tabs;
}
perform custom_tab_content() {

	// The brand new tab content material

	echo '<h4>Ship us an enquiry</h4>';
	echo '<p>Lorem ipsum dolor sit amet consectetur adipiscing elit ultricies convallis volutpat, placerat proin scelerisque eget velit tellus at nibh risus. </p>';
	echo do_shortcode( '' );
}

Methods to show “You save” on the market value?

Check out the screenshot right here under. That is what these snippets are doing for you.

Useful Woocommerce Single Product Page Hacks

So, with a view to make it work like that use this snippet. Listen, that this one right here under works solely with easy merchandise. For variable merchandise check out the nex snippet.

<span function="button" tabindex="0" data-code="perform you_save_text_for_product() {
international $product;

// works for Easy and Variable sort
$regular_price = get_post_meta( $product->get_id(), '_regular_price', true ); // 36.32
$sale_price = get_post_meta( $product->get_id(), '_sale_price', true ); // 24.99

if( !empty($sale_price) ) {

$saved_amount = $regular_price – $sale_price;
$currency_symbol = get_woocommerce_currency_symbol();

$proportion = spherical( ( ( $regular_price – $sale_price ) / $regular_price ) * 100 );
?>
<p class="you_save_price">You save: <?php echo $currency_symbol .''. number_format($saved_amount, 2, '.', ''); ?></p>

perform you_save_text_for_product() {
	international $product;

	// works for Easy and Variable sort
	$regular_price 	= get_post_meta( $product->get_id(), '_regular_price', true ); // 36.32
	$sale_price 	= get_post_meta( $product->get_id(), '_sale_price', true ); // 24.99
		
	if( !empty($sale_price) ) {
	
		$saved_amount 		= $regular_price - $sale_price;
		$currency_symbol 	= get_woocommerce_currency_symbol();

		$proportion = spherical( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
		?>
			<p class="you_save_price">You save: <?php echo $currency_symbol .''. number_format($saved_amount, 2, '.', ''); ?></p>				
		<?php		
	} 
		
}
add_action( 'woocommerce_single_product_summary', 'you_save_text_for_product', 12 ); // hook precedence

If you need to customise the look of it then check out the you_save_price CCS class. Now go to Customizer >> Extra CSS and add this small piece of CSS to customise it.

.you_save_price {
	background: #f9f9f9;
	padding: 10px;
	border: 1px dashed #ddd;
	width: 150px;
	font-size: 14px;
	font-weight: 600;
	margin-top: 10px;
}

Now, this code above reveals the saved quantity solely for easy merchandise. When you would additionally like to point out it for variable merchandise, then add this piece of code.

<span function="button" tabindex="0" data-code="// For product variations (on variable merchandise)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 );
perform variable_product_saving_amount( $information, $product, $variation ) {

if( $variation->is_on_sale() ) {
$saved_amount = $information['display_regular_price'] – $information['display_price'];
$proportion = spherical( $saved_amount / $information['display_regular_price'] * 100 );

$information['price_html'] .= '<p id="you_save_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$proportion.'%)

// For product variations (on variable merchandise)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 );
perform variable_product_saving_amount( $information, $product, $variation ) {

    if( $variation->is_on_sale() ) {
        $saved_amount  = $information['display_regular_price'] - $information['display_price'];
        $proportion    = spherical( $saved_amount / $information['display_regular_price'] * 100 );

        $information['price_html'] .= '<p id="you_save_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$proportion.'%)</p>';
    }
    return $information;
}

Methods to show “You save” quantity on the Woocommerce cart and checkout web page?

In case you want to show “You save” quantity on the Woocommerce cart and checkout web page, then use this snippet.

// Show the "You save" line earlier than the subtotal on the Woocommerce cart and checkout pages
add_action( 'woocommerce_cart_totals_before_order_total', 'wpsh_display_you_save_cart' );
add_action( 'woocommerce_review_order_before_order_total', 'wpsh_display_you_save_checkout' );

perform wpsh_display_you_save_cart() {
    display_you_save_message();
}

perform wpsh_display_you_save_checkout() {
    display_you_save_message();
}

perform display_you_save_message() {
    // Get the present WooCommerce cart object
    $cart = WC()->cart;
    $total_savings = 0;
    $tax_based_prices = get_option('woocommerce_prices_include_tax') === 'no';

    // Loop by means of cart gadgets to calculate whole financial savings
    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $amount = $cart_item['quantity'];

        // Get the right common and sale value
        if ( $product->is_type('variable') && $cart_item['variation_id'] ) {
            $variation = wc_get_product( $cart_item['variation_id'] );
            $regular_price = floatval($variation->get_regular_price());
            $sale_price = floatval($variation->get_sale_price());
        } else {
            $regular_price = floatval($product->get_regular_price());
            $sale_price = floatval($product->get_sale_price());
        }

        // Regulate costs if entered unique of tax
        if ( $tax_based_prices ) {
            $regular_price = wc_get_price_including_tax($product, array('value' => $regular_price));
            $sale_price = wc_get_price_including_tax($product, array('value' => $sale_price));
        }

        // Guarantee sale value is decrease than common value
        if ( $sale_price && $regular_price > $sale_price ) {
            $total_savings += ( $regular_price - $sale_price ) * $amount;
        }
    }

    // Show the full financial savings if any
    if ( $total_savings > 0 ) {
        echo '<tr class="order-total">
                <th>' . __( 'You save', 'woocommerce' ) . ':</th>
                <td data-title="' . __( 'You save', 'woocommerce' ) . '">' . wc_price( $total_savings ) . '</td>
              </tr>';
    }
}

Methods to disguise Woocommerce product costs for out-of-stock merchandise?

This snippet right here under hides Woocommerce product value for out-of-stock merchandise

add_filter( 'woocommerce_get_price_html', 'wpsh_hide_price', 9999, 2 );
 
perform wpsh_hide_price( $value, $product ) {
   if ( is_admin() ) return $value; // BAIL IF BACKEND
   if ( ! $product->is_in_stock() ) {
      $value = apply_filters( 'woocommerce_empty_price_html', '', $product );
   }
   return $value;
}

Methods to Show a Contact Type Solely When a Woocommerce Product is Out Of Inventory?

Code Snippets plugin is a free plugin that means that you can run PHP/HTML/CSS/JavaScript code snippets in your website with out the necessity to modify features.php. The plugin might be discovered right here. An alternative choice is to stick the code to your little one theme’s features.php file.

<span function="button" tabindex="0" data-code="/* Present contact type as a substitute of "Out Of Inventory" message */

add_action('woocommerce_single_product_summary', 'out_of_stock_show_form', 20);
perform out_of_stock_show_form() {
international $product;
if(!$product->is_in_stock( )) {
echo '<div class="your-form">';
echo 'Sadly this product is out of inventory.<br>Contact us for extra data';
echo do_shortcode('[your-shortcode]'); // Change with your personal contact type shortcode
echo '</div>';
}
}

/* It will present your contact type when the variation is out of inventory */

add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );
perform variation_out_of_stock_show_form( $information, $product, $variation ) {
if( ! $information['is_in_stock'] )
{
$information['availability_html'] = '<div class="your-form">';
$information['availability_html'] .= 'Sadly this product is out of inventory.<br>Contact us for extra data';
$information['availability_html'] .= do_shortcode('[your-shortcode]'); // Change with your personal contact type shortcode
$information['availability_html'] .= '

/* Present contact type as a substitute of "Out Of Inventory" message */

add_action('woocommerce_single_product_summary', 'out_of_stock_show_form', 20);
perform out_of_stock_show_form() {
    international $product;
    if(!$product->is_in_stock( )) {
        echo  '<div class="your-form">';
        echo  'Sadly this product is out of inventory.<br>Contact us for extra data';
        echo  do_shortcode('[your-shortcode]'); // Change with your personal contact type shortcode
        echo  '</div>';
    }
}
 
/* It will present your contact type when the variation is out of inventory */

add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );
perform variation_out_of_stock_show_form( $information, $product, $variation ) {
    if( ! $information['is_in_stock'] )
    {
        $information['availability_html'] = '<div class="your-form">';
        $information['availability_html'] .= 'Sadly this product is out of inventory.<br>Contact us for extra data';
        $information['availability_html'] .= do_shortcode('[your-shortcode]'); // Change with your personal contact type shortcode
        $information['availability_html'] .= '</div>';
    }
    return $information;
}

Take note of the road which comprises [your-shortcode]. That is the place it’s best to add your contact type shortcode.

Methods to disable (gray out) number of out-of-stock variation?

In your clients, it’s a lot simpler to decide on solely the out there variations. Subsequently, I’ll present you how you can disable /gray out) a number of out-of-stock product variations.

add_filter( 'woocommerce_variation_is_active', 'disable_variations_out_of_stock', 10, 2 );
 
perform disable_variations_out_of_stock( $is_active, $variation ) {
    if ( ! $variation->is_in_stock() ) return false;
    return $is_active;
}

Methods to add {custom} inventory standing to merchandise in WooCommerce?

Woocommerce comes with default inventory statuses “in inventory”, “out of inventory” and “out there on backorder”. If you need so as to add {custom} inventory standing to merchandise in WooCommerce then use this snippet right here under.

This snippet right here under provides 4 extra inventory statuses to Woocommerce.

  • Pre order
  • Due in inventory in 14 days
  • Due in inventory in 21 days
  • Due in inventory in 31 days

That is the top consequence.

How to add custom stock status to products in WooCommerce?

Listen that so as to add or take away your {custom} inventory statuses you want to take away some traces accordingly. Check out the video above that reveals how to try this (see video beginning at 2:10) .

<span function="button" tabindex="0" data-code="// Add {custom} inventory standing to merchandise in WooCommerce
perform filter_woocommerce_product_stock_status_options( $standing ) {
// Add new statuses
$standing['pre_order'] = __( 'Pre order', 'woocommerce' );
$standing['due_in_14'] = __( 'Due in inventory in 14 days', 'woocommerce' );
$standing['due_in_21'] = __( 'Due in inventory in 21 days', 'woocommerce' );
$standing['due_in_31'] = __( 'Due in inventory in 31 days', 'woocommerce' );
return $standing;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );

// Availability textual content
perform filter_woocommerce_get_availability_text( $availability, $product ) {
// Get inventory standing
swap( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'due_in_14':
$availability = __( 'Due in inventory in 14 days', 'woocommerce' );
break;
case 'due_in_21':
$availability = __( 'Due in inventory in 21 days', 'woocommerce' );
break;
case 'due_in_31':
$availability = __( 'Due in inventory in 31 days', 'woocommerce' );
break;
}

return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

// Availability CSS class
perform filter_woocommerce_get_availability_class( $class, $product ) {
// Get inventory standing
swap( $product->get_stock_status() ) {
case 'pre_order':
$class = 'pre-order';
break;
case 'due_in_14':
$class = 'due-in-14';
break;
case 'due_in_21':
$class = 'due-in-21';
break;
case 'due_in_31':
$class = 'due-in-31';
break;
}

return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

// Admin inventory html
perform filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Easy
if ( $product->is_type( 'easy' ) ) {
// Get inventory standing
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );

// Get inventory standing
$product_stock_status = $variation->get_stock_status();

/*
Presently the standing of the final variant within the loop will probably be displayed.

So from right here you want to add your personal logic, relying on what you anticipate out of your {custom} inventory standing.

By default, for the present statuses. The standing displayed on the admin merchandise checklist desk for variable merchandise is set as:

– Product needs to be in inventory if a toddler is in inventory.
– Product needs to be out of inventory if all youngsters are out of inventory.
– Product needs to be on backorder if all youngsters are on backorder.
– Product needs to be on backorder if at the least one little one is on backorder and the remaining are out of inventory.
*/
}
}

// Inventory standing
swap( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" type="background:clear none;colour:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'due_in_14':
$stock_html = '<mark class="due-in-14" type="background:clear none;colour:#666;font-weight:700;line-height:1;">' . __( 'Due in inventory in 14 days', 'woocommerce' ) . '</mark>';
break;
case 'due_in_21':
$stock_html = '<mark class="due-in-21" type="background:clear none;colour:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in inventory in 21 days', 'woocommerce' ) . '</mark>';
break;
case 'due_in_31':
$stock_html = '<mark class="due-in-31" type="background:clear none;colour:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in inventory in 31 days', 'woocommerce' ) . '

// Add {custom} inventory standing to merchandise in WooCommerce
perform filter_woocommerce_product_stock_status_options( $standing ) {
    // Add new statuses
    $standing['pre_order'] = __( 'Pre order', 'woocommerce' );
    $standing['due_in_14'] = __( 'Due in inventory in 14 days', 'woocommerce' );
  	$standing['due_in_21'] = __( 'Due in inventory in 21 days', 'woocommerce' );
    $standing['due_in_31'] = __( 'Due in inventory in 31 days', 'woocommerce' );
    return $standing;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );

// Availability textual content
perform filter_woocommerce_get_availability_text( $availability, $product ) {
    // Get inventory standing
    swap( $product->get_stock_status() ) {
        case 'pre_order':
            $availability = __( 'Pre order', 'woocommerce' );
        break;
        case 'due_in_14':
            $availability = __( 'Due in inventory in 14 days', 'woocommerce' );
        break;
		case 'due_in_21':
            $availability = __( 'Due in inventory in 21 days', 'woocommerce' );
        break;
		case 'due_in_31':
            $availability = __( 'Due in inventory in 31 days', 'woocommerce' );
        break;
    }

    return $availability; 
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

// Availability CSS class
perform filter_woocommerce_get_availability_class( $class, $product ) {
    // Get inventory standing
    swap( $product->get_stock_status() ) {
        case 'pre_order':
            $class = 'pre-order';
        break;
        case 'due_in_14':
            $class = 'due-in-14';
        break;
		case 'due_in_21':
            $class = 'due-in-21';
        break;
		case 'due_in_31':
            $class = 'due-in-31';
        break;
    }

    return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

// Admin inventory html
perform filter_woocommerce_admin_stock_html( $stock_html, $product ) {
    // Easy
    if ( $product->is_type( 'easy' ) ) {
        // Get inventory standing
        $product_stock_status = $product->get_stock_status();
    // Variable
    } elseif ( $product->is_type( 'variable' ) ) {
        foreach( $product->get_visible_children() as $variation_id ) {
            // Get product
            $variation = wc_get_product( $variation_id );
            
            // Get inventory standing
            $product_stock_status = $variation->get_stock_status();
            
            /*
            Presently the standing of the final variant within the loop will probably be displayed.
            
            So from right here you want to add your personal logic, relying on what you anticipate out of your {custom} inventory standing.
            
            By default, for the present statuses. The standing displayed on the admin merchandise checklist desk for variable merchandise is set as:
            
            - Product needs to be in inventory if a toddler is in inventory.
            - Product needs to be out of inventory if all youngsters are out of inventory.
            - Product needs to be on backorder if all youngsters are on backorder.
            - Product needs to be on backorder if at the least one little one is on backorder and the remaining are out of inventory.
            */
        }
    }
    
    // Inventory standing
    swap( $product_stock_status ) {
        case 'pre_order':
            $stock_html = '<mark class="pre-order" type="background:clear none;colour:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
        break;
        case 'due_in_14':
            $stock_html = '<mark class="due-in-14" type="background:clear none;colour:#666;font-weight:700;line-height:1;">' . __( 'Due in inventory in 14 days', 'woocommerce' ) . '</mark>';
        break;
		case 'due_in_21':
            $stock_html = '<mark class="due-in-21" type="background:clear none;colour:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in inventory in 21 days', 'woocommerce' ) . '</mark>';
        break;
		case 'due_in_31':
            $stock_html = '<mark class="due-in-31" type="background:clear none;colour:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in inventory in 31 days', 'woocommerce' ) . '</mark>';
        break;
    }
 
    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );

Methods to take away Woocommerce opinions tab?

See one other video I made on how you can create, rename, take away and reorder {custom} product tabs.

add_filter( 'woocommerce_product_tabs', 'remove_review_tab', 98 );
perform remove_review_tab( $tabs ) {
    unset( $tabs['reviews'] ); // Take away the opinions tab
    return $tabs;
}

Methods to create {custom} order statuses for Woocommerce?

Now let’s check out how you can create {custom} order standing for Woocommerce. With the assistance of this snippet I’m going to create a {custom} standing known as “In-progress” that’s displayed on each backend and front-end order pages.

<span function="button" tabindex="0" data-code="// Add {custom} Woocommerce order standing
perform register_in_progress_order_status() {
register_post_status( 'wc-invoiced', array(
'label' => _x( 'In-progress', 'Order Standing', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_all_admin_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'In-progress <span class="depend">(%s)</span>', 'In-progress <span class="depend">(%s)</span>', 'woocommerce' )
)
);
}

add_action( 'init', 'register_in_progress_order_status' );

perform my_invoiced_order_status( $order_statuses ){
$order_statuses['wc-invoiced'] = _x( 'In-progress', 'Order Standing', 'woocommerce' );
return $order_statuses;

}
add_filter( 'wc_order_statuses', 'my_invoiced_order_status' );

perform show_in_bulk_actions() {
international $post_type;

if( 'shop_order' == $post_type ) {
?>
<script sort="textual content/javascript">
jQuery(doc).prepared(perform(){
jQuery('<choice>').val('mark_invoiced').textual content('<?php _e( 'Change Standing to In-progress','woocommerce' ); ?>').appendTo("choose[name='action']");
jQuery('<choice>').val('mark_invoiced').textual content('<?php _e( 'Change Standing to In-progress','woocommerce' ); ?>').appendTo("choose[name='action2']");
});
</script>

// Add {custom} Woocommerce order standing
perform register_in_progress_order_status() {
    register_post_status( 'wc-invoiced', array(
            'label' => _x( 'In-progress', 'Order Standing', 'woocommerce' ),
            'public' => true,
            'exclude_from_search' => false,
            'show_in_all_admin_list' => true,
            'show_in_admin_status_list' => true,
            'label_count' => _n_noop( 'In-progress <span class="depend">(%s)</span>', 'In-progress <span class="depend">(%s)</span>', 'woocommerce' )
        )
    );
}

add_action( 'init', 'register_in_progress_order_status' );

perform my_invoiced_order_status( $order_statuses ){
    $order_statuses['wc-invoiced'] = _x( 'In-progress', 'Order Standing', 'woocommerce' );
    return $order_statuses;

}
add_filter( 'wc_order_statuses', 'my_invoiced_order_status' );

perform show_in_bulk_actions() {
    international $post_type;

    if( 'shop_order' == $post_type ) {
        ?>
            <script sort="textual content/javascript">
                jQuery(doc).prepared(perform(){
                    jQuery('<choice>').val('mark_invoiced').textual content('<?php _e( 'Change Standing to In-progress','woocommerce' ); ?>').appendTo("choose[name='action']");
                    jQuery('<choice>').val('mark_invoiced').textual content('<?php _e( 'Change Standing to In-progress','woocommerce' ); ?>').appendTo("choose[name='action2']");
                });
            </script>
        <?php
    }
}
add_action( 'admin_footer', 'show_in_bulk_actions' );

Methods to show buyer cellphone and e mail in Woocommerce orders desk?

Check out the screenshot under, and also you’ll see that there’s a buyer cellphone and e mail that’s displayed in Woocommerce orders desk. If you need to attain the identical consequence, then use this snippet right here under.

Display customer email in Woocommerce orders table
<span function="button" tabindex="0" data-code="// Show buyer cellphone and e mail in Woocommerce orders desk
add_action( 'manage_shop_order_posts_custom_column' , 'wpsh_phone_and_email_column', 50, 2 );
perform wpsh_phone_and_email_column( $column, $post_id ) {
if ( $column == 'order_number' )
{
international $the_order;
// Show buyer cellphone in Woocommerce orders desk
if( $cellphone = $the_order->get_billing_phone() ){
$phone_wp_dashicon = '<span class="dashicons dashicons-phone"></span> ';
echo '<br><a href="tel:'.$cellphone.'">' . $phone_wp_dashicon . $cellphone.'</a></sturdy>';
}
// Show buyer e mail in Woocommerce orders desk
if( $e mail = $the_order->get_billing_email() ){
echo '<br><sturdy><a href="mailto:'.$e mail.'">' . $e mail . '</a>

// Show buyer cellphone and e mail in Woocommerce orders desk
add_action( 'manage_shop_order_posts_custom_column' , 'wpsh_phone_and_email_column', 50, 2 );
perform wpsh_phone_and_email_column( $column, $post_id ) {
    if ( $column == 'order_number' )
    {
        international $the_order;
 // Show buyer cellphone in Woocommerce orders desk
        if( $cellphone = $the_order->get_billing_phone() ){
            $phone_wp_dashicon = '<span class="dashicons dashicons-phone"></span> ';
            echo '<br><a href="tel:'.$cellphone.'">' . $phone_wp_dashicon . $cellphone.'</a></sturdy>';
        }
	 // Show buyer e mail in Woocommerce orders desk
	          if( $e mail = $the_order->get_billing_email() ){
            echo '<br><sturdy><a href="mailto:'.$e mail.'">' . $e mail . '</a></sturdy>';
        }
    }
}

Methods to autocomplete Woocommerce processing orders?

Why would you want to autocomplete Woocommerce processing orders? For instance, for those who’re promoting digital product (Programs and so forth.) and also you want to add an entry to it proper after funds.

// Autocomplete Woocommerce processing orders

add_filter( 'woocommerce_payment_complete_order_status', 'processing_orders_autocomplete', 9999 );
perform processing_orders_autocomplete() {
   return 'accomplished';
}

Methods to autocomplete all Woocommerce orders?

However (simply “however) what if you need to autocomplete all Woocommerce orders? That’s evan on-hold orders? If that is so then use this nippet right here under.

// Auto Full all WooCommerce orders.

add_action( 'woocommerce_thankyou', 'wpsh_complete_all_orders' );
perform wpsh_complete_all_orders( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }
    $order = wc_get_order( $order_id );
    $order->update_status( 'accomplished' );
}

Methods to Add Bought merchandise tab to Woocommerce my account web page?

Now let’s add Bought merchandise tab to Woocommerce my account web page (se the screenshot above). This tabb shows your not too long ago bought merchandise within the separate tabe.

<span function="button" tabindex="0" data-code="// Add Bought merchandise tab to Woocommerce my account web page

// right here we hook the My Account menu hyperlinks and add our {custom} one
add_filter( 'woocommerce_account_menu_items', 'wpsh_purchased_products', 40 );
perform wpsh_purchased_products( $menu_links ){

// Set 5 on this line to one thing else if you need to maneuver the place of the tab
return array_slice( $menu_links, 0, 5, true )
+ array( 'purchased-products' => 'Bought Merchandise' )
+ array_slice( $menu_links, 2, NULL, true );

}

add_action( 'init', 'wpsh_purchased_products_endpoint' );
perform wpsh_purchased_products_endpoint() {
add_rewrite_endpoint( 'purchased-products', EP_PAGES );
}

// Add bought porducts as a tab content material
add_action( 'woocommerce_account_purchased-products_endpoint', 'wpsh_purchased_products_content' );
perform wpsh_purchased_products_content() {

international $wpdb;

// Bought merchandise are sorted by date
$purchased_products_ids = $wpdb->get_col(
$wpdb->put together(
"
SELECT itemmeta.meta_value
FROM " . $wpdb->prefix . "woocommerce_order_itemmeta itemmeta
INNER JOIN " . $wpdb->prefix . "woocommerce_order_items gadgets
ON itemmeta.order_item_id = gadgets.order_item_id
INNER JOIN $wpdb->posts orders
ON orders.ID = gadgets.order_id
INNER JOIN $wpdb->postmeta ordermeta
ON orders.ID = ordermeta.post_id
WHERE itemmeta.meta_key = '_product_id'
AND ordermeta.meta_key = '_customer_user'
AND ordermeta.meta_value = %s
ORDER BY orders.post_date DESC
",
get_current_user_id()
)
);

// Don’t show duplicate merchandise
$purchased_products_ids = array_unique( $purchased_products_ids );
if( ! empty( $purchased_products_ids ) ) {

echo '<div class="woocommerce-message">Bought merchandise

// Add Bought merchandise tab to Woocommerce my account web page

// right here we hook the My Account menu hyperlinks and add our {custom} one
add_filter( 'woocommerce_account_menu_items', 'wpsh_purchased_products', 40 );
perform wpsh_purchased_products( $menu_links ){

// Set 5 on this line to one thing else if you need to maneuver the place of the tab
	return array_slice( $menu_links, 0, 5, true )
	+ array( 'purchased-products' => 'Bought Merchandise' )
	+ array_slice( $menu_links, 2, NULL, true );

}

add_action( 'init', 'wpsh_purchased_products_endpoint' );
perform wpsh_purchased_products_endpoint() {
	add_rewrite_endpoint( 'purchased-products', EP_PAGES );
}

// Add bought porducts as a tab content material
add_action( 'woocommerce_account_purchased-products_endpoint', 'wpsh_purchased_products_content' );
perform wpsh_purchased_products_content() {

	international $wpdb;

	// Bought merchandise are sorted by date
	$purchased_products_ids = $wpdb->get_col( 
		$wpdb->put together(
			"
			SELECT      itemmeta.meta_value
			FROM        " . $wpdb->prefix . "woocommerce_order_itemmeta itemmeta
			INNER JOIN  " . $wpdb->prefix . "woocommerce_order_items gadgets
			            ON itemmeta.order_item_id = gadgets.order_item_id
			INNER JOIN  $wpdb->posts orders
			            ON orders.ID = gadgets.order_id
			INNER JOIN  $wpdb->postmeta ordermeta
			            ON orders.ID = ordermeta.post_id
			WHERE       itemmeta.meta_key = '_product_id'
			            AND ordermeta.meta_key = '_customer_user'
			            AND ordermeta.meta_value = %s
			ORDER BY    orders.post_date DESC
			",
			get_current_user_id()
		)
	);

	// Don’t show duplicate merchandise
	$purchased_products_ids = array_unique( $purchased_products_ids );
	if( ! empty( $purchased_products_ids ) ) {
	  
	 echo '<div class="woocommerce-message">Bought merchandise</div>';
	  
		$purchased_products = new WP_Query( array(
			'post_type' => 'product',
			'post_status' => 'publish',
			'post__in' => $purchased_products_ids,
			'orderby' => 'post__in',
			'posts_per_page' => -1,
		) );
	
		woocommerce_product_loop_start();

		whereas ( $purchased_products->have_posts() ) : $purchased_products->the_post();

			wc_get_template_part( 'content material', 'product' );

		endwhile;

		woocommerce_product_loop_end();

		woocommerce_reset_loop();
		wp_reset_postdata();

	} else {
	  // Change this textual content if wanted)
		echo 'Sadly you haven't any purchases but';
	}
}

Methods to add “Order once more” button to Woocommerce my account web page?

As menitonet above, we’re goint so as to add “Order once more” button to Woocommerce my account web page. Use this snippet right here under.

// Add "Order once more" button to Woocommerce my account web page
add_filter( 'woocommerce_my_account_my_orders_actions', 'wpsh_order_again_button', 9999, 2 );
    
perform wpsh_order_again_button( $actions, $order ) {
    if ( $order->has_status( 'accomplished' ) ) {
        $actions['order-again'] = array(
            'url' => wp_nonce_url( add_query_arg( 'order_again', $order->get_id(), wc_get_cart_url() ), 'woocommerce-order_again' ),
            'identify' => __( 'Order once more', 'woocommerce' ),
        );
    }
    return $actions;
}

Methods to add “Cancel” button to Woocommerce my account web page?

Check out this screenshot right here. As you see, there are two {custom} buttons (“Cancel” and “Order once more” buttons). So, if you need so as to add “Cancel” button to Woocommerce my account web page, then use this snippet right here under.

NB! Listen on the line 8. In it you may set the delay in days. Presently it’s set to three dayst, that’s withnin 3 days it’s allowed to cancel the order.

How to add "Cancel" button to Woocommerce my account page?

// Add "Cancel" button to Woocommerce my account web page

add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'wpsh_add_cancel_button', 20, 2 );
perform wpsh_add_cancel_button( $statuses, $order = '' ){

    // Set HERE the order statuses the place you need the cancel button to look
    $custom_statuses    = array( 'pending', 'processing', 'on-hold', 'failed' );

    // Set HERE the delay (in days)
    $length = 3; // 3 days

    // UPDATE: Get the order ID and the WC_Order object
    if( ! is_object( $order ) && isset($_GET['order_id']) )
        $order = wc_get_order( absint( $_GET['order_id'] ) );

    $delay = $length*24*60*60; // (length in seconds)
    $date_created_time  = strtotime($order->get_date_created()); // Creation date time stamp
    $date_modified_time = strtotime($order->get_date_modified()); // Modified date time stamp
    $now = strtotime("now"); // Now  time stamp

    // Utilizing Creation date time stamp
    if ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;
    else return $statuses;
}

Methods to add Woocommerce product ID column?

Subsequent, let’s customise Woocommerce admin dashboard additional. Generally you could must visually see your Woocommerce product ID columns. This snippet will aid you out.

// Add Woocommerce produc ID column?

perform woo_product_extra_columns($columns)
{
	$newcolumns = array(
		"cb"       		=> "<enter sort  = "checkbox" />",
		"product_ID"    => esc_html__('ID', 'woocommerce'),
	);
	$columns = array_merge($newcolumns, $columns);
	
	return $columns;
}
add_filter("manage_edit-product_columns", "woo_product_extra_columns");

perform woo_product_extra_columns_content($column)
{
	international $submit;
	
	$product_id = $submit->ID;
	swap ($column)
	{
		case "product_ID":
			echo $product_id;
		break;		
	}
}
add_action("manage_posts_custom_column",  "woo_product_extra_columns_content");

// Regulate column dimension
add_action('admin_head', 'my_column_width');
perform my_column_width() {
    echo '<type sort="textual content/css">';
    echo '.column-product_ID { width:40px; }';
    echo '</type>';
}
  • Methods to disguise firm area primarily based on a Woocommerce {custom} checkout radio choice?
  • Methods to use Woocommerce coupons in 23 alternative ways? (23 easy hacks)
  • Methods to Customise Woocommerce Orders web page? (20 hacks)
  • Methods to customise Woocommerce cart web page? 21 helpful Woocommerce Cart Web page Hacks
  • Methods to Customise Woocommerce store and class web page? 17 helpful hacks
  • Methods to customise Woocommerce Single Product Web page | 14 Helpful Hacks
  • Methods to Customise Woocommerce Checkout web page? 28 helpful hacks
  • Methods to customise Woocommerce Single Product Web page | 14 Helpful Hacks
  • Methods to Disguise Woocommerce Transport Strategies (Conditionally) – 15 Hacks
  • Methods to Customise Woocommerce Inventory Standing? (17 hacks)

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top