0

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

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.

  • Thank-you, worked like a charm! – Karan Sethi Aug 14 at 15:41
  • How to load it in the same page rather than opening it in the new tab – Karan Sethi Aug 14 at 16:29
  • Remove target="_blank" – Andy Mardell Sep 5 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.