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_order_items( $order_id = 0, $order_items_types = array() ) { global $export, $wpdb; if( !empty( $order_id ) ) { $order_items_sql = $wpdb->prepare( "SELECT `order_item_id` as id, `order_item_name` as name, `order_item_type` as type FROM `" . $wpdb->prefix . "woocommerce_order_items` WHERE `order_id` = %d", $order_id ); if( $order_items = $wpdb->get_results( $order_items_sql ) ) { $wpdb->flush(); foreach( $order_items as $key => $order_item ) { // Default to Line Item for empty Order Item types if( empty( $order_items_types ) ) $order_items_types = array( 'line_item' ); // Filter Order Item types from Orders export if( !in_array( $order_item->type, $order_items_types ) ) { unset( $order_items[$key] ); continue; } $order_item_meta_sql = $wpdb->prepare( "SELECT `meta_key`, `meta_value` FROM `" . $wpdb->prefix . "woocommerce_order_itemmeta` WHERE `order_item_id` = %d ORDER BY meta_key ASC", $order_item->id ); if( $order_item_meta = $wpdb->get_results( $order_item_meta_sql ) ) { $order_items[$key]->product_id = ''; $order_items[$key]->variation_id = ''; $order_items[$key]->sku = ''; $order_items[$key]->description = ''; $order_items[$key]->excerpt = ''; $order_items[$key]->variation = ''; $order_items[$key]->quantity = ''; $order_items[$key]->total = ''; $order_items[$key]->subtotal = ''; $order_items[$key]->rrp = ''; $order_items[$key]->stock = ''; $order_items[$key]->tax = ''; $order_items[$key]->tax_subtotal = ''; $order_items[$key]->tax_class = ''; $order_items[$key]->category = ''; $order_items[$key]->tag = ''; $order_items[$key]->total_sales = ''; $order_items[$key]->weight = ''; $order_items[$key]->total_weight = ''; $size = count( $order_item_meta ); for( $i = 0; $i < $size; $i++ ) { // Go through each Order Item meta found switch( $order_item_meta[$i]->meta_key ) { case '_qty': $order_items[$key]->quantity = $order_item_meta[$i]->meta_value; break; case '_product_id': if( $order_items[$key]->product_id = $order_item_meta[$i]->meta_value ) { $product = get_post( $order_items[$key]->product_id ); if( $product !== null ) { $order_items[$key]->description = woo_ce_format_description_excerpt( $product->post_content ); $order_items[$key]->excerpt = woo_ce_format_description_excerpt( $product->post_excerpt ); } unset( $product ); $order_items[$key]->sku = get_post_meta( $order_items[$key]->product_id, '_sku', true ); $order_items[$key]->category = woo_ce_get_product_assoc_categories( $order_items[$key]->product_id ); $order_items[$key]->tag = woo_ce_get_product_assoc_tags( $order_items[$key]->product_id ); $order_items[$key]->total_sales = get_post_meta( $order_items[$key]->product_id, 'total_sales', true ); $order_items[$key]->weight = get_post_meta( $order_items[$key]->product_id, '_weight', true ); $order_items[$key]->total_weight = ( $order_items[$key]->weight <> '' ? $order_items[$key]->weight * $order_items[$key]->quantity : '' ); $order_items[$key]->rrp = get_post_meta( $order_items[$key]->product_id, '_price', true ); if( isset( $order_items[$key]->rrp ) && $order_items[$key]->rrp != '' ) $order_items[$key]->rrp = woo_ce_format_price( $order_items[$key]->rrp ); $order_items[$key]->stock = get_post_meta( $order_items[$key]->product_id, '_stock', true ); } break; case '_tax_class': $order_items[$key]->tax_class = woo_ce_format_order_item_tax_class( $order_item_meta[$i]->meta_value ); break; case '_line_subtotal': $order_items[$key]->subtotal = woo_ce_format_price( $order_item_meta[$i]->meta_value ); break; case '_line_subtotal_tax': $order_items[$key]->tax_subtotal = woo_ce_format_price( $order_item_meta[$i]->meta_value ); break; case '_line_total': $order_items[$key]->total = woo_ce_format_price( $order_item_meta[$i]->meta_value ); break; case '_line_tax': $order_items[$key]->tax = woo_ce_format_price( $order_item_meta[$i]->meta_value ); break; case '_variation_id': $order_items[$key]->variation = ''; if( $order_items[$key]->variation_id = $order_item_meta[$i]->meta_value ) { // Check if the Variation SKU is set and default to the Product SKU if it is empty $variation_sku = get_post_meta( $order_items[$key]->variation_id, '_sku', true ); if( !empty( $variation_sku ) ) $order_items[$key]->sku = $variation_sku; unset( $variation_sku ); $variations_sql = "SELECT `meta_key` FROM `" . $wpdb->postmeta . "` WHERE `post_id` = " . $order_items[$key]->variation_id . " AND `meta_key` LIKE 'attribute_pa_%' ORDER BY `meta_key` ASC"; // Check if the variation has a taxonomy if( $variations = $wpdb->get_col( $variations_sql ) ) { $attributes = woo_ce_get_product_attributes(); foreach( $variations as $variation ) { $variation = str_replace( 'attribute_pa_', '', $variation ); foreach( $attributes as $attribute ) { if( $attribute->attribute_name == $variation ) { if( empty( $attribute->attribute_label ) ) $attribute->attribute_label = $attribute->attribute_name; $variation_label = $attribute->attribute_label; break; } } $slug = get_post_meta( $order_items[$key]->variation_id, sprintf( 'attribute_pa_%s', $variation ), true ); $term_taxonomy = 'pa_' . $variation; if( taxonomy_exists( $term_taxonomy ) ) { $term = get_term_by( 'slug', $slug, $term_taxonomy ); if( $term && !is_wp_error( $term ) ) $order_items[$key]->variation .= sprintf( apply_filters( 'woo_ce_get_order_items_variation_taxonomy', '%s: %s' ), $variation_label, $term->name ) . "|"; } } unset( $variations, $variation, $variation_label, $slug, $term_taxonomy, $term ); $order_items[$key]->variation = substr( $order_items[$key]->variation, 0, -1 ); } else { // Check for per-Product variations that are not linked to a taxonomy $variations_sql = "SELECT `meta_key` FROM `" . $wpdb->postmeta . "` WHERE `post_id` = " . $order_items[$key]->variation_id . " AND `meta_key` LIKE 'attribute_%' ORDER BY `meta_key` ASC"; if( $variations = $wpdb->get_col( $variations_sql ) ) { foreach( $variations as $variation ) { $variation = str_replace( 'attribute_', '', $variation ); $attribute = get_post_meta( $order_items[$key]->product_id, '_product_attributes', true ); $variation_label = ''; if( !empty( $attribute ) ) { if( isset( $attribute[$variation] ) ) $variation_label = $attribute[$variation]['name']; } $slug = get_post_meta( $order_items[$key]->variation_id, sprintf( 'attribute_%s', $variation ), true ); if( !empty( $slug ) && !empty( $variation_label ) ) $order_items[$key]->variation .= sprintf( apply_filters( 'woo_ce_get_order_items_variation_custom', '%s: %s' ), $variation_label, ucwords( $slug ) ) . "\n"; } $order_items[$key]->variation = substr( $order_items[$key]->variation, 0, -1 ); unset( $variations, $variation, $attribute, $slug ); } } } break; // This is for any custom Order Item meta default: $order_items[$key] = apply_filters( 'woo_ce_order_item_custom_meta', $order_items[$key], $order_item_meta[$i]->meta_key, $order_item_meta[$i]->meta_value ); break; } } } unset( $order_item_meta ); if( $order_items[$key]->type == 'fee' ) $order_items[$key]->quantity = 1; $order_items[$key] = apply_filters( 'woo_ce_order_item', $order_items[$key], $order_id ); $order_items[$key]->type = woo_ce_format_order_item_type( $order_items[$key]->type ); } return $order_items; } } }
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; }
function woo_ce_get_products($args = array()) { $limit_volume = -1; $offset = 0; $product_categories = false; $product_tags = false; $product_status = false; $product_type = false; if ($args) { $limit_volume = $args['limit_volume']; $offset = $args['offset']; if (!empty($args['product_categories'])) { $product_categories = $args['product_categories']; } if (!empty($args['product_tags'])) { $product_tags = $args['product_tags']; } if (!empty($args['product_status'])) { $product_status = $args['product_status']; } if (!empty($args['product_type'])) { $product_type = $args['product_type']; } } $post_type = array('product', 'product_variation'); $args = array('post_type' => $post_type, 'numberposts' => $limit_volume, 'offset' => $offset, 'orderby' => 'ID', 'order' => 'ASC', 'post_status' => woo_ce_post_statuses()); if ($product_categories) { $args['tax_query'] = array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $product_categories)); } if ($product_tags) { $args['tax_query'] = array(array('taxonomy' => 'product_tag', 'field' => 'id', 'terms' => $product_tags)); } if ($product_status) { $args['post_status'] = woo_ce_post_statuses($product_status, true); } if ($product_type) { if (in_array('variation', $product_type)) { $args['post_type'] = 'product_variation'; } else { $args['tax_query'] = array(array('taxonomy' => 'product_type', 'field' => 'slug', 'terms' => $product_type)); } } $products = get_posts($args); if ($products) { $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; foreach ($products as $key => $product) { $products[$key]->parent_id = ''; $products[$key]->parent_sku = ''; if ($product->post_type == 'product_variation') { $products[$key]->parent_id = $product->post_parent; $products[$key]->parent_sku = get_post_meta($product->post_parent, '_sku', true); } $products[$key]->product_id = $product->ID; $products[$key]->sku = get_post_meta($product->ID, '_sku', true); $products[$key]->name = get_the_title($product->ID); $products[$key]->description = woo_ce_clean_html($product->post_content); $products[$key]->regular_price = get_post_meta($product->ID, '_regular_price', true); $products[$key]->price = get_post_meta($product->ID, '_price', true); if (!empty($products[$key]->regular_price) && $products[$key]->regular_price != $products[$key]->price) { $products[$key]->price = $products[$key]->regular_price; } $products[$key]->sale_price = get_post_meta($product->ID, '_sale_price', true); $products[$key]->sale_price_dates_from = woo_ce_format_sale_price_dates(get_post_meta($product->ID, '_sale_price_dates_from', true)); $products[$key]->sale_price_dates_to = woo_ce_format_sale_price_dates(get_post_meta($product->ID, '_sale_price_dates_to', true)); $products[$key]->slug = $product->post_name; $products[$key]->permalink = get_permalink($product->ID); $products[$key]->excerpt = woo_ce_clean_html($product->post_excerpt); $products[$key]->type = woo_ce_get_product_assoc_type($product->ID); if ($product->post_type == 'product_variation') { $products[$key]->type = __('Variation', 'woo_ce'); } $products[$key]->visibility = woo_ce_format_visibility(get_post_meta($product->ID, '_visibility', true)); $products[$key]->featured = woo_ce_format_switch(get_post_meta($product->ID, '_featured', true)); $products[$key]->virtual = woo_ce_format_switch(get_post_meta($product->ID, '_virtual', true)); $products[$key]->downloadable = woo_ce_format_switch(get_post_meta($product->ID, '_downloadable', true)); $products[$key]->weight = get_post_meta($product->ID, '_weight', true); $products[$key]->weight_unit = $weight_unit; $products[$key]->height = get_post_meta($product->ID, '_height', true); $products[$key]->height_unit = $height_unit; $products[$key]->width = get_post_meta($product->ID, '_width', true); $products[$key]->width_unit = $width_unit; $products[$key]->length = get_post_meta($product->ID, '_length', true); $products[$key]->length_unit = $length_unit; $products[$key]->category = woo_ce_get_product_assoc_categories($product->ID); $products[$key]->tag = woo_ce_get_product_assoc_tags($product->ID); $products[$key]->manage_stock = woo_ce_format_switch(get_post_meta($product->ID, '_manage_stock', true)); $products[$key]->allow_backorders = woo_ce_format_switch(get_post_meta($product->ID, '_backorders', true)); $products[$key]->sold_individually = woo_ce_format_switch(get_post_meta($product->ID, '_sold_individually', true)); $products[$key]->upsell_ids = woo_ce_convert_product_ids(get_post_meta($product->ID, '_upsell_ids', true)); $products[$key]->crosssell_ids = woo_ce_convert_product_ids(get_post_meta($product->ID, '_crosssell_ids', true)); $products[$key]->quantity = get_post_meta($product->ID, '_stock', true); $products[$key]->stock_status = woo_ce_format_stock_status(get_post_meta($product->ID, '_stock_status', true)); $products[$key]->image = woo_ce_get_product_assoc_featured_image($product->ID); $products[$key]->product_gallery = woo_ce_get_product_assoc_product_gallery($product->ID); $products[$key]->tax_status = woo_ce_format_tax_status(get_post_meta($product->ID, '_tax_status', true)); $products[$key]->tax_class = woo_ce_format_tax_class(get_post_meta($product->ID, '_tax_class', true)); $products[$key]->product_url = get_post_meta($product->ID, '_product_url', true); $products[$key]->button_text = get_post_meta($product->ID, '_button_text', true); $products[$key]->file_download = woo_ce_get_product_assoc_file_downloads($product->ID); $products[$key]->download_limit = get_post_meta($product->ID, '_download_limit', true); $products[$key]->download_expiry = get_post_meta($product->ID, '_download_expiry', true); $products[$key]->purchase_note = get_post_meta($product->ID, '_purchase_note', true); $products[$key]->product_status = woo_ce_format_product_status($product->post_status); $products[$key]->comment_status = woo_ce_format_comment_status($product->comment_status); if ($attributes = woo_ce_get_product_attributes()) { if ($product->post_type == 'product_variation') { foreach ($attributes as $attribute) { $products[$key]->{'attribute_' . $attribute->attribute_name} = get_post_meta($product->ID, sprintf('attribute_pa_%s', $attribute->attribute_name), true); } } else { $products[$key]->attributes = maybe_unserialize(get_post_meta($product->ID, '_product_attributes', true)); if (!empty($products[$key]->attributes)) { foreach ($attributes as $attribute) { if (isset($products[$key]->attributes['pa_' . $attribute->attribute_name])) { $products[$key]->{'attribute_' . $attribute->attribute_name} = woo_ce_get_product_assoc_attributes($product->ID, $products[$key]->attributes['pa_' . $attribute->attribute_name]); } } } } } $products[$key] = apply_filters('woo_ce_product_item', $products[$key], $product->ID); } } return $products; }
function woo_cd_get_order_items($order_id = 0) { global $export, $wpdb; if ($order_id) { $order_items_sql = $wpdb->prepare("SELECT `order_item_id` as id, `order_item_name` as name, `order_item_type` as type FROM `" . $wpdb->prefix . "woocommerce_order_items` WHERE `order_id` = %d", $order_id); if ($order_items = $wpdb->get_results($order_items_sql)) { $wpdb->flush(); foreach ($order_items as $key => $order_item) { $order_item_meta_sql = $wpdb->prepare("SELECT `meta_key`, `meta_value` FROM `" . $wpdb->prefix . "woocommerce_order_itemmeta` WHERE `order_item_id` = %d", $order_item->id); if ($order_item_meta = $wpdb->get_results($order_item_meta_sql)) { $order_items[$key]->product_id = ''; $order_items[$key]->variation_id = ''; $order_items[$key]->variation = ''; $order_items[$key]->quantity = ''; $order_items[$key]->total = ''; $order_items[$key]->subtotal = ''; $order_items[$key]->tax = ''; $order_items[$key]->tax_subtotal = ''; $order_items[$key]->tax_class = ''; $order_items[$key]->category = ''; $order_items[$key]->tag = ''; $size = count($order_item_meta); for ($i = 0; $i < $size; $i++) { switch ($order_item_meta[$i]->meta_key) { case '_qty': $order_items[$key]->quantity = $order_item_meta[$i]->meta_value; break; case '_product_id': if ($order_items[$key]->product_id = $order_item_meta[$i]->meta_value) { $order_items[$key]->sku = get_post_meta($order_items[$key]->product_id, '_sku', true); $order_items[$key]->category = woo_ce_get_product_assoc_categories($order_items[$key]->product_id); $order_items[$key]->tag = woo_ce_get_product_assoc_tags($order_items[$key]->product_id); } break; case '_tax_class': $order_items[$key]->tax_class = get_post_meta($order_items[$key]->product_id, 'size', true); break; case '_line_subtotal': $order_items[$key]->subtotal = $order_item_meta[$i]->meta_value; break; case '_line_subtotal_tax': $order_items[$key]->tax_subtotal = $order_item_meta[$i]->meta_value; break; case '_line_total': $order_items[$key]->total = $order_item_meta[$i]->meta_value; break; case '_line_tax': $order_items[$key]->tax = $order_item_meta[$i]->meta_value; break; case '_variation_id': $order_items[$key]->variation = ''; if ($order_items[$key]->variation_id = $order_item_meta[$i]->meta_value) { $order_items[$key]->sku = get_post_meta($order_items[$key]->variation_id, '_sku', true); $variations_sql = "SELECT `meta_key` FROM `" . $wpdb->postmeta . "` WHERE `post_id` = " . (int) $order_items[$key]->variation_id . " AND `meta_key` LIKE 'attribute_pa_%'"; if ($variations = $wpdb->get_col($variations_sql)) { foreach ($variations as $variation) { $variation = str_replace('attribute_pa_', '', $variation); $variation_label_sql = $wpdb->prepare("SELECT `attribute_label` FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies` WHERE `attribute_name` = '%s' LIMIT 1", $variation); $variation_label = $wpdb->get_var($variation_label_sql); $slug = get_post_meta($order_items[$key]->variation_id, sprintf('attribute_pa_%s', $variation), true); $term_taxonomy = 'pa_' . $variation; if (taxonomy_exists($term_taxonomy)) { $term = get_term_by('slug', $slug, $term_taxonomy); if ($term) { $order_items[$key]->variation .= sprintf('%s: %s', $variation_label, $term->name) . $export->category_separator; } } } $order_items[$key]->variation = substr($order_items[$key]->variation, 0, -1); } } break; default: $order_items[$key] = apply_filters('woo_cd_order_items', $order_items[$key], $order_item_meta[$i]->meta_key, $order_item_meta[$i]->meta_value); break; } } } unset($order_item_meta); if ($order_items[$key]->type == 'fee') { $order_items[$key]->quantity = 1; } $order_items[$key]->type = woo_cd_format_order_item_type($order_items[$key]->type); } return $order_items; } } }