Created
July 18, 2017 06:01
-
-
Save tanmay27vats/270524d948835da4eb3acaa90e563b32 to your computer and use it in GitHub Desktop.
Get product's default variation ID, variation price or variation object without plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tv_find_matching_product_variation( $product, $attributes ) | |
{ | |
foreach( $attributes as $key => $value ) | |
{ | |
if( strpos( $key, 'attribute_' ) === 0 ) | |
{ | |
continue; | |
} | |
unset( $attributes[ $key ] ); | |
$attributes[ sprintf( 'attribute_%s', $key ) ] = $value; | |
} | |
if( class_exists('WC_Data_Store') ) | |
{ | |
$data_store = WC_Data_Store::load( 'product' ); | |
return $data_store->find_matching_product_variation( $product, $attributes ); | |
} | |
else | |
{ | |
return $product->get_matching_variation( $attributes ); | |
} | |
} | |
//on product single page. | |
/* $attributes something like this. */ | |
/*$attributes = array( | |
'pa_size' => 'small', | |
'pa_color' => 'red', | |
'pa_brand' => 'nike' | |
);*/ | |
$variation_id = milkbed_find_matching_product_variation($product, $product->default_attributes); | |
$variable_product = wc_get_product($variation_id); | |
//$default_variation_regular_price = $variable_product->regular_price; | |
//$default_variation_sale_price = $variable_product->sale_price; | |
$default_variation_price = $variable_product->price; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment