对于 woocommerce 3+ (仅限):
在 woocommerce 3 中,您将改用 woocommerce_after_shop_loop_item 动作挂钩,因为挂钩 woocommerce_after_add_to_cart_button 将不再起作用。
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 文件中。经过测试并且可以工作。
在 woocommerce 3 之前:
这可以使用钩子 woocommerce_after_add_to_cart_button 在产品页面上添加您的附加按钮,使用此自定义功能:
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 文件中。
此代码经过测试且功能齐全。
基于此:Add a button after add to cart and redirect it to some custom link in WooCommerce
还有这个:PHP - How to remove all specific characters at the end of a string?