How to make a custom button that redirects to a "user specified link while entering product details" woocommerce

by Karan Sethi   Last Updated August 14, 2019 12:08 PM

Tried this solution https://stackoverflow.com/questions/37228791/custom-button-next-to-add-to-cart-button-of-woocommerce-based-on-product-type but its not working

function wc_shop_demo_button() {
    echo '<a class="button demo_button" style="padding-right: 0.75em;padding-left: 0.75em;margin-left: 8px; background-color: #0ebc30;" href="'.get_field( "url_demo" ).'" target="_blank">View Demo</a>';
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_shop_demo_button', 20 );
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_demo_button', 20 );

Errors Shown

Your PHP code changes were rolled back due to an error on line 78 of file wp-content/themes/storefront/functions.php. Please fix and try saving again.

Uncaught Error: Call to undefined function get_field() in wp-content/themes/storefront/functions.php:78 Stack trace:

  1. wp-includes/class-wp-hook.php(286): wc_shop_demo_button('')

  2. wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)

  3. wp-includes/plugin.php(465): WP_Hook->do_action(Array)

  4. wp-content/plugins/woocommerce/templates/content-product.php(65): do_action('woocommerce_aft...')

  5. wp-includes/template.php(706): require('/home/u99678646...')

  6. wp-content/plugins/woocommerce/includes/wc-core-functions.php(195): load_template('/home/u99678646...', false)

  7. wp-content/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-products.php(652): wc_get_template_part('content', 'product')

  8. /home/u9967864

Tags : woocommerce


Answers 1


You're getting Call to undefined function get_field() because you don't have Advanced Custom Fields installed.

You'll need to install ACF, set up a field group, set the location to "Post type is equal to product", and add a text/link field called "url_demo" in order for it to work.

Then your code will go and grab the user-defined value of "url_demo" if it exists.

Andy Mardell
Andy Mardell
August 14, 2019 11:22 AM

Related Questions


How can add and preview product Frontend

Updated March 26, 2015 12:02 PM


How to add "Reply-to" in Woocommerce

Updated April 04, 2015 15:03 PM


WooCommerce epdq gateway Change order status

Updated April 06, 2015 19:03 PM