简体   繁体   English

根据 WooCommerce 产品类型更改添加到购物车按钮和文本

[英]Change add to cart button and text based on WooCommerce product type

How to change a WooCommerce add to cart button in Product List loop but depending on the product type, like for example:如何在产品列表循环中更改 WooCommerce 添加到购物车按钮,但取决于产品类型,例如:

  1. For products with Variations I want a text in add to cart button to: "Show product"对于有变体的产品,我希望在“添加到购物车”按钮中添加一个文本:“显示产品”
  2. For Simple prodcuts "Show product"对于简单的产品“显示产品”
  3. For products Out of stock: "Unavailable"对于缺货的产品:“不可用”

I tried with below code but doesn't work:我尝试使用以下代码但不起作用:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    $button_text = __( "Out of stock", "woocommerce" );
    return '<a class="view-product" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    if( ! $product->managing_stock() && ! $product->is_in_stock() ) {
        return $button;
    }
    if( $product->is_type( 'variable' ) ) return $button;
}

Try the following instead:请尝试以下操作:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Out of stock products
    if( ! $product->is_in_stock() ) {
        $button_text = __( "Unavailable", "woocommerce" );
    }
    // Simple and Variable products
    elseif( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) {
        $button_text = __( "Show product", "woocommerce" );
    } 
    // Other product types
    else {
        $button_text = add_to_cart_text(); 
    }
    
    return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should work它应该工作

This doesnt seem to update anything for me.这似乎对我没有任何更新。 Ive added it in the child functions.php file but there was no change我已将其添加到子函数中。php 文件但没有变化

I installed this plugin https://wordpress.org/plugins/button-customizer-for-woocommerce/ and added 2 filters to functions.php and finally, it works我安装了这个插件https://wordpress.org/plugins/button-customizer-for-woocommerce/并为函数添加了 2 个过滤器。php 最后,它可以工作了

 add_filter( 'woocommerce_booking_single_check_availability_text', 'wooninja_booking_check_availability_text' );
function wooninja_booking_check_availability_text() {
    return "your text";
} 
 add_filter( 'woocommerce_booking_single_add_to_cart_text', 'wooninja_woocommerce_booking_single_add_to_cart_text' );
function wooninja_woocommerce_booking_single_add_to_cart_text() {
    return "your text";
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Woocommerce中的父产品类别更改“添加到购物车”按钮文本 - Change Add To Cart button text based on parent product categories in Woocommerce 基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮 - Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce 当产品在 Woocommerce 的购物车中时,更改添加到购物车按钮样式 - Change add to cart button style when product is in cart in Woocommerce WooCommerce:当产品已在购物车中时更改添加到购物车的文本 - WooCommerce: change the add to cart text when the product is already in cart WooCommerce-产品已经在购物车中,更改“添加到购物车”文本为ajax - WooCommerce - Product already in cart, change “add to cart” text ajax 在Woocommerce单个产品页面中的单击事件上更改添加到购物车按钮文本 - Change add to cart button text on click event in Woocommerce single product pages 根据 WooCommerce 产品自定义库存状态禁用添加到购物车按钮 - Disable add to cart button based on WooCommerce product custom stock status 更改Woocommerce 3中特定产品类别的“添加到购物车”文本 - Change the add to cart text for a specific product category in Woocommerce 3 在 WooCommerce 中产品缺货时更改单个添加到购物车文本 - Change single add to cart text when product is out of stock in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM