My Favorite WooCommerce Snippets

//Filter out any products with 0 price
add_action( 'pre_get_posts', 'my_modify_main_query' );
function my_modify_main_query( $query ) {
    if( $query->is_main_query() && is_shop() && !is_admin()) {
    $query->set( 'posts_per_page', 12 );
    $meta_query2 = array(
        array(
            'key' => '_price',
            'value' => '0',
            'compare' => '>'
        )
    );
    $query->set( 'meta_query', $meta_query2 );
    }
}


//Order by SKU
//add_action( 'pre_get_posts', 'my_modify_main_query' );
function my_modify_main_query( $query ) {
    if( $query->is_main_query() && is_shop() && !is_admin()) {
    $query->set( 'posts_per_page', 12 );
    $query->set( 'meta_key', '_sku' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'ASC' );
    }
}

//Create custom Add to Cart button (can be used in a loop)
Buy now

//Prevent custom Add to Cart button that goes to Cart from adding two products
add_action('add_to_cart_redirect', 'resolve_dupes_add_to_cart_redirect');
function resolve_dupes_add_to_cart_redirect($url = false) {
  if(!empty($url)) { return $url; }
  return 'xxxxxxx.xxx' . add_query_arg(array(), remove_query_arg('add-to-cart'));
}

//Display an add to cart button using shortcode (use in woocommerce template or anywhere)
echo do_shortcode("[add_to_cart id=$product_id]");

//CSS for add to cart
.product.woocommerce.add_to_cart_inline {
    border: none !important;
    padding: 0 !important;
}
span.woocommerce-Price-amount.amount {
    display: none;
}



//add_action('add_attachment', 'attach_pics_to_prods');

function attach_pics_to_prods( $attachmentID ) {
  if ( ! class_exists( 'WC_Product' ) ) return; // if no WooCommerce do nothing

  // an attachment was jus saved, first of all get the file name
  $src = wp_get_attachment_image_src( $attachmentID, 'full' );
  $filename = pathinfo( $src[0], PATHINFO_FILENAME );

  $str_name = array_shift(explode('-', $filename));

  // now let's see if exits a product with the sku that match filename
  $info = '';
  $args = array(
    'post_type' => 'product',
    'meta_key' => '_sku',
    'meta_value' => $str_name,
    'meta_compare' => 'LIKE' // assuming sku is unique get only one post
  );
  $prods = get_posts( $args );


//cman250-1
//cman255-1
//cman255 (1)
  if ( ! empty($prods) && count($prods) > 1) { //if we found multiple products
    foreach ($prods as $product) {
        // ok we have a match, exists a product having sku that match filename
        if (has_post_thumbnail($product)) {

            delete_post_thumbnail($product);
        }
        // set the thumbnail for the product
        set_post_thumbnail( $product, $attachmentID );

        // now "attach" the post to the product setting 'post_parent'
        $attachment = get_post( $attachmentID );
        $attachment->post_parent = $product->ID;
        wp_update_post( $attachment );          
    }
  } else if (! empty($prods) && count($prods) == 1){ //if we only have one product
    // ok we have a match, exists a product having sku that match filename
    $product = array_pop( $prods );

    if (has_post_thumbnail($product)) {

        delete_post_thumbnail($product);
    }
    // set the thumbnail for the product
    set_post_thumbnail( $product, $attachmentID );

    // now "attach" the post to the product setting 'post_parent'
    $attachment = get_post( $attachmentID );
    $attachment->post_parent = $product->ID;
    wp_update_post( $attachment );
    } else {
        $no_match = $src[0] . '\n' . ': ' . $attachmentID;
        file_put_contents('no_match_woo_error.log', print_r($no_match, true), FILE_APPEND);
    }
};



//Sort by SKU unless Sort & Filter is trying to sort the products
add_action( 'pre_get_posts', 'my_modify_main_query' );
function my_modify_main_query( $query ) {
    if( $query->is_main_query() && is_shop() && !is_admin()) {
      //if Sort & Filter is trying to sort the products, do not sort by SKU
      if (get_query_var( 'sort_order', $default ) === NULL) {;
        $query->set( 'posts_per_page', 12 );
        $query->set( 'meta_key', '_sku' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'ASC' );
      }
    }
}