Example #1
0
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_extend_product_item( $product, $product_id ) {

	global $export;

	// 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_title_attributes = get_post_meta( $product_id, '_aioseop_titleatr', true );
		$product->aioseop_menu_label = 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
			if( isset( $product->msrp ) && $product->msrp != '' )
				$product->msrp = woo_ce_format_price( $product->msrp );
	}

	// WooCommerce Brands Addon - http://woothemes.com/woocommerce/
	// WooCommerce Brands - http://proword.net/Woocommerce_Brands/
	if( class_exists( 'WC_Brands' ) || class_exists( 'woo_brands' ) || taxonomy_exists( apply_filters( 'woo_ce_brand_term_taxonomy', 'product_brand' ) ) )
		$product->brands = woo_ce_get_product_assoc_brands( $product_id, $product->parent_id );

	// Cost of Goods - http://www.skyverge.com/product/woocommerce-cost-of-goods-tracking/
	if( class_exists( 'WC_COG' ) )
		$product->cost_of_goods = get_post_meta( $product_id, '_wc_cog_cost', true );

	// Per-Product Shipping - http://www.woothemes.com/products/per-product-shipping/
	if( function_exists( 'woocommerce_per_product_shipping_init' ) )
		$product->per_product_shipping = get_post_meta( $product_id, '_per_product_shipping', true );

	// Product Vendors - http://www.woothemes.com/products/product-vendors/
	if( class_exists( 'WooCommerce_Product_Vendors' ) ) {
		$product->vendors = woo_ce_get_product_assoc_product_vendors( $product_id, $product->parent_id );
		$product->vendor_ids = woo_ce_get_product_assoc_product_vendors( $product_id, $product->parent_id, 'term_id' );
		$product->vendor_commission = woo_ce_get_product_assoc_product_vendor_commission( $product_id, $product->vendor_ids );
	}

	// WooCommerce Wholesale Pricing - http://ignitewoo.com/woocommerce-extensions-plugins-themes/woocommerce-wholesale-pricing/
	if( class_exists( 'woocommerce_wholesale_pricing' ) ) {
		$product->wholesale_price = woo_ce_format_price( get_post_meta( $product_id, 'wholesale_price', true ) );
		$product->wholesale_price_text = get_post_meta( $product_id, 'wholesale_price_text', true );
	}

	// WooCommerce Subscriptions - http://www.woothemes.com/products/woocommerce-subscriptions/
	if( class_exists( 'WC_Subscriptions_Manager' ) ) {
		$product->subscription_price = get_post_meta( $product_id, '_subscription_price', true );
		$product->subscription_period_interval = woo_ce_format_product_subscription_period_interval( get_post_meta( $product_id, '_subscription_period_interval', true ) );
		$product->subscription_period = get_post_meta( $product_id, '_subscription_period', true );
		$product->subscription_length = woo_ce_format_product_subscripion_length( get_post_meta( $product_id, '_subscription_length', true ), $product->subscription_period );
		$product->subscription_sign_up_fee = get_post_meta( $product_id, '_subscription_sign_up_fee', true );
		$product->subscription_trial_length = get_post_meta( $product_id, '_subscription_trial_length', true );
		$product->subscription_trial_period = get_post_meta( $product_id, '_subscription_trial_period', true );
		$product->subscription_limit = woo_ce_format_product_subscription_limit( get_post_meta( $product_id, '_subscription_limit', true ) );
	}

	// WooCommerce Bookings - http://www.woothemes.com/products/woocommerce-bookings/
	if( class_exists( 'WC_Bookings' ) ) {
		$product->booking_has_persons = get_post_meta( $product_id, '_wc_booking_has_persons', true );
		$product->booking_has_resources = get_post_meta( $product_id, '_wc_booking_has_resources', true );
		$product->booking_base_cost = get_post_meta( $product_id, '_wc_booking_cost', true );
		$product->booking_block_cost = get_post_meta( $product_id, '_wc_booking_base_cost', true );
		$product->booking_display_cost = get_post_meta( $product_id, '_wc_display_cost', true );
		$product->booking_requires_confirmation = get_post_meta( $product_id, '_wc_booking_requires_confirmation', true );
		$product->booking_user_can_cancel = get_post_meta( $product_id, '_wc_booking_user_can_cancel', true );
	}

	// Barcodes for WooCommerce - http://www.wolkenkraft.com/produkte/barcodes-fuer-woocommerce/
	if( function_exists( 'wpps_requirements_met' ) ) {
		// Cannot clean up the barcode type as the developer has not exposed any functions or methods
		$product->barcode_type = get_post_meta( $product_id, '_barcode_type', true );
		$product->barcode = get_post_meta( $product_id, '_barcode', true );
	}

	// Custom Product meta
	$custom_products = woo_ce_get_option( 'custom_products', '' );
	if( !empty( $custom_products ) ) {
		foreach( $custom_products as $custom_product ) {
			if( !empty( $custom_product ) ) {
				$product->{$custom_product} = woo_ce_format_custom_meta( get_post_meta( $product_id, $custom_product, true ) );
			}
		}
	}

	if( $export->gallery_unique ) {
		$max_size = woo_ce_get_option( 'max_product_gallery', 3 );
		if( !empty( $product->product_gallery ) ) {
			// Tack on a extra digit to max_size so we get the correct number of columns
			$max_size++;
			$product_gallery = explode( $export->category_separator, $product->product_gallery );
			$size = count( $product_gallery );
			for( $i = 1; $i < $size; $i++ ) {
				if( $i == $max_size )
					break;
				$product->{'product_gallery_' . $i} = $product_gallery[$i];
			}
			$product->product_gallery = $product_gallery[0];
			unset( $product_gallery );
		}
	}

	return $product;

}