Woocommerce商店页面和档案中的附加按钮 [英] Additional button in Woocommerce shop page and archives
问题描述
我想在产品页面上添加添加到购物车旁边的按钮,在点击时将-sample添加到产品网址。
I'd like to add a button next to "Add to Cart" on the product page that adds "-sample" to the product URL when clicked.
示例:
您正在查看产品1的页面,网址为 http://www.example.com/shop/product-1/
You're viewing Product 1's page and the URL is "http://www.example.com/shop/product-1/
"
当您点击按钮时,它会添加 - 样品到URL http://www.example.com/shop/product-1-sample/
When you click on the button, it adds "-sample" to the URL
"http://www.example.com/shop/product-1-sample/
"
我怎样才能实现这个目标?
How can I achieve this?
谢谢
推荐答案
对于woocommerce 3 + (仅限):
在woocommerce 3中,您将使用 woocommerce_after_shop_loop_item
操作挂钩,因为挂钩 woocommerce_after_add_to_cart_button
将不再有效。
In woocommerce 3 you will use woocommerce_after_shop_loop_item
action hook instead, as the hook woocommerce_after_add_to_cart_button
will not work anymore.
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = $product->get_permalink();
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
代码继续 function.php
活动子主题(或活动主题)的文件。测试和工作。
Code goes on function.php
file of your active child theme (or active theme). Tested and works.
在woocommerce 3之前:
这可以使用钩子 woocommerce_after_add_to_cart_button
在产品页面上添加您的附加按钮,使用此自定义功能:
This is possible using hook woocommerce_after_add_to_cart_button
to add your additional button on product pages, using this custom function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = get_permalink( get_the_id() );
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
此代码为 function.php
活动子主题或主题的文件。
此代码已经过测试,功能齐全。
基于此:添加到购物车并重定向后添加一个按钮它是WooCommerce中的一些自定义链接
这篇关于Woocommerce商店页面和档案中的附加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!