【问题标题】:How to add CSS class "out-of-stock" to link if item is sold out woocommerce?如果商品已售罄woocommerce,如何添加 CSS 类“缺货”链接?
【发布时间】:2022-08-03 02:26:01
【问题描述】:

现在我有像这样的链接 <a href=\"/////\" class=\"woocommerce-LoopProduct-link woocommerce-loop-product__link\">

我需要 <a href=\"/////\" class=\"woocommerce-LoopProduct-link woocommerce-loop-product__link out-of-stock\">

    标签: wordpress woocommerce


    【解决方案1】:

    您可以使用动作挂钩 -woocommerce_before_shop_loop_item修改链接并添加您自己的 CSS 类。

    以下代码应该可以工作 -

    if ( ! function_exists( 'custom_woocommerce_template_loop_product_link_open' ) ) {
        /**
         * Insert the opening anchor tag for products in the loop.
         */
        function custom_woocommerce_template_loop_product_link_open() {
            global $product;
    
            $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
            
            if ( ! $product->is_in_stock() ) {
             $outOfStock = "out-of-stock";
            }
    
            echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link ' . $outOfStock .'">';
        }
    }
        
    add_action( 'woocommerce_before_shop_loop_item', 'custom_woocommerce_template_loop_product_link_open', 10 );
    

    您可以将此代码添加到您的主题 functions.php 文件或使用代码 sn-ps 插件。

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2021-04-26
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多