简体   繁体   English

基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮

[英]Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type

I want add a custom Button "View Demo" next to "Add to Cart" button of WooCommerce based on Product Type, both on main shop page and single product page.我想在主商店页面和单个产品页面上根据产品类型在 WooCommerce 的“添加到购物车”按钮旁边添加一个自定义按钮“查看演示”。

I've done this steps:我已经完成了以下步骤:

Add code to theme file header.php :将代码添加到主题文件header.php

<script>var url_demo = "<?php echo the_field('url_demo'); ?>"</script>

Add jQuery script using "TC Custom JavaScript" plugin:使用“TC Custom JavaScript”插件添加 jQuery 脚本:

jQuery(function($) {
 $('.add_to_cart_button, .single_add_to_cart_button').after(' <a class="button demo_button" style="padding-right: 0.75em;padding-left: 0.75em;margin-left: 8px; background-color: #0ebc30;" href="'+url_demo+'" target="_blank">View Demo</a>');
});

It's work, the Custom button "View Demo" shown on main shop page and single product page.它的工作,在主商店页面和单个产品页面上显示的自定义按钮“查看演示”。

But I've some problem now, the "View Demo" button link only correct on Single Product page, while on shop main page the "View Demo" button, link to the same url.但是我现在有一些问题,“查看演示”按钮链接仅在单个产品页面上正确,而在商店主页上的“查看演示”按钮链接到相同的 url。 Is my code above wrong?我上面的代码错了吗?

My questions are, how to add "View Demo" button (both on main shop page and single product page) that only show for spesific product type, for example only show on category Theme ?我的问题是,如何添加仅针对特定产品类型显示的“查看演示”按钮(在主商店页面和单个产品页面上),例如仅在类别主题上显示? Last, is there any other way to add demo link without editing the header.php file like above method?最后,有没有其他方法可以添加演示链接,而无需像上面的方法那样编辑header.php文件? I just anticipating the header.php file reset if the theme updated.如果主题更新,我只是期待header.php文件重置。

I am using another way to do it: WooCommerce hooks .我正在使用另一种方法来做到这一点: WooCommerce hooks

You don't need anymore the jQuery script and also the javascript located in your header.php file, so you can erase them.您不再需要jQuery 脚本以及位于header.php文件中的 javascript,因此您可以删除它们。

Using get_field() instead of the_field (thanks to Deepti chipdey ) to get only the value concatenated in the echoed string.使用get_field()而不是the_field (感谢Deepti chipdey )只获取echoed字符串中连接的值。

Paste this code snippet in your function.php file located in your active child theme or theme folder:将此代码片段粘贴到位于活动子主题或主题文件夹中的function.php文件中:

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 );

I have target the hooks used to display Add to cart button on shop page and in single product pages, to display your View demo button after it, buy lowering the priority.我已经针对用于在商店页面和单个产品页面上显示添加到购物车按钮的钩子,在它之后显示您的查看演示按钮,降低优先级购买。

For some reason LoicTheAztec 's answer did not do it for me.出于某种原因, LoicTheAztec回答对我没有帮助。

Here is what worked:这是有效的:

function wc_shop_demo_button() {
    echo '<a class="button demo_button" href="'.get_field( "url_demo" ).'" target="_blank">View Demo</a>';
}
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_demo_button' );

Hope that helps someone in their journey.希望对他们的旅程有所帮助。

Change改变

the_field('url_demo');

to

 get_field('url_demo');

HELP, How can i position it above ADD TO CART button ?帮助,我怎样才能将它放在添加到购物车按钮上方?

Position here定位在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM