Guest User

Untitled

a guest
Dec 11th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. <?php
  2. global $product;
  3.  
  4. $variation_ids = array();
  5.  
  6. if( $product->is_type('variable') ){
  7. $default_attributes = $product->get_default_attributes();
  8. foreach($product->get_available_variations() as $variation_values ){
  9. foreach($variation_values['attributes'] as $key => $attribute_value ){
  10. $attribute_name = str_replace( 'attribute_', '', $key );
  11. if( $attribute_name == 'pa_color'){
  12. $default_value = $product->get_variation_default_attribute($attribute_name);
  13. if( $default_value == $attribute_value ){
  14. // We set all related variation IDs in an array
  15. $variation_ids[] = $variation_values['variation_id'];
  16. }
  17. }
  18. }
  19. }
  20.  
  21. if( count( $variation_ids ) > 0 ){
  22. // Iterating through each variation ID
  23. foreach( $variation_ids as $variation_id ){
  24. // Get the "default" WC_Product_Variation object
  25. $variation = wc_get_product($variation_id);
  26. // Get variation attribute values
  27. $variation_attributte = $variation->get_variation_attributes();
  28. // Raw output variation attribute values
  29. echo '<pre>'; print_r($variation_attributte); echo '</pre>';
  30. // Get the active price
  31. $price = $variation->get_price();
  32. // Output price
  33. echo '<p>Price: ' . $price . '</p>';
  34. }
  35. }
  36. }
  37. ?>
Add Comment
Please, Sign In to add comment