Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Last active May 23, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricBusch/86b5b45266a8c7e80ecbb8aa7e8c946b to your computer and use it in GitHub Desktop.
Save EricBusch/86b5b45266a8c7e80ecbb8aa7e8c946b to your computer and use it in GitHub Desktop.
Some merchants provide a larger version of their images in other fields than the image field. This tutorial explains how you can import those large images instead of the smaller ones if the larger ones are available. [datafeedr][dfrpswc]
<?php
/**
* Import a different image field than the default "image" field if a more desired field exists.
*
* Some merchants provide a larger version of their images in other fields than the image field. This code allows
* you to import those large images instead of the smaller ones if the larger ones are available.
*
* @param array $meta An array of meta values.
* @param array $post An array of post data including ID, post_title, post_status, etc...
* @param array $product An array of product data returned from the Datafeedr API.
* @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data.
* @param string $action The action the Product Set is performing. Value are either "insert" or "update".
*
* @return array $meta Update $meta array.
*/
add_filter( 'dfrpswc_filter_postmeta_array', 'mycode_use_different_image_field', 20, 5 );
function mycode_use_different_image_field( $meta, $post, $product, $set, $action ) {
/**
* An array of fields in the order in which you want to choose them.
* You can add more fields to this list but do not remove the 'image'
* field and do not change its position. It should always be last.
*/
$image_fields = array(
'largeimage', // Use the 'largeimage' field if it exists.
'merchantimageurl', // Use the 'merchantimageurl' field if it exists.
'image', // DO NOT REMOVE THIS LINE.
);
// Loop through fields in order of usage preference.
foreach ( $image_fields as $field ) {
if ( isset( $product[ $field ] ) ) {
$meta['_dfrps_featured_image_url'] = $product[ $field ];
break;
}
}
return $meta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment