Ganga
Ganga

Reputation: 797

WooComerce, if custom checkbox is checked hide stock status

I have a bit trouble with woocomerce checkbox, i add custom checkbox to product page wit this code:

 woocommerce_wp_checkbox( 
array( 
    'id'            => '_checkbox', 
    'wrapper_class' => 'show_if_simple', 
    'label'         => __('My Checkbox Field', 'woocommerce' ), 
    'description'   => __( 'Check me!', 'woocommerce' ) 
    )
);
}

then save value with this:

    $woocommerce_checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox );

Now i tried to write function which make my stock status hiden when this checkbox is checked but i fail , can i ask you guys for some support ?

Upvotes: 3

Views: 1692

Answers (1)

Domain
Domain

Reputation: 11808

If the code for saving the option of checkbox works fine and the saved option for that products reflects in the database then adding the following code will help to to complete your task

add_filter('woocommerce_stock_html','wdm_remove_stock_html',10,3);

function wdm_remove_stock_html($availability_html, $availability, $product)
{
if ( 'yes' === get_post_meta( $product->id,'_checkbox', true) ) {
    return '';
}else{
 return $availability_html;   
}
}

Upvotes: 1

Related Questions