简体   繁体   English

如果Woocommerce中的产品缺货,请用表格替换数量字段

[英]Replace quantity field with a form if product is out of stock in Woocommerce

In woocommerce, using contact Form 7 plugin, I'm trying to replace the product quantity field, in the product summary, with a form, when a product is out of stock. 在woocommerce中,使用联系Form 7插件,当产品缺货时,我试图用表格替换产品摘要中的产品数量字段。

It works fine on variable products but on simple products it still shows the form and the quantity box. 它在各种产品上都可以正常工作,但在简单的产品上,它仍然显示表格和数量框。

It feels like I'm overlooking something very basic. 感觉就像我忽略了一些非常基本的内容。

I've replaced the different echo with "simple" and "variable" to find out which form is shown, but on simple products it still shows the 'variable' form . 我已经用“ simple”和“ variable”替换了不同的echo ,以找出显示的是哪种形式, 但是在简单的产品上,它仍然显示'variable'形式

Here is my code: 这是我的代码:

add_action( 'woocommerce_single_product_summary', 'add_form' );
function add_form() {
    global $product;

    if( $product->is_type( 'simple' ) ){
        // a simple product
        if(!$product->is_in_stock( )) {
            echo do_shortcode('[contact-form-7 id="304" title="Contact stock"]');
            //echo "simple";
        }
    } elseif( $product->is_type( 'variable' ) ){
        // a variable product
        $count_in_stock == 0;
        $variation_ids = $product->get_children(); // Get product variation IDs

        foreach( $variation_ids as $variation_id ){
            $variation = wc_get_product($variation_id);
            if( $variation->is_in_stock() )
                $count_in_stock++;
        }   
    }

    if( $count_in_stock == 0 ) {
        echo do_shortcode('[contact-form-7 id="304" title="Contact stock"]');
        //echo "variable";
    }   
}

Try the following code, that will replace quantity field and add to cart button with a form when the product is "out of stock" (for all product types, including variable products) . 尝试以下代码,当产品“缺货”时(对于所有产品类型,包括可变产品) ,它将替换数量字段使用表格添加到购物车按钮

You say "on simple products it still shows the 'variable' form" : It's because you are using the same shortcode on both simple and variable products. 您说“在简单产品上,它仍然显示'可变'形式” :这是因为在简单产品和可变产品上都使用相同的简码。 So you will need to add the correct different shortcode for simple products. 因此,您将需要为简单产品添加正确的不同简码。

The code: 编码:

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 4 );
function action_single_product_summary_callback() {
    global $product;

    // Variable products
    if ( $product->is_type( 'variable' ) ){
        $count_in_stock = 0;

        foreach ( $product->get_visible_children() as $variation_id ) {
            $variation = wc_get_product($variation_id);

            if( $variation->is_in_stock() ) {
                $count_in_stock++;
            }
        }
        if ( $count_in_stock === 0 ) {
            // Remove quantity field and add to cart button
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            // Display the contact form
            add_action( 'woocommerce_single_variation', 'display_variable_product_out_of_stock_form', 20 );
        }
    }
    // Other products (Simple … )
    else {
        if ( ! $product->is_in_stock() ) {
            // Remove quantity field and add to cart button
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            // Display the contact form
            add_action( 'woocommerce_single_product_summary', 'display_simple_product_out_of_stock_form', 30 );
        }
    }
}

// Form for variable products
function display_variable_product_out_of_stock_form() {
    echo do_shortcode('[contact-form-7 id="304" title="Contact stock"]');
}

// Form for Simple products
function display_simple_product_out_of_stock_form() {
    echo do_shortcode('[contact-form-7 id="304" title="Contact stock"]'); // <== NOT the correct shortcode
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM