| @@ -1,0 +1,97 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\WooCommerce\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +// Helper function to get product variations | |
| 12 | +function gamipress_wc_get_product_variations( $product_id ) { | |
| 13 | + | |
| 14 | + $product = wc_get_product( $product_id ); | |
| 15 | + | |
| 16 | + // Bail if product doesn't exists | |
| 17 | + if( ! $product ) { | |
| 18 | + return array(); | |
| 19 | + } | |
| 20 | + | |
| 21 | + // Bail if product is not variable | |
| 22 | + if( ! $product->is_type( 'variable' ) ) { | |
| 23 | + return array(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + $available_variations = $product->get_available_variations(); | |
| 27 | + | |
| 28 | + // Bail if there isn't any variations | |
| 29 | + if( ! is_array( $available_variations ) ) { | |
| 30 | + return array(); | |
| 31 | + } | |
| 32 | + | |
| 33 | + // Return product variations | |
| 34 | + return $available_variations; | |
| 35 | + | |
| 36 | +} | |
| 37 | + | |
| 38 | +// Helper function to get product variations dropdown | |
| 39 | +function gamipress_wc_get_product_variations_dropdown( $product_id, $selected = 0, $site_id = 0 ) { | |
| 40 | + | |
| 41 | + if( gamipress_is_network_wide_active() && $site_id !== get_current_blog_id() ) { | |
| 42 | + switch_to_blog( $site_id ); | |
| 43 | + $variations = gamipress_wc_get_product_variations( $product_id ); | |
| 44 | + restore_current_blog(); | |
| 45 | + } else { | |
| 46 | + $variations = gamipress_wc_get_product_variations( $product_id ); | |
| 47 | + } | |
| 48 | + | |
| 49 | + if( empty( $variations ) ) { | |
| 50 | + return ''; | |
| 51 | + } | |
| 52 | + | |
| 53 | + $output = '<select>'; | |
| 54 | + | |
| 55 | + foreach( $variations as $variation ) { | |
| 56 | + | |
| 57 | + $attributes = array(); | |
| 58 | + | |
| 59 | + foreach( $variation['attributes'] as $attribute ) { | |
| 60 | + if( ! empty( $attribute ) ) { | |
| 61 | + $attributes[] = $attribute; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + $output .= '<option value="' . $variation['variation_id'] . '" ' . selected( $selected, $variation['variation_id'], false ) . '>' . implode( ', ', $attributes ) . ' (#' . $variation['variation_id'] . ')</option>'; | |
| 66 | + } | |
| 67 | + | |
| 68 | + $output .= '</select>'; | |
| 69 | + | |
| 70 | + return $output; | |
| 71 | + | |
| 72 | +} | |
| 73 | + | |
| 74 | +// Helper function to get product variation title | |
| 75 | +function gamipress_wc_get_product_variation_title( $product_id, $variation_id, $site_id = 0 ) { | |
| 76 | + | |
| 77 | + if( gamipress_is_network_wide_active() && $site_id !== get_current_blog_id() ) { | |
| 78 | + switch_to_blog( $site_id ); | |
| 79 | + $variation_attributes = wc_get_product_variation_attributes( $variation_id ); | |
| 80 | + restore_current_blog(); | |
| 81 | + } else { | |
| 82 | + $variation_attributes = wc_get_product_variation_attributes( $variation_id ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + $attributes = array(); | |
| 86 | + | |
| 87 | + foreach( $variation_attributes as $attribute ) { | |
| 88 | + if( ! empty( $attribute ) ) { | |
| 89 | + $attributes[] = $attribute; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + return get_post_field( 'post_title', $product_id ) | |
| 94 | + . ( ! empty( $attributes ) ? ' (' . implode( ', ', $attributes ) . ')' : '' ); | |
| 95 | + | |
| 96 | +} | |
| 97 | + | |