Woocommerce - 删除添加到购物车按钮功能并更改按钮文本
Woocommerce - Remove add to cart button functionality and change button text
在 WooCommerce 中,我想替换添加到购物车按钮并将其更改为 "Read More" 但仅适用于某些产品类别 。
我尝试为此使用了很多不同的代码片段,但似乎没有任何东西能给我想要的结果。
原因是我想将我商店的某些部分用作产品目录,显示商品及其价格但不允许用户购买。
有谁知道how/if这可能吗?
任何帮助都将不胜感激。
以下是一些函数,它们将仅针对某些已定义的产品类别替代:
- 在商店和档案页面中:按钮通过 link 添加到购物车到产品
- 在单个产品页面中: 通过自定义按钮添加到购物车按钮和数量
您必须在该函数中定义:
- 数组中的产品类别(2次)
- "Read more"按钮产品link(用于简单产品页面)
代码如下:
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_conditionally_loop_add_to_cart_button', 10, 2 );
function replacing_conditionally_loop_add_to_cart_button( $button, $product ) {
// Set HERE your product categories in the array
$categories = array( 't-shirts', 'gloves' );
if( has_term( $categories, 'product_cat', $product->get_id() ) ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
// Button replacement function for Single product pages (SET the link below)
function custom_button_read_more( $product_link ){
global $product;
// Set HERE your product link
$product_link = home_url( '/some-link-to-define/' );
$button_text = __('Read more', 'woocommerce');
echo '<a href="'.$product_link.'" class="button">'.$button_text.'</a>';
}
// replacing add to cart button and quantities by your custom button in Single product pages
add_action( 'woocommerce_single_product_summary', 'replacing_conditionally_template_single_add_to_cart', 1, 0 );
function replacing_conditionally_template_single_add_to_cart() {
global $product;
// Set HERE your product categories in the array
$categories = array( 't-shirts', 'gloves' );
if( has_term( $categories, 'product_cat', $product->get_id()) ){
// For variable product types
if( $product->is_type( 'variable' ) ){
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
// The button replacement
add_action( 'woocommerce_single_variation', 'custom_button_read_more', 20 );
}
else // For all other product types
{
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The button replacement
add_action( 'woocommerce_single_product_summary', 'custom_button_read_more', 30 );
}
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
所有代码都在 Woocommerce 3+ 上测试并且有效。
相似答案:
在 WooCommerce 中,我想替换添加到购物车按钮并将其更改为 "Read More" 但仅适用于某些产品类别 。
我尝试为此使用了很多不同的代码片段,但似乎没有任何东西能给我想要的结果。
原因是我想将我商店的某些部分用作产品目录,显示商品及其价格但不允许用户购买。
有谁知道how/if这可能吗?
任何帮助都将不胜感激。
以下是一些函数,它们将仅针对某些已定义的产品类别替代:
- 在商店和档案页面中:按钮通过 link 添加到购物车到产品
- 在单个产品页面中: 通过自定义按钮添加到购物车按钮和数量
您必须在该函数中定义:
- 数组中的产品类别(2次)
- "Read more"按钮产品link(用于简单产品页面)
代码如下:
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_conditionally_loop_add_to_cart_button', 10, 2 );
function replacing_conditionally_loop_add_to_cart_button( $button, $product ) {
// Set HERE your product categories in the array
$categories = array( 't-shirts', 'gloves' );
if( has_term( $categories, 'product_cat', $product->get_id() ) ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
// Button replacement function for Single product pages (SET the link below)
function custom_button_read_more( $product_link ){
global $product;
// Set HERE your product link
$product_link = home_url( '/some-link-to-define/' );
$button_text = __('Read more', 'woocommerce');
echo '<a href="'.$product_link.'" class="button">'.$button_text.'</a>';
}
// replacing add to cart button and quantities by your custom button in Single product pages
add_action( 'woocommerce_single_product_summary', 'replacing_conditionally_template_single_add_to_cart', 1, 0 );
function replacing_conditionally_template_single_add_to_cart() {
global $product;
// Set HERE your product categories in the array
$categories = array( 't-shirts', 'gloves' );
if( has_term( $categories, 'product_cat', $product->get_id()) ){
// For variable product types
if( $product->is_type( 'variable' ) ){
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
// The button replacement
add_action( 'woocommerce_single_variation', 'custom_button_read_more', 20 );
}
else // For all other product types
{
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The button replacement
add_action( 'woocommerce_single_product_summary', 'custom_button_read_more', 30 );
}
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
所有代码都在 Woocommerce 3+ 上测试并且有效。
相似答案: