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

如果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.

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.

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

在woocommerce中,使用联系表格7 插件,我想在产品缺货时用产品摘要替换产品数量字段。 p>

它可以在变量产品上正常工作,但在简单的产品上它仍会显示表格和数量框。 p>

感觉我忽略了一些非常基本的东西。

我用“simple”和“variable”替换了不同的 echo code>以找出显示的表单,但是在简单的产品上它仍然显示 '变量'形式 strong>。 p>

这是我的代码: p>

  add_action('woocommerce_single_product_summary','add_form')  ; 
_function add_form(){
 global $ product; 
 
 if if($ product-> is_type('simple')){
 //一个简单的产品
 if(!$ product-> is_in_stock  ()){
 echo do_shortcode('[contact-form-7 id =“304”  title =“Contact stock”]'); 
 // echo“simple”; 
} 
} elseif($ product-> is_type('variable')){
 //一个变量product 
 $  count_in_stock == 0; 
 $ variation_ids = $ product-> get_children();  //获取产品变体ID 
 
 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“ 变量“; 
} 
} 
  code>  pre> 
  div>

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). Tested and works.