/** * Add to cart * * The function will make sure that products won't be duplicated for each * language * * @param integer $id the current product id * * @return integer The final product id */ public function add_to_cart($id) { $result = $id; // get the product translations $ids = Utilities::getProductTranslationsArrayByID($id); // check if any of product's translation is already in cart foreach (WC()->cart->get_cart() as $values) { $product = $values['data']; if (in_array($product->id, $ids)) { $result = $product->id; break; } } return $result; }
/** * Get array of product translations IDS * * @param integer $ID the product ID * * @return array array contains all translation IDS for the given product */ protected function getProductPostTranslationIDS($ID) { $result = array($ID); $product = wc_get_product($ID); if ($product && $product->product_type === 'variation') { $IDS = Product\Variation::getRelatedVariation($ID, true); if (is_array($IDS)) { $result = array_merge($result, $IDS); } } else { $IDS = Utilities::getProductTranslationsArrayByID($ID); if (is_array($IDS)) { $result = array_merge($result, $IDS); } } return $IDS ? $IDS : array($ID); }
/** * Translate product IDS for product report * * @global \Polylang $polylang * @global \WooCommerce $woocommerce * * @return false if woocommerce or polylang not found */ public function translateProductIDS() { global $polylang, $woocommerce; if (!$polylang || !$woocommerce) { return false; } /* Check for product_ids */ if (!isset($_GET['product_ids'])) { return false; } $IDS = (array) $_GET['product_ids']; $extendedIDS = array(); if (static::isCombine()) { foreach ($IDS as $ID) { $translations = Utilities::getProductTranslationsArrayByID($ID); $extendedIDS = array_merge($extendedIDS, $translations); } } elseif (isset($_GET['lang']) && esc_attr($_GET['lang']) !== 'all') { $lang = esc_attr($_GET['lang']); foreach ($IDS as $ID) { $translation = Utilities::getProductTranslationByID($ID, $lang); $extendedIDS[] = $translation->id; } } /* Update with extended list */ if (!empty($extendedIDS)) { $_GET['product_ids'] = $extendedIDS; } }