Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

php - Is product_visibility taxonomy set to false when a product get "in stock" in WooCommerce?

Based on Hide WooCommerce variable product from catalog if all variations are out of stock answer code, I have made a variable product out of stock after checking that all of it's variation stock quantities are less than 0:

My code attempt:

function is_wc_variable_product_in_stock( $product_id ){
    global $wpdb;

    $count = $wpdb->get_var( $wpdb->prepare( "
        SELECT COUNT(ID)
        FROM {$wpdb->posts} p
        INNER JOIN {$wpdb->postmeta} pm
            ON p.ID           =  pm.post_id
        WHERE p.post_type     =  'product_variation'
            AND p.post_status =  'publish'
            AND p.post_parent =  %d
            AND pm.meta_key   =  '_stock'
            AND pm.meta_value != '0'
    ", $product_id ) );

    return $count > 0 ? true : false;
}

/* Set product out of stock if all it's variations are out of stock*/
function hide_out_of_stock_product() {
    global $product;
    $product_id = $product->get_id();
    $is_prod_in_stock = is_wc_variable_product_in_stock( $product_id );
    if ( ! $is_prod_in_stock && $product->get_type() === 'variable' ) {
        $out_of_stock_staus = 'outofstock';
        // Updating the product status
        update_post_meta( $product_id, '_stock_status', wc_clean( $out_of_stock_staus ) );
        // Updating post term relationship
        wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );
    }
}
add_action( 'woocommerce_after_shop_loop_item', 'hide_out_of_stock_product', 9 );

In the above code I've set the product_visiblity to true for the outofstock products. Now my question is that will the product_visiblity be set to false when the product is made instock from the dashboard ?

Hope my question is clear. Also correct me if the logic is flawed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not sure that you are using the correct hook…

When a product is back in stock, WooCommerce remove outofstock term (term id) for "product_visibility" taxonomy from post term relationship and also set post meta data value to "instock" for _stock_status meta key.

To remove post term relationship you certainly use wp_remove_object_terms() function.

So you need to make some changes in your 2nd function to make variable products in stock when any of its variations is in stock (untested):

add_action( 'woocommerce_after_shop_loop_item', 'show_hide_variable_product_based_on_stock', 9 );
function show_hide_variable_product_based_on_stock() {
    global $product;
    
    $product_id       = $product->get_id();
    $is_prod_in_stock = is_wc_variable_product_in_stock( $product->get_id() );
    $current_status   = get_post_meta( $product_id, '_stock_status', true );
    $status_to_set    = '';
    
    if ( $product->is_type('variable') ) {
        $status_to_set    = ''; 
        if ( ! $is_prod_in_stock && $current_status === 'instock' ) {
            $status_to_set = 'outofstock';
        } elseif ( $is_prod_in_stock && $current_status === 'outofstock' ) {
            $status_to_set = 'instock';
        } else {
            $status_to_set = '';
        }
        if ( ! empty($status_to_set) ) {
            // Updating the product status (meta data)
            update_post_meta( $product_id, '_stock_status', $status_to_set ); 
            
            // Add or Remove "outofstock" post term relationship for "product_visibility" taxonomy
            if( $status_to_set === 'outofstock' ) {
                wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true ); // Add
            } else {
                wp_remove_object_terms( $product_id, 'outofstock', 'product_visibility' ); // Remove
            }
        } 
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...