Tried this solution 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:
wp-includes/class-wp-hook.php(286): wc_shop_demo_button('')
wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
wp-includes/plugin.php(465): WP_Hook->do_action(Array)
wp-content/plugins/woocommerce/templates/content-product.php(65): do_action('woocommerce_aft...')
wp-includes/template.php(706): require('/home/u99678646...')
wp-content/plugins/woocommerce/includes/wc-core-functions.php(195): load_template('/home/u99678646...', false)
wp-content/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-products.php(652): wc_get_template_part('content', 'product')
/home/u9967864
Tried this solution https://stackoverflow/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:
wp-includes/class-wp-hook.php(286): wc_shop_demo_button('')
wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
wp-includes/plugin.php(465): WP_Hook->do_action(Array)
wp-content/plugins/woocommerce/templates/content-product.php(65): do_action('woocommerce_aft...')
wp-includes/template.php(706): require('/home/u99678646...')
wp-content/plugins/woocommerce/includes/wc-core-functions.php(195): load_template('/home/u99678646...', false)
wp-content/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-products.php(652): wc_get_template_part('content', 'product')
/home/u9967864
1 Answer
Reset to default 1You'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.