This post with WooCommerce code snippets is mostly for myself, but I also answer questions at the WooCommerce Community Boards periodically, and this list of snippets can be useful for answering those questions.
/**
* Remove WooCommerce Updater Notice
*/
remove_action('admin_notices', 'woothemes_updater_notice');
/**
* Code to fix HTTP Error on uploads
*/
add_filter( 'wp_image_editors', 'change_graphic_lib' );
function change_graphic_lib($array) {
return array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
}
/************* DECLARE WOOCOMMERCE SUPPORT ***************/
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
/***
* Add notice to WooCommerce pages
*/
function my_update_notice() {
echo '<div class="update-nag notice">';
echo '<p>';
_e( 'In order for Room View to work properly with your artwork, you must enter your artwork size as HEIGHT by WIDTH. For example if you have a painting that is 20cm tall and 40cm wide, you would select your size as 020 x 40 cm', 'anphira' );
echo '</p>';
echo '</div>';
}
add_action( 'admin_notices', 'my_update_notice' );
/***********************************
* Require image to publish product
***********************************/
function on_all_status_transitions( $new_status, $old_status, $post ) {
/* If new status is draft and post type is product, then set original to manage stock & qty to 1 */
if ( $new_status == 'draft' && !empty($post->ID) && in_array( $post->post_type, array( 'product') ) ) {
$post = wc_get_product( $post->ID );
if($post->get_type() == 'variable') {
$variations = $post->get_available_variations();
foreach ($variations as $variation) {
if( ( ! isset( $variation->manage_stock ) || ($variation->manage_stock == 'no' ) ) && ($variation['attributes']['attribute_pa_type-slug'] == 'original') ) {
add_post_meta( $variation->id, '_stock', 1, true );
update_post_meta( $variation['variation_id'], '_stock', '1');
update_post_meta( $variation['variation_id'], '_stock_status', 'instock');
update_post_meta( $variation['variation_id'], '_manage_stock', 'yes');
}
}
}
}
/* If new status is publish and post type is product, then require featured image */
elseif ( $new_status == 'publish' && !empty($post->ID) && in_array( $post->post_type, array( 'product') ) ) {
if( ! has_post_thumbnail($post) ) {
$post->post_status = 'draft';
// unhook this function so it doesn't loop infinitely
remove_action('transition_post_status', 'on_all_status_transitions' );
wp_update_post($post);
// re-hook this function
add_action('transition_post_status', 'on_all_status_transitions', 10, 3 );
}
}
}
add_action( 'transition_post_status', 'on_all_status_transitions', 10, 3 );
