【问题标题】:Change add to cart button color and disable it for out of stock products in Woocommerce更改添加到购物车按钮颜色并禁用 Woocommerce 中的缺货产品
【发布时间】:2018-10-19 17:06:54
【问题描述】:

在 woocommerce 中,我已将添加到购物车按钮重定向更改为添加到结帐页面。

当某些产品在类别或主页中缺货时,添加到结帐按钮会错过内容图标。

如何使用 php 更改按钮的颜色并为按钮按下事件禁用它

我应该使用类似的东西吗:

add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_stock', 10 );
function woocommerce_template_loop_stock() {
global $product;
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
    echo '<p class="stock out-of-stock">Out of Stock</p>';
}

但我有点困惑

【问题讨论】:

    标签: php wordpress button woocommerce product


    【解决方案1】:

    试试下面的代码:

    // For Woocommerce version 3 and above only
    add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link', 20, 3 );
    function filter_loop_add_to_cart_link( $button, $product, $args = array() ) {
        if( $product->is_in_stock() ) return $button;
    
        // HERE set your button text (when product is not on stock)
        $button_text = __('Not available', 'woocommerce');
    
        // HERE set your button STYLING (when product is not on stock)
        $color = "#777";      // Button text color
        $background = "#aaa"; // Button background color
    
    
        // Changing and disbling the button when products are not in stock
        $style = 'color:'.$color.';background-color:'.$background.';cursor:not-allowed;';
        return sprintf( '<a class="button disabled" style="%s">%s</a>', $style, $button_text );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 1970-01-01
      • 2018-07-07
      • 2017-11-23
      • 2021-07-21
      • 2019-02-20
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多