Monthly Archives: April 2018

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.