首页 文章

更改添加到购物车按钮颜色并禁用Woocommerce中的缺货产品

提问于
浏览
2

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

但我有点困惑

1 回答

  • 2

    请尝试以下代码:

    // 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文件中 . 经过测试和工作 .

    enter image description here


相关问题