Snippets

Visser Labs Add a custom Product import field to Product Importer Deluxe

Created by Michael Visser last modified
<?php
function custom_woo_pd_extend_options_addons( $fields ) {

	// Import field title: Average Rating (shown as a custom Product import field)
	// Import field key: average_rating (use this key throughout the below Filters)

	$fields[] = array(
		'name' => 'average_rating',
		'label' => __( 'Average Rating', 'woocommerce-product-importer' )
	);
	return $fields;

}
add_filter( 'woo_pd_options_addons', 'custom_woo_pd_extend_options_addons' );

function custom_woo_pd_extend_import_addons( $import, $csv_data ) {

	if( isset( $csv_data['average_rating'] ) ) {
		$import->headers[] = 'average_rating';
		$import->csv_average_rating = array_filter( $csv_data['average_rating'] );
		$import->log .= "<br />>>> " . __( 'Average Rating has been detected and grouped', 'woocommerce-product-importer' );
	}
	return $import;

}
add_filter( 'woo_pd_import_addons', 'custom_woo_pd_extend_import_addons', 10, 2 );

function custom_woo_pd_extend_product_addons( $product, $import, $count ) {

	$product->average_rating = ( isset( $import->csv_average_rating[$count] ) ? $import->csv_average_rating[$count] : null );
	return $product;

}
add_filter( 'woo_pd_product_addons', 'custom_woo_pd_extend_product_addons', 10, 3 );

function custom_woo_pd_create_product_addons( $product, $import ) {

	// Custom Post meta key: _wc_average_rating
	// Custom Post meta value: $product->average_rating

	if( $product->average_rating !== null ) {
		if( $product->post_type <> 'product_variation' ) {
			if( WOO_PD_DEBUG !== true )
				update_post_meta( $product->ID, '_wc_average_rating', absint( $product->average_rating ) );
		}
	}
	return $product;

}
add_filter( 'woo_pd_create_product_addons', 'custom_woo_pd_create_product_addons', 10, 2 );

function custom_woo_pd_create_product_log_addons( $import, $product ) {

	if( $product->average_rating !== null ) {
		if( $product->post_type <> 'product_variation' ) {
			if( $import->advanced_log )
				$import->log .= "<br />>>>>>> " . sprintf( __( 'Setting Average Rating: %s', 'woocommerce-product-importer' ), $product->average_rating );
			else
				$import->log .= "<br />>>>>>> " . __( 'Setting Average Rating', 'woocommerce-product-importer' );
		} else {
			if( $import->advanced_log )
				$import->log .= "<br />>>>>>> " . __( 'Skipping Average Rating, cannot set Average Rating for Variations', 'woocommerce-product-importer' );
			else
				$import->log .= "<br />>>>>>> " . __( 'Skipping Average Rating', 'woocommerce-product-importer' );
		}
	} else if( $import->advanced_log ) {
		$import->log .= "<br />>>>>>> " . __( 'Skipping Average Rating', 'woocommerce-product-importer' );
	}
	return $import;

}
add_filter( 'woo_pd_create_product_log_addons', 'custom_woo_pd_create_product_log_addons', 10, 2 );
?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.