Woocommerce 商店页面和档案中的附加按钮 [英] Additional button in Woocommerce shop page and archives
问题描述
我想在产品页面上的添加到购物车"旁边添加一个按钮,在点击时将-sample"添加到产品 URL.
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 的页面并且 URL 是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 中添加-sample"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?
谢谢
推荐答案
For 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屋!