Skip to content

Commit b9e4e0e

Browse files
committed
ensure correct language is used during feed generation when retrieving products
1 parent a38cac4 commit b9e4e0e

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

src/Feed/FeedBuilder/FeedBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ abstract public function get_languages(): array;
124124
/**
125125
* List current languages.
126126
*
127-
* @return array
127+
* @return string
128128
*/
129129
abstract public function current_languages(): string;
130130

src/Feed/FeedBuilder/FeedBuilderPolylang.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function generate_feed( $lang = null ) {
3131
$args = [
3232
'lang' => $language,
3333
];
34-
$products = Products::get_instance()->get_products( $args );
34+
$products = Products::get_instance()->get_products( $args, $lang );
3535

3636
$this->write_products_feed(
3737
self::get_feed_file_path( $language ),
@@ -160,7 +160,8 @@ public function generate_part( string $lang, int $page = 1, int $post_per_page =
160160
'return' => 'ids',
161161
'lang' => $lang,
162162
);
163-
$products = Products::get_instance()->get_products( $args );
163+
164+
$products = Products::get_instance()->get_products( $args, $lang );
164165

165166
// If the query doesn't return any products, schedule the combine action and stop the current action.
166167
if ( empty( $products ) ) {

src/Feed/FeedBuilder/FeedBuilderWpml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function generate_feed( $lang = null ) {
3333
$original_language = apply_filters( 'wpml_current_language', null );
3434
do_action( 'wpml_switch_language', $language );
3535

36-
$products = Products::get_instance()->get_products();
36+
$products = Products::get_instance()->get_products( [], $lang );
3737
$this->write_products_feed(
3838
self::get_feed_file_path( $language ),
3939
$products

src/Products/Products.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ public function __wakeup() {
4545
throw new \Exception( 'Cannot serialize singleton' );
4646
}
4747

48-
public function get_list_args() {
48+
/**
49+
* Get default query args to retrieve products for the feed.
50+
*
51+
* @param string $lang The current language associated with the feed. Language slug or empty string if
52+
* site isn't multilingual.
53+
*
54+
* @return array
55+
*/
56+
public function get_list_args( $lang = '' ) {
4957
$default_args = array(
5058
'limit' => - 1,
5159
'orderby' => 'date',
@@ -61,7 +69,7 @@ public function get_list_args() {
6169
/**
6270
* Export only the categories selected in BO
6371
*/
64-
$current_language = ShoppingFeedHelper::current_language();
72+
$current_language = ! empty( $lang ) ? $lang : ShoppingFeedHelper::current_language();
6573
$export_categories = ShoppingFeedHelper::get_sf_feed_export_categories( $current_language );
6674
if ( ! empty( $export_categories ) ) {
6775
$categories = [];
@@ -76,7 +84,7 @@ public function get_list_args() {
7684
$default_args['category'] = $categories;
7785
}
7886

79-
return wp_parse_args( ShoppingFeedHelper::wc_products_custom_query_args(), $default_args );
87+
return wp_parse_args( ShoppingFeedHelper::wc_products_custom_query_args( $lang ), $default_args );
8088
}
8189

8290
/**
@@ -92,8 +100,17 @@ public function get_list() {
92100
}
93101
}
94102

95-
public function get_products( $args = array() ) {
96-
$query = new \WC_Product_Query( wp_parse_args( $args, $this->get_list_args() ) );
103+
/**
104+
* Get a list of product to include in the feed.
105+
*
106+
* @param array $args Query args for the `WC_Product_Query`.
107+
* @param string $lang The current language associated with the feed. Language slug or empty string if
108+
* site isn't multilingual.
109+
*
110+
* @return array
111+
*/
112+
public function get_products( $args = array(), $lang = '' ) {
113+
$query = new \WC_Product_Query( wp_parse_args( $args, $this->get_list_args( $lang ) ) );
97114

98115
return $query->get_products();
99116
}

src/ShoppingFeedHelper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,21 @@ public static function wc_product_ean( $wc_product = false ) {
632632

633633
/**
634634
* Add filter for products list query
635+
*
636+
* @param string $lang The current language associated with the feed. Language slug or empty string if
637+
* site isn't multilingual.
638+
*
635639
* @return array
636640
*/
637-
public static function wc_products_custom_query_args() {
638-
return apply_filters( 'shopping_feed_products_custom_args', array() );
641+
public static function wc_products_custom_query_args( $lang = '' ) {
642+
/**
643+
* Filter args used to retrieve products for the feed.
644+
*
645+
* @param array $args Custom query args for the `WC_Product_Query`.
646+
* @param string $lang The current language associated with the feed. Language slug or empty string if
647+
* site isn't multilingual.
648+
*/
649+
return apply_filters( 'shopping_feed_products_custom_args', array(), $lang );
639650
}
640651

641652
/**

0 commit comments

Comments
 (0)