Category Archives for "WooCommerce"

Add filter to WooCommerce category metabox on Product Set “edit” page

If you have a lot of categories (like I have on geargrabber.net) you may find that scrolling and finding the category you are looking for is time-consuming. Here’s what the default WooCommerce category metabox looks like on a Product Set “edit” page:

You can use the Post Category Filter plugin to add a filter to your WooCommerce category box to make it easy to filter your long list of WooCommerce categories to something more manageable.

Here’s what the end result looks like:

Simple as that!

1 Custom WooCommerce Buy Buttons

There are lots of fun things you can do to WooCommerce [Buy Now] and [Add to Cart] buttons.

Here I’ll show you a few ways you can customize WooCommerce buttons which appear in The Loop (Shop Frontpage, Shop Category pages, etc…). You can add the code snippets below to a Custom Plugin.

First, let’s see how buttons look like be default. Here is a screenshot of 3 products on a category page. All 3 of these products are External (or Affiliate) products so they display a greyed out [Buy Now] button.

Default Button Display in WooCommerce Loop

Add Highlighting to Buttons for Products On Sale

This one will format the WooCommerce buttons by highlighting the buttons for products which are on sale.

Here’s what that would look like:

The custom code for that looks like this:

<?php /** * Add highlighting to an add to cart button for WooCommerce products which are on sale. * * @param string $html * @param WC_Product $product * @param array $args * * @return string */ function mycode_display_highlighted_add_to_cart_button_for_products_on_sale( $html, $product, $args ) { if ( ! $product->is_on_sale() ) { return $html; } $extra_class = ' alt'; $extra_text = ' SALE'; $url = $product->add_to_cart_url(); $quantity = isset( $args['quantity'] ) ? $args['quantity'] : 1; $class = isset( $args['class'] ) ? $args['class'] . $extra_class : 'button' . $extra_class; $attributes = isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : ''; $text = $product->add_to_cart_text() . $extra_text; $format = '<a href="%1$s" data-quantity="%2$s" class="%3$s" %4$s>%5$s</a>'; return sprintf( $format, esc_url( $url ), esc_attr( $quantity ), esc_attr( $class ), $attributes, esc_html( $text ) ); } add_filter( 'woocommerce_loop_add_to_cart_link', 'mycode_display_highlighted_add_to_cart_button_for_products_on_sale', 20, 3 );

Remove Buy/Add Buttons

In some cases you may want to remove the buttons altogether. This might be useful for the next few code snippets.

Here’s how to remove the Buy/Add buttons from your WooCommerce Loops.

<?php /** * Remove the action which adds [Add to Cart] & [Buy Now] buttons * to the WooCommerce Product Loop */ function mycode_remove_add_to_cart_from_loop() { remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' ); } add_action( 'init', 'mycode_remove_add_to_cart_from_loop' );

Add [More Details] Button Linking to Single Product Page

Now that the Buy/Add button is gone, we can add some other buttons to The Loop.

Here we will add a [More Details] button which links to the single product page.

While it’s not that exciting, we will build on this in the following examples. But if you like this, here’s the code:

<?php /** * Adds an action to the loop to display a [More Details] button * to the WooCommerce Product Loop which links to the Single Product Page. */ function mycode_add_more_details_button_to_single() { add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_single' ); } add_action( 'init', 'mycode_add_more_details_button_to_single' ); /** * Display [More Details] button HTML with a link to the Single Product Page. * * @global WC_Product $product */ function mycode_display_more_details_button_to_single() { global $product; $url = $product->get_permalink(); $text = __( 'More Details', 'mycode' ); $title = sprintf( __( 'View more details about %s', 'mycode' ), $product->get_name() ); $class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() ); $format = '<a href="%1$s" title="%2$s" class="%3$s">%4$s</a>'; printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), esc_html( $text ) ); }

Link Directly to External/Affiliate Site from [More Details] Button

The one is strictly for External/Affiliate products. This adds a new [More Details] button which links directly to the external/affiliate website instead of your single product page.

This looks very similar to the default functionality of WooCommerce.

And here’s the custom code:

<?php /** * Adds an action to the loop to display a [More Details] button * to the WooCommerce Product Loop which links to an External/Affiliate product. */ function mycode_add_more_details_button_to_external() { add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_external' ); } add_action( 'init', 'mycode_add_more_details_button_to_external' ); /** * Display [More Details] button HTML with a link to an External/Affiliate product. * * @global WC_Product_External $product */ function mycode_display_more_details_button_to_external() { /** * @var WC_Product_External $product */ global $product; if ( 'external' != $product->get_type() ) { return; } $url = $product->get_product_url(); $text = __( 'Buy Now', 'mycode' ); $title = sprintf( __( 'View more details about %s', 'mycode' ), $product->get_name() ); $class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() ); $format = '<a href="%1$s" title="%2$s" class="%3$s" target="_blank">%4$s</a>'; printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), esc_html( $text ) ); }

Add Price to Button

Here we will add the product’s price (regular or sale price) to the button. This helps prices stand out a bit more.

Here’s the custom code for that:

<?php /** * Adds an action to the loop to display a [Buy $199.99] button containing the price * to the WooCommerce Product Loop which links to an External/Affiliate product. */ function mycode_add_more_details_button_to_external_with_price() { add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_external_with_price' ); } add_action( 'init', 'mycode_add_more_details_button_to_external_with_price' ); /** * Display [Buy $199.99] button HTML containing the price with a link to an External/Affiliate product. * * @global WC_Product_External $product */ function mycode_display_more_details_button_to_external_with_price() { /** * @var WC_Product_External $product */ global $product; if ( 'external' != $product->get_type() ) { return; } $url = $product->get_product_url(); $price = wc_price( $product->get_price() ); $text = sprintf( __( 'Buy %s', 'mycode' ), $price ); $title = sprintf( __( 'Buy %s', 'mycode' ), $product->get_name() ); $class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() ); $format = '<a href="%1$s" title="%2$s" class="%3$s" target="_blank">%4$s</a>'; printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), $text ); }

Add Merchant’s Name to Button

Here’s another one that works well for External/Affiliate products. We can also add the merchant’s name to the button.

And here’s the code:

<?php /** * Adds an action to the loop to display a [Buy at Acme] button containing the merchant's name * to the WooCommerce Product Loop which links to an External/Affiliate product. */ function mycode_add_more_details_button_to_external_with_merchant_name() { add_action( 'woocommerce_after_shop_loop_item', 'mycode_display_more_details_button_to_external_with_merchant_name' ); } add_action( 'init', 'mycode_add_more_details_button_to_external_with_merchant_name' ); /** * Display [Buy at Acme] button containing the merchant's name in the * WooCommerce Product Loop which links to an External/Affiliate product. * * @global WC_Product_External $product */ function mycode_display_more_details_button_to_external_with_merchant_name() { /** * @var WC_Product_External $product */ global $product; if ( 'external' != $product->get_type() ) { return; } $url = $product->get_product_url(); $merchant = $product->get_attribute( 'pa_merchant' ); $text = $merchant ? sprintf( __( 'Buy at %s', 'mycode' ), $merchant ) : __( 'Buy Now', 'mycode' ); $title = sprintf( __( 'View more details about %s', 'mycode' ), $product->get_name() ); $class = sprintf( 'button add_to_cart_button product_type_%s', $product->get_type() ); $format = '<a href="%1$s" title="%2$s" class="%3$s" target="_blank">%4$s</a>'; printf( $format, esc_url( $url ), esc_attr( $title ), esc_attr( $class ), esc_html( $text ) ); }

Those are all for the WooCommerce Loop but if you want to make similar customizations on the Single Product Page, you would want to use the woocommerce_after_single_product_summary hook.

6 Modifying WooCommerce Product URLs

Before WooCommerce 2.5, it was possible to change/modify/override the URL to the product page using a filter on the the_permalink hook. For example, you could do this:

[php] add_filter( ‘the_permalink’, ‘mycode_the_permalink’ );
function mycode_the_permalink( $url ) {
global $product;
if ( ! empty( $product ) ) {
$url = ‘http://www.google.com’;
}

return $url;
}
[/php]

That would change all of the links to your product pages to a link to Google instead. If you felt compelled to give Google more Page Rank, you could do that. However, in version 2.5 of WooCommerce, that code no longer works.

Instead you need to remove WooCommerce’s woocommerce_before_shop_loop_item action and then re-add it again with your own function. Here’s a version:

[php] remove_action( ‘woocommerce_before_shop_loop_item’, ‘woocommerce_template_loop_product_link_open’, 10 );
add_action( ‘woocommerce_before_shop_loop_item’, ‘mycode_woocommerce_template_loop_product_link_open’, 20 );
function mycode_woocommerce_template_loop_product_link_open() {

$url = ‘https://www.datafeedr.com/’;

echo ‘<a href="’ . $url . ‘">’;
}
[/php]

Now all links to the product pages which are generated within the Loop on your site will be linked to Datafeedr! Yay! We could use a little help with Page Rank.

Speeding up large Datafeedr + WooCommerce stores

I wanted to share a few links from Dave Hilditch over at http://www.affiliatewebdesigners.com/. He’s shared some tips on how to optimize for large, 100K+ product stores on WooCommerce. Check them out here:

Hopefully these help some folks struggling with building huge Datafeedr/WooCommerce stores.