重命名已售罄产品的产品存档上的 "Select options" 按钮

Rename "Select options" button on products archive for sold out products

我有一个 shop 并且每个已售罄的产品尽管不可用,但仍在参考产品页面。 “产品选项”按钮仍然存在。所以我发现这段代码应该更改“添加到购物车”按钮(在我的例子中主要是 'select options' 因为我的大部分项目都是可变的)。 我只想为售罄的产品更改此按钮上的文本。我希望它声明“特殊订单”或其他内容。我想将他们转至外部页面。

我该怎么做?

add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {


    $sold_out = __( "Sold Out", "woocommerce" );

    $availability = $product->get_availability();
    $stock_status = $availability['class'];

    // Only for variable products on single product pages
    if ( $product->is_type('variable') && is_product() )
    {
    ?>
    <script>
    jQuery(document).ready(function($) {
        $('select').blur( function(){
            if( '' != $('input.variation_id').val() && $('p.stock').hasClass('out-of-stock') )
                $('button.single_add_to_cart_button').html('<?php echo $sold_out; ?>');
            else 
                $('button.single_add_to_cart_button').html('<?php echo $button_text; ?>');

            console.log($('input.variation_id').val());
        });
    });
    </script>
    <?php
    }
    // For all other cases (not a variable product on single product pages)
    elseif ( ! $product->is_type('variable') && ! is_product() ) 
    {
        if($stock_status == 'out-of-stock')
            $button_text = $sold_out.' ('.$stock_status.')';
        else
            $button_text.=' ('.$stock_status.')';
    }
    return $button_text;
}

以下代码片段可能会引导您走上正确的道路。

如果产品是变量产品,检查所有变体的库存状态并获取数组的值。如果可变产品的所有变体都缺货,则可以将文本更改为Sold Out。您还可以根据可变产品中变体的可用性更改文本。

if ( $product->is_type('variable') )
    {
        foreach ($product->get_available_variations() as $key => $value){
            $stock[] = isset( $value['is_in_stock'] ) ? $value['is_in_stock'] : true;
        }
        
        if( empty( array_filter( $stock ) ) ){
            $button_text = __( "Sold Out", "woocommerce" );
        }
    }
}

查看下面的完整代码段。

add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {

   $stock = array();

    // Only for variable products on single product pages
    if ( $product->is_type('variable') )
    {
        foreach ($product->get_available_variations() as $key => $value){
            $stock[] = isset( $value['is_in_stock'] ) ? $value['is_in_stock'] : true;
        }
        
        if( empty( array_filter( $stock ) ) ){
            $button_text = __( "Sold Out", "woocommerce" );
        }
    }
    // For all other cases (not a variable product on single product pages)
    elseif ( ! $product->is_type('variable') && !$product->is_in_stock() ) 
    {
        $button_text = __( "Sold Out", "woocommerce" );
    }
    return $button_text;
}