function woo_ce_get_product_data($product_id = 0, $args = array()) { // Get Product defaults $weight_unit = get_option('woocommerce_weight_unit'); $dimension_unit = get_option('woocommerce_dimension_unit'); $height_unit = $dimension_unit; $width_unit = $dimension_unit; $length_unit = $dimension_unit; $product = get_post($product_id); $_product = function_exists('wc_get_product') ? wc_get_product($product_id) : false; $product->parent_id = ''; $product->parent_sku = ''; if ($product->post_type == 'product_variation') { // Assign Parent ID for Variants then check if Parent exists if ($product->parent_id = $product->post_parent) { $product->parent_sku = get_post_meta($product->post_parent, '_sku', true); } else { $product->parent_id = ''; } } $product->product_id = $product_id; $product->sku = get_post_meta($product_id, '_sku', true); $product->name = get_the_title($product_id); $product->permalink = get_permalink($product_id); $product->product_url = method_exists($_product, 'get_permalink') ? $_product->get_permalink() : get_permalink($product_id); $product->slug = $product->post_name; $product->description = $product->post_content; $product->excerpt = $product->post_excerpt; $product->regular_price = get_post_meta($product_id, '_regular_price', true); // Check that a valid price has been provided and that wc_format_localized_price() exists if (isset($product->regular_price) && $product->regular_price != '' && function_exists('wc_format_localized_price')) { $product->regular_price = wc_format_localized_price($product->regular_price); } $product->price = get_post_meta($product_id, '_price', true); if ($product->regular_price != '' && $product->regular_price != $product->price) { $product->price = $product->regular_price; } // Check that a valid price has been provided and that wc_format_localized_price() exists if (isset($product->price) && $product->price != '' && function_exists('wc_format_localized_price')) { $product->price = wc_format_localized_price($product->price); } $product->sale_price = get_post_meta($product_id, '_sale_price', true); // Check that a valid price has been provided and that wc_format_localized_price() exists if (isset($product->sale_price) && $product->sale_price != '' && function_exists('wc_format_localized_price')) { $product->sale_price = wc_format_localized_price($product->sale_price); } $product->sale_price_dates_from = woo_ce_format_sale_price_dates(get_post_meta($product_id, '_sale_price_dates_from', true)); $product->sale_price_dates_to = woo_ce_format_sale_price_dates(get_post_meta($product_id, '_sale_price_dates_to', true)); $product->post_date = woo_ce_format_date($product->post_date); $product->post_modified = woo_ce_format_date($product->post_modified); $product->type = woo_ce_get_product_assoc_type($product_id); if ($product->post_type == 'product_variation') { $product->type = __('Variation', 'woo_ce'); } $product->visibility = woo_ce_format_visibility(get_post_meta($product_id, '_visibility', true)); $product->featured = woo_ce_format_switch(get_post_meta($product_id, '_featured', true)); $product->virtual = woo_ce_format_switch(get_post_meta($product_id, '_virtual', true)); $product->downloadable = woo_ce_format_switch(get_post_meta($product_id, '_downloadable', true)); $product->weight = get_post_meta($product_id, '_weight', true); $product->weight_unit = $product->weight != '' ? $weight_unit : ''; $product->height = get_post_meta($product_id, '_height', true); $product->height_unit = $product->height != '' ? $height_unit : ''; $product->width = get_post_meta($product_id, '_width', true); $product->width_unit = $product->width != '' ? $width_unit : ''; $product->length = get_post_meta($product_id, '_length', true); $product->length_unit = $product->length != '' ? $length_unit : ''; $product->category = woo_ce_get_product_assoc_categories($product_id, $product->parent_id); $product->tag = woo_ce_get_product_assoc_tags($product_id); $product->manage_stock = woo_ce_format_switch(get_post_meta($product_id, '_manage_stock', true)); $product->allow_backorders = woo_ce_format_switch(get_post_meta($product_id, '_backorders', true)); $product->sold_individually = woo_ce_format_switch(get_post_meta($product_id, '_sold_individually', true)); $product->upsell_ids = woo_ce_get_product_assoc_upsell_ids($product_id); $product->crosssell_ids = woo_ce_get_product_assoc_crosssell_ids($product_id); $product->quantity = get_post_meta($product_id, '_stock', true); $product->stock_status = woo_ce_format_stock_status(get_post_meta($product_id, '_stock_status', true), $product->quantity); $product->image = woo_ce_get_product_assoc_featured_image($product_id); $product->product_gallery = woo_ce_get_product_assoc_product_gallery($product_id); $product->tax_status = woo_ce_format_tax_status(get_post_meta($product_id, '_tax_status', true)); $product->tax_class = woo_ce_format_tax_class(get_post_meta($product_id, '_tax_class', true)); $product->external_url = get_post_meta($product_id, '_product_url', true); $product->button_text = get_post_meta($product_id, '_button_text', true); $product->file_download = woo_ce_get_product_assoc_file_downloads($product_id); $product->download_limit = get_post_meta($product_id, '_download_limit', true); $product->download_expiry = get_post_meta($product_id, '_download_expiry', true); $product->download_type = woo_ce_format_download_type(get_post_meta($product_id, '_download_type', true)); $product->purchase_note = get_post_meta($product_id, '_purchase_note', true); $product->product_status = woo_ce_format_product_status($product->post_status); $product->enable_reviews = woo_ce_format_comment_status($product->comment_status); $product->menu_order = $product->menu_order; // Attributes // Scan for global Attributes first if ($attributes = woo_ce_get_product_attributes()) { if ($product->post_type == 'product_variation') { // We're dealing with a single Variation, strap yourself in. foreach ($attributes as $attribute) { $attribute_value = get_post_meta($product_id, sprintf('attribute_pa_%s', $attribute->attribute_name), true); if (!empty($attribute_value)) { $term_id = term_exists($attribute_value, sprintf('pa_%s', $attribute->attribute_name)); if ($term_id !== 0 && $term_id !== null && !is_wp_error($term_id)) { $term = get_term($term_id['term_id'], sprintf('pa_%s', $attribute->attribute_name)); $attribute_value = $term->name; unset($term); } unset($term_id); } $product->{'attribute_' . $attribute->attribute_name} = $attribute_value; unset($attribute_value); } } else { // Either the Variation Parent or a Simple Product, scan for global and custom Attributes $product->attributes = maybe_unserialize(get_post_meta($product_id, '_product_attributes', true)); if (!empty($product->attributes)) { // Check for taxonomy-based attributes foreach ($attributes as $attribute) { if (isset($product->attributes['pa_' . $attribute->attribute_name])) { $product->{'attribute_' . $attribute->attribute_name} = woo_ce_get_product_assoc_attributes($product_id, $product->attributes['pa_' . $attribute->attribute_name], 'product'); } else { $product->{'attribute_' . $attribute->attribute_name} = woo_ce_get_product_assoc_attributes($product_id, $attribute, 'global'); } } // Check for per-Product attributes (custom) foreach ($product->attributes as $key => $attribute) { if ($attribute['is_taxonomy'] == 0) { if (!isset($product->{'attribute_' . $key})) { $product->{'attribute_' . $key} = $attribute['value']; } } } } } } // Advanced Google Product Feed - http://plugins.leewillis.co.uk/downloads/wp-e-commerce-product-feeds/ if (function_exists('woocommerce_gpf_install')) { $product->gpf_data = get_post_meta($product_id, '_woocommerce_gpf_data', true); $product->gpf_availability = isset($product->gpf_data['availability']) ? woo_ce_format_gpf_availability($product->gpf_data['availability']) : ''; $product->gpf_condition = isset($product->gpf_data['condition']) ? woo_ce_format_gpf_condition($product->gpf_data['condition']) : ''; $product->gpf_brand = isset($product->gpf_data['brand']) ? $product->gpf_data['brand'] : ''; $product->gpf_product_type = isset($product->gpf_data['product_type']) ? $product->gpf_data['product_type'] : ''; $product->gpf_google_product_category = isset($product->gpf_data['google_product_category']) ? $product->gpf_data['google_product_category'] : ''; $product->gpf_gtin = isset($product->gpf_data['gtin']) ? $product->gpf_data['gtin'] : ''; $product->gpf_mpn = isset($product->gpf_data['mpn']) ? $product->gpf_data['mpn'] : ''; $product->gpf_gender = isset($product->gpf_data['gender']) ? $product->gpf_data['gender'] : ''; $product->gpf_age_group = isset($product->gpf_data['age_group']) ? $product->gpf_data['age_group'] : ''; $product->gpf_color = isset($product->gpf_data['color']) ? $product->gpf_data['color'] : ''; $product->gpf_size = isset($product->gpf_data['size']) ? $product->gpf_data['size'] : ''; } // All in One SEO Pack - http://wordpress.org/extend/plugins/all-in-one-seo-pack/ if (function_exists('aioseop_activate')) { $product->aioseop_keywords = get_post_meta($product_id, '_aioseop_keywords', true); $product->aioseop_description = get_post_meta($product_id, '_aioseop_description', true); $product->aioseop_title = get_post_meta($product_id, '_aioseop_title', true); $product->aioseop_titleatr = get_post_meta($product_id, '_aioseop_titleatr', true); $product->aioseop_menulabel = get_post_meta($product_id, '_aioseop_menulabel', true); } // WordPress SEO - http://wordpress.org/plugins/wordpress-seo/ if (function_exists('wpseo_admin_init')) { $product->wpseo_focuskw = get_post_meta($product_id, '_yoast_wpseo_focuskw', true); $product->wpseo_metadesc = get_post_meta($product_id, '_yoast_wpseo_metadesc', true); $product->wpseo_title = get_post_meta($product_id, '_yoast_wpseo_title', true); $product->wpseo_googleplus_description = get_post_meta($product_id, '_yoast_wpseo_google-plus-description', true); $product->wpseo_opengraph_description = get_post_meta($product_id, '_yoast_wpseo_opengraph-description', true); } // Ultimate SEO - http://wordpress.org/plugins/seo-ultimate/ if (function_exists('su_wp_incompat_notice')) { $product->useo_meta_title = get_post_meta($product_id, '_su_title', true); $product->useo_meta_description = get_post_meta($product_id, '_su_description', true); $product->useo_meta_keywords = get_post_meta($product_id, '_su_keywords', true); $product->useo_social_title = get_post_meta($product_id, '_su_og_title', true); $product->useo_social_description = get_post_meta($product_id, '_su_og_description', true); $product->useo_meta_noindex = get_post_meta($product_id, '_su_meta_robots_noindex', true); $product->useo_meta_noautolinks = get_post_meta($product_id, '_su_disable_autolinks', true); } // WooCommerce MSRP Pricing - http://woothemes.com/woocommerce/ if (function_exists('woocommerce_msrp_activate')) { $product->msrp = get_post_meta($product_id, '_msrp_price', true); if ($product->msrp == false && $product->post_type == 'product_variation') { $product->msrp = get_post_meta($product_id, '_msrp', true); } // Check that a valid price has been provided and that wc_format_localized_price() exists if (isset($product->msrp) && $product->msrp != '' && function_exists('wc_format_localized_price')) { $product->msrp = wc_format_localized_price($product->msrp); } } // Allow Plugin/Theme authors to add support for additional Product columns $product = apply_filters('woo_ce_product_item', $product, $product_id); return $product; }
function woo_ce_get_product_data( $product_id = 0, $args = array(), $fields = array() ) { // Get Product defaults $weight_unit = get_option( 'woocommerce_weight_unit' ); $dimension_unit = get_option( 'woocommerce_dimension_unit' ); $height_unit = $dimension_unit; $width_unit = $dimension_unit; $length_unit = $dimension_unit; $product = get_post( $product_id ); $_product = ( function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : false ); $product->parent_id = ''; $product->parent_sku = ''; if( $product->post_type == 'product_variation' ) { // Assign Parent ID for Variants then check if Parent exists if( $product->parent_id = $product->post_parent ) $product->parent_sku = get_post_meta( $product->post_parent, '_sku', true ); else $product->parent_id = ''; } $product->product_id = $product_id; $product->sku = get_post_meta( $product_id, '_sku', true ); $product->name = get_the_title( $product_id ); if( $product->post_type <> 'product_variation' ) $product->permalink = get_permalink( $product_id ); $product->product_url = ( method_exists( $_product, 'get_permalink' ) ? $_product->get_permalink() : get_permalink( $product_id ) ); $product->slug = $product->post_name; $product->description = woo_ce_format_description_excerpt( $product->post_content ); $product->excerpt = woo_ce_format_description_excerpt( $product->post_excerpt ); $product->price = get_post_meta( $product_id, '_regular_price', true ); $product->sale_price = get_post_meta( $product_id, '_sale_price', true ); // Check if we're dealing with a Variable Product Type $term_taxonomy = 'product_type'; if( has_term( 'variable', $term_taxonomy, $product_id ) ) $product->price = get_post_meta( $product_id, '_price', true ); if( isset( $product->price ) && $product->price != '' ) $product->price = woo_ce_format_price( $product->price ); if( isset( $product->sale_price ) && $product->sale_price != '' ) $product->sale_price = woo_ce_format_price( $product->sale_price ); $product->sale_price_dates_from = woo_ce_format_product_sale_price_dates( get_post_meta( $product_id, '_sale_price_dates_from', true ) ); $product->sale_price_dates_to = woo_ce_format_product_sale_price_dates( get_post_meta( $product_id, '_sale_price_dates_to', true ) ); $product->post_date = woo_ce_format_date( $product->post_date ); $product->post_modified = woo_ce_format_date( $product->post_modified ); $product->type = woo_ce_get_product_assoc_type( $product_id ); if( $product->post_type == 'product_variation' ) { // Override the Product Type for Variations $product->type = __( 'Variation', 'woo_ce' ); // Override the Description and Excerpt if Variation Formatting is enabled if( woo_ce_get_option( 'variation_formatting', 0 ) ) { $parent = get_post( $product->parent_id ); $product->description = $parent->post_content; $product->excerpt = $parent->post_excerpt; unset( $parent ); } } $product->visibility = woo_ce_format_product_visibility( get_post_meta( $product_id, '_visibility', true ) ); $product->featured = woo_ce_format_switch( get_post_meta( $product_id, '_featured', true ) ); $product->virtual = woo_ce_format_switch( get_post_meta( $product_id, '_virtual', true ) ); $product->downloadable = woo_ce_format_switch( get_post_meta( $product_id, '_downloadable', true ) ); $product->weight = get_post_meta( $product_id, '_weight', true ); $product->weight_unit = ( $product->weight != '' ? $weight_unit : '' ); $product->height = get_post_meta( $product_id, '_height', true ); $product->height_unit = ( $product->height != '' ? $height_unit : '' ); $product->width = get_post_meta( $product_id, '_width', true ); $product->width_unit = ( $product->width != '' ? $width_unit : '' ); $product->length = get_post_meta( $product_id, '_length', true ); $product->length_unit = ( $product->length != '' ? $length_unit : '' ); $product->category = woo_ce_get_product_assoc_categories( $product_id, $product->parent_id ); $product->tag = woo_ce_get_product_assoc_tags( $product_id ); $product->manage_stock = woo_ce_format_switch( get_post_meta( $product_id, '_manage_stock', true ) ); $product->allow_backorders = woo_ce_format_switch( get_post_meta( $product_id, '_backorders', true ) ); $product->sold_individually = woo_ce_format_switch( get_post_meta( $product_id, '_sold_individually', true ) ); $product->total_sales = get_post_meta( $product_id, 'total_sales', true ); $product->upsell_ids = woo_ce_get_product_assoc_upsell_ids( $product_id ); $product->crosssell_ids = woo_ce_get_product_assoc_crosssell_ids( $product_id ); $product->quantity = get_post_meta( $product_id, '_stock', true ); $product->quantity = ( function_exists( 'wc_stock_amount' ) ? wc_stock_amount( $product->quantity ) : $product->quantity ); $product->stock_status = woo_ce_format_product_stock_status( get_post_meta( $product_id, '_stock_status', true ), $product->quantity ); $product->image = woo_ce_get_product_assoc_featured_image( $product_id, $product->parent_id ); $product->image_thumbnail = woo_ce_get_product_assoc_featured_image( $product_id, $product->parent_id, 'thumbnail' ); $product->product_gallery = woo_ce_get_product_assoc_product_gallery( $product_id ); $product->product_gallery_thumbnail = woo_ce_get_product_assoc_product_gallery( $product_id, 'thumbnail' ); $product->tax_status = woo_ce_format_product_tax_status( get_post_meta( $product_id, '_tax_status', true ) ); $product->tax_class = woo_ce_format_product_tax_class( get_post_meta( $product_id, '_tax_class', true ) ); $product->shipping_class = woo_ce_get_product_assoc_shipping_class( $product_id ); $product->external_url = get_post_meta( $product_id, '_product_url', true ); $product->button_text = get_post_meta( $product_id, '_button_text', true ); $product->download_file_path = woo_ce_get_product_assoc_download_files( $product_id, 'url' ); $product->download_file_name = woo_ce_get_product_assoc_download_files( $product_id, 'name' ); $product->download_limit = get_post_meta( $product_id, '_download_limit', true ); $product->download_expiry = get_post_meta( $product_id, '_download_expiry', true ); $product->download_type = woo_ce_format_product_download_type( get_post_meta( $product_id, '_download_type', true ) ); $product->purchase_note = get_post_meta( $product_id, '_purchase_note', true ); $product->product_status = woo_ce_format_post_status( $product->post_status ); $product->enable_reviews = woo_ce_format_comment_status( $product->comment_status ); $product->menu_order = $product->menu_order; // Attributes if( $attributes = woo_ce_get_product_attributes() ) { // Scan for global Attributes first if( $product->post_type == 'product_variation' ) { // We're dealing with a single Variation, strap yourself in. foreach( $attributes as $attribute ) { $attribute_value = get_post_meta( $product_id, sprintf( 'attribute_pa_%s', $attribute->attribute_name ), true ); if( !empty( $attribute_value ) ) { $term_id = term_exists( $attribute_value, sprintf( 'pa_%s', $attribute->attribute_name ) ); if( $term_id !== 0 && $term_id !== null && !is_wp_error( $term_id ) ) { $term = get_term( $term_id['term_id'], sprintf( 'pa_%s', $attribute->attribute_name ) ); $attribute_value = $term->name; unset( $term ); } unset( $term_id ); } $product->{'attribute_' . $attribute->attribute_name} = $attribute_value; unset( $attribute_value ); } } else { // Either the Variation Parent or a Simple Product, scan for global and custom Attributes $product->attributes = maybe_unserialize( get_post_meta( $product_id, '_product_attributes', true ) ); if( !empty( $product->attributes ) ) { $default_attributes = maybe_unserialize( get_post_meta( $product_id, '_default_attributes', true ) ); $product->default_attributes = ''; // Check for taxonomy-based attributes foreach( $attributes as $attribute ) { if( !empty( $default_attributes ) && is_array( $default_attributes ) ) { if( array_key_exists( 'pa_' . $attribute->attribute_name, $default_attributes ) ) $product->default_attributes .= $attribute->attribute_label . ': ' . $default_attributes['pa_' . $attribute->attribute_name] . "|"; } if( isset( $product->attributes['pa_' . $attribute->attribute_name] ) ) $product->{'attribute_' . $attribute->attribute_name} = woo_ce_get_product_assoc_attributes( $product_id, $product->attributes['pa_' . $attribute->attribute_name], 'product' ); else $product->{'attribute_' . $attribute->attribute_name} = woo_ce_get_product_assoc_attributes( $product_id, $attribute, 'global' ); } // Check for per-Product attributes (custom) foreach( $product->attributes as $key => $attribute ) { if( $attribute['is_taxonomy'] == 0 ) { if( !isset( $product->{'attribute_' . $key} ) ) $product->{'attribute_' . $key} = $attribute['value']; } } if( !empty( $product->default_attributes ) ) $product->default_attributes = substr( $product->default_attributes, 0, -1 ); } } } // Allow Plugin/Theme authors to add support for additional Product columns $product = apply_filters( 'woo_ce_product_item', $product, $product_id ); // Trim back the Product just to requested export fields if( !empty( $fields ) ) { $fields = array_merge( $fields, array( 'id', 'ID', 'post_parent', 'filter' ) ); if( !empty( $product ) ) { foreach( $product as $key => $data ) { if( !in_array( $key, $fields ) ) unset( $product->$key ); } } } return $product; }