Woocommerce - 删除添加到购物车的按钮功能,并更改按钮文字
在WooCommerce我想替换添加到购物车按钮,并将其更改为“更多”但仅限于某些产品类别。Woocommerce - 删除添加到购物车的按钮功能,并更改按钮文字
我已经尝试过使用相当多的不同代码片段,但没有任何东西似乎给我我想要的结果。
原因是我想使用我店的某些部分作为显示商品和价格的产品目录,但不允许用户购买它们。
有没有人知道如何/如果这是可能的?
任何帮助都将不胜感激。
这里有一些功能,它将取代一些定义的产品类别只:
- 在店铺和档案网页:按钮被链接添加到购物车的产品
- 在单品页:通过您的自定义按钮添加到购物车按钮,批量
你将不得不限定在功效:
- 阵列中的产品类别(2次)
- 的“更多”按钮的产品链接(简单的产品页)
下面是代码:
// 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+上测试并正常工作。
类似的答案:Customize "Add to cart" button for a specific product category in WooCommerce
嘿@LoicTheAztec,对不起,因此延迟尝试此码。但是,我刚刚测试了它(在woocommerce 3.1.2),它不工作。有任何想法吗?目前我的fuctions.php中粘贴https://hastebin.com/dimurugoyo.php –
@LBlakemore我真的不知道为什么不能为你工作...可能是你的主题已经有一些定制...我已经重新测试过代码,它完全适合我在2个不同的测试服务器(WC 3.1.2)... – LoicTheAztec
请做一些更多的研究,有很多目录模式插件,有可以为你做这个。 – Kodaloid