Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Fix #247 - Woocommerce [products] shortcode and Polylang
Browse files Browse the repository at this point in the history
  • Loading branch information
hyyan committed Oct 24, 2017
1 parent 0c1dc52 commit c18a273
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/Hyyan/WPI/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
*
* @author Hyyan Abo Fakher <hyyanaf@gmail.com>
*/
class Pages
{
class Pages {

/**
* Construct object.
*/
public function __construct()
{
public function __construct() {
$method = array($this, 'getPostTranslationID');
$pages = apply_filters(HooksInterface::PAGES_LIST, array(
'shop',
Expand All @@ -48,10 +47,9 @@ public function __construct()
/* To get product from current language in the shop page */
add_filter('parse_request', array($this, 'correctShopPage'));
}

add_filter(
'woocommerce_shortcode_products_query',
array($this, 'addShortcodeLanguageFilter'), 10, 2
'woocommerce_shortcode_products_query', array($this, 'addShortcodeLanguageFilter'), 10, 2
);
}

Expand All @@ -62,8 +60,7 @@ public function __construct()
*
* @return int
*/
public function getPostTranslationID($id)
{
public function getPostTranslationID($id) {
if (!function_exists('pll_get_post')) {
return $id;
}
Expand All @@ -85,8 +82,7 @@ public function getPostTranslationID($id)
* @return bool false if the current language is the same as default
* language or if the "pagename" var is empty
*/
public function correctShopPage(\WP $wp)
{
public function correctShopPage(\WP $wp) {
global $polylang;

$shopID = wc_get_page_id('shop');
Expand Down Expand Up @@ -131,8 +127,7 @@ public function correctShopPage(\WP $wp)
*
* @return string translated url
*/
public function translateShopUrl($url, $language)
{
public function translateShopUrl($url, $language) {
$result = $url;

if (!is_post_type_archive('product')) {
Expand All @@ -155,7 +150,7 @@ public function translateShopUrl($url, $language)

return $result;
}

/**
* Add Shortcode Language Filter
*
Expand All @@ -167,13 +162,18 @@ public function translateShopUrl($url, $language)
*
* @return string modified form
*/
public function addShortcodeLanguageFilter($query_args, $atts)
{
if (function_exists('pll_current_language')) {
$query_args['lang'] = isset($query_args['lang']) ?
$query_args['lang'] : pll_current_language();

return $query_args;
public function addShortcodeLanguageFilter($query_args, $atts) {

$ids = explode(',', $atts['ids']);
$transIds = array();
foreach ($ids as $id) {
array_push($transIds, pll_get_post($id));
}

$atts['ids'] = $transIds;
$query_args['post__in'] = $transIds;

return $query_args;
}

}

0 comments on commit c18a273

Please sign in to comment.