Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add settings to get main parent image #138

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Admin/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,26 @@ function () {
self::SF_FEED_SETTINGS_PAGE,
'sf_feed_settings_categories'
);

// Get main image from parent without have variant in feed
add_settings_field(
'get_parent_image_if_empty_in_feed',
__( 'Set the main image of the parent if the variation is blank', 'shopping-feed' ),
function () {
?>
<label>
<input
type="checkbox"
name="<?php echo esc_attr( sprintf( '%s[get_parent_image_if_empty_in_feed]', self::SF_FEED_OPTIONS ) ); ?>"
<?php checked( $this->sf_feed_options['get_parent_image_if_empty_in_feed'], 'on' ); ?>
/>
</label>

<?php
},
self::SF_FEED_SETTINGS_PAGE,
'sf_feed_settings_categories'
);

//Identifier Field
add_settings_field(
Expand Down
3 changes: 3 additions & 0 deletions src/Products/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ public function get_variations( $for_feed = false ) {

$product = new \WC_Product_Variable( $this->id );
$show_out_of_stock_variations = $for_feed && ShoppingFeedHelper::show_out_of_stock_products_in_feed();
$check_parent_main_image = ShoppingFeedHelper::check_get_parent_image_if_empty_in_feed();
$variations = [];
foreach ( $product->get_children() as $variation_id ) {
$variation = wc_get_product( $variation_id );
Expand All @@ -479,6 +480,8 @@ public function get_variations( $for_feed = false ) {
$variation_data['length'] = $variation->get_length();
if ( ! empty( get_the_post_thumbnail_url( $variation->get_id(), 'full' ) ) ) {
$variation_data['image_main'] = get_the_post_thumbnail_url( $variation->get_id(), 'full' );
} elseif ($check_parent_main_image && ! empty( get_the_post_thumbnail_url( $variation->get_parent_id(), 'full' ) ) ) {
$variation_data['image_main'] = get_the_post_thumbnail_url( $variation->get_parent_id(), 'full' );
}

$variation_data['attributes'] = $this->get_variation_attributes( $variation );
Expand Down
9 changes: 9 additions & 0 deletions src/ShoppingFeedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ public static function show_out_of_stock_products_in_feed() {
return 'on' === self::get_sf_feed_options( 'out_of_stock_products_in_feed' );
}

/**
* Should the main image of the parent product be collected if the variation image is empty?
*
* @return bool true if you have to make this check, false otherwise.
*/
public static function check_get_parent_image_if_empty_in_feed() {
return 'on' === self::get_sf_feed_options( 'get_parent_image_if_empty_in_feed' );
}

/**
* Return display mode for category
* @return string
Expand Down