最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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:

  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

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:

  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

Share Improve this question asked Aug 14, 2019 at 11:18 Karan SethiKaran Sethi 1133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论