简体   繁体   English

如果 WooCommerce 中的所有变体都缺货,则显示已售罄的灰色按钮

[英]Display a Sold out greyed button if all variations are out of stock in WooCommerce

I have products in WooCommerce, with variations.我在 WooCommerce 中有产品,有变化。 If ALL variations are out of stock, I want to change the Add to Cart button text to say "Sold out" and edit the CSS of the button also (change it's color) BEFORE a variation is selected in the dropdown...如果所有变体都缺货,我想将“添加到购物车”按钮文本更改为“售罄”,并在下拉列表中选择变体之前编辑按钮的 CSS(更改其颜色)...

So here's a scenario:所以这是一个场景:

I goto a variable product single page.我转到可变产品单页。 The product has 4 variations:该产品有 4 个变体:

CURRENTLY: The "add to cart" button displays (greyed out) and can be clicked before a variation is selected.当前:“添加到购物车”按钮显示(变灰)并且可以在选择变体之前单击。 An alert appears telling the user to select a variation.会出现一个提醒,告诉用户选择一个变体。 When I select a variation from dropdown, the button is greyed out if out of stock in that variation.当我从下拉列表中选择一个变体时,如果该变体中缺货,该按钮将显示为灰色。 If all 4 variations are out of stock, the initial load of the page still shows add to cart button greyed out and on click says to select a variation.如果所有 4 个变体都缺货,页面的初始加载仍然显示添加到购物车按钮为灰色,点击时表示选择一个变体。

WHAT I WANT: If there is AT LEAST one variation still in stock, the standard WooCommerce functionality stays (Add To Cart visible, with alert popping up when clicked to tell them to select a variation).我想要什么:如果库存中至少有一个变体,则标准的 WooCommerce 功能将保持不变(添加到购物车可见,单击时会弹出警告以告诉他们选择变体)。 If NO variations are in stock, the Add to Cart button says "sold out" straight away, and is greyed out.如果库存中没有任何变体,“添加到购物车”按钮会立即显示“售罄”,并且显示为灰色。 (Before I select a variation). (在我选择一个变体之前)。

The problem I'm finding is that all the existing code out there to change the add to cart button text is done when a variation is selected from the dropdown.我发现的问题是,当从下拉列表中选择一个变体时,所有用于更改添加到购物车按钮文本的现有代码都已完成。 I somehow need to check if ANY of the variations are in stock, (before they are selected) and then either change the button text to "sold out" if ALL variations are out of stock, or leave it at first load, and change the text only when the out of stock variation is selected.我以某种方式需要检查是否有任何变体有库存(在选择它们之前),然后如果所有变体都缺货,则将按钮文本更改为“售罄”,或者在第一次加载时将其保留,然后更改text only when the out of stock variation is selected.

I've tried the following code:我试过以下代码:

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

This changes the buton text, but only when variations are selected - I need to check if all variations are out of stock and then change the text straight away.这改变了Buton文本,但只有在选择变体时 - 我需要检查所有变体是否缺货,然后立即更改文本。

The following code will display an inactive greyed "sold out" button for variable products, when all variations are out of stock:当所有变体都缺货时,以下代码将为可变产品显示一个非活动的灰色“售罄”按钮:

// Single variable produccts pages - Sold out functionality
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    global $product;

    // For variable product types
    if( $product->is_type( 'variable' ) ) {
        $is_soldout = true;
        foreach( $product->get_available_variations() as $variation ){
            if( $variation['is_in_stock'] )
                $is_soldout = false;
        }
        if( $is_soldout ){
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'sold_out_button', 20 );
        }
    }
}

// The sold_out button replacement
function sold_out_button() {
    global $post, $product;

    ?>
    <div class="woocommerce-variation-add-to-cart variations_button">
        <?php
            do_action( 'woocommerce_before_add_to_cart_quantity' );

            woocommerce_quantity_input( array(
                'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
                'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
            ) );

            do_action( 'woocommerce_after_add_to_cart_quantity' );
        ?>
        <a class="single_sold_out_button button alt disabled wc-variation-is-unavailable"><?php _e( "Sold Out", "woocommerce" ); ?></a>
    </div>
    <?php
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示在 WooCommerce 可变产品上售罄,当所有变体都缺货时 - Display sold out on WooCommerce variable product when all variations are out of stock 如果 WooCommerce 中的一个变体缺货,则将所有变体设置为缺货 - Set all variations out of stock if one variation is out of stock in WooCommerce 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations 如果所有变体都缺货,则从目录中隐藏 WooCommerce 可变产品 - Hide WooCommerce variable product from catalog if all variations are out of stock 显示和隐藏缺货变化 woocommerce - Showing and hiding out of stock variations woocommerce woocommerce-显示库存变化 - woocommerce - display variations in stock 如何使用 PayPal 智能按钮在商品缺货时显示售罄消息 - How to display sold out message once an itemruns out of stock with PayPal smart button 如何在某个 Woocommerce 类别存档页面中显示已售出/缺货商品,但不在其他页面中显示它们? - How do I show sold/out of stock items in a certain Woocommerce category archive page, but not display them in others? 向显示为色板的变体添加“缺货”类 - WooCommerce - Adding an “out-of-stock” class to variations shown as swatches - WooCommerce 通过 WooCommerce 中的自定义库存数量减少来灰显“缺货”产品变化 - Greying out “out-of-stock” product variations with custom stock quantity reduction in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM