• Hi,
    when using SKU in attribute variations, the search results on a fatal error :

    
    Fatal error:  Uncaught exception 'Exception' with message 'Invalid Product.' in /wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php:134
    Stack trace:
    #0 /wp-content/plugins/woocommerce/includes/class-wc-data-store.php(147): WC_Product_Data_Store_CPT->read(Object(WC_Product))
    #1 /wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(126): WC_Data_Store->read(Object(WC_Product))
    #2 /wp-content/plugins/woo-search-on-product-sku/index.php(19): WC_Product->__construct('1131')
    #3 /wp-content/plugins/woo-search-on-product-sku/index.php(31): wsops_get_product_by_sku('MDCM')
    #4 [internal function]: wsops_product_redirect('')
    #5 /wp-includes/class-wp-hook.php(298): call_user_func_array('wsops in /wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php on line 134
    

    This is a blocking issue.

    • This topic was modified 6 years, 6 months ago by Bastien Ho.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Bastien Ho

    (@bastho)

    This patch does the trick:

    
    <?php
         function wsops_get_product_by_sku( $sku ) {
    	  global $wpdb;
    	  $un_spaced_sku = str_replace(' ', '', $sku);
    	  $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND (meta_value='%s'  OR meta_value='%s') LIMIT 1", $sku , $un_spaced_sku ) );
    
    	  $post_type = get_post_type($product_id);
    		if ( $product_id && $post_type=='product_variation') {
    			$product_variation = get_post($product_id);
    			if(!is_wp_error($product_variation) && $product_variation->post_parent){
    				return new WC_Product( $product_variation->post_parent );
    			}
    	  }elseif ( $product_id && $post_type=='product') {
    		return new WC_Product( $product_id );
    	  }
    		return null;
    	}
    ?>
    Thread Starter Bastien Ho

    (@bastho)

    Hi,

    any chance to see this patch implemetend ?

    BR.

    • This reply was modified 6 years, 6 months ago by Bastien Ho.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fatal error in variation SKU’ is closed to new replies.