/** * Translate Variation for given variable product * * @param integer $ID product variable ID * @param \WP_Post $post Product Post * @param boolean $update true if update , false otherwise * * @return boolean */ public function duplicateVariations($ID, \WP_Post $post, $update) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } global $pagenow; if (!in_array($pagenow, array('post.php', 'post-new.php'))) { return; } $product = wc_get_product($ID); if (!$product) { return; } $from = null; if (pll_get_post_language($ID) == pll_default_language()) { $from = $product; } else { if (isset($_GET['from_post'])) { /* * This check will make sure that variation , will be * created for brand new products which are not saved yet by user */ $from = Utilities::getProductTranslationByID(esc_attr($_GET['from_post']), pll_default_language()); } else { $from = Utilities::getProductTranslationByObject($product, pll_default_language()); } } if (!$from instanceof \WC_Product_Variable) { return; } $langs = pll_languages_list(); foreach ($langs as $lang) { $variation = new Variation($from, Utilities::getProductTranslationByObject($product, $lang)); remove_action('save_post', array($this, __FUNCTION__), 10); $variation->duplicate(); add_action('save_post', array($this, __FUNCTION__), 10, 3); } }
/** * 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; } }
/** * Replace products in cart with translation of the product in the current * language * * @param \WC_Product|\WC_Product_Variation $cart_item_data Product data * @param array $cart_item Cart item * * @return \WC_Product|\WC_Product_Variation */ public function translate_cart_item_product($cart_item_data, $cart_item) { $cart_product_id = isset($cart_item['product_id']) ? $cart_item['product_id'] : 0; $cart_variation_id = isset($cart_item['variation_id']) ? $cart_item['variation_id'] : 0; // By default, returns the same input $cart_item_data_translation = $cart_item_data; switch ($cart_item_data->product_type) { case 'variation': $variation_translation = $this->get_variation_translation($cart_variation_id); $cart_item_data_translation = $variation_translation ? $variation_translation : $cart_item_data_translation; break; case 'simple': default: $product_translation = Utilities::getProductTranslationByID($cart_product_id); $cart_item_data_translation = $product_translation ? $product_translation : $cart_item_data_translation; break; } return $cart_item_data_translation; }