【问题标题】:Change add to cart button and text based on WooCommerce product type根据 WooCommerce 产品类型更改添加到购物车按钮和文本
【发布时间】:2021-04-24 18:39:49
【问题描述】:

如何更改产品列表循环中的 WooCommerce 添加到购物车按钮,但取决于产品类型,例如:

  1. 对于有变体的产品,我想在“添加到购物车”按钮中添加一个文本:“显示产品”
  2. 对于简单的产品“显示产品”
  3. 对于缺货的产品:“不可用”

我尝试使用以下代码但不起作用:

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;
}

【问题讨论】:

    标签: php wordpress woocommerce product archive


    【解决方案1】:

    请尝试以下方法:

    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>';
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该工作

    【讨论】:

    • 是的!谢谢 !我只从那里删除了 $product->managing_stock()
    【解决方案2】:

    要更改 woocommerce Book Now 按钮,我安装了这个插件 https://wordpress.org/plugins/button-customizer-for-woocommerce/ 并在 functions.php 中添加了 2 个过滤器,最后,它可以工作了

     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";
    } 
    

    【讨论】:

    猜你喜欢
    • 2018-11-12
    • 2018-10-04
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多