l_check_on_duplicated_products_in_cart', $cart_contents, $key, $cart_content ); if ( apply_filters( 'wcml_exception_duplicate_products_in_cart', false, $cart_content ) ) { continue; } $quantity = $cart_content['quantity']; $search_key = $this->wcml_generate_cart_key( $cart_contents, $key ); if ( array_key_exists( $search_key, $exists_products ) ) { unset( $cart_contents[ $key ] ); $cart_contents[ $exists_products[ $search_key ] ]['quantity'] = $cart_contents[ $exists_products[ $search_key ] ]['quantity'] + $quantity; $this->woocommerce->cart->calculate_totals(); } else { $exists_products[ $search_key ] = $key; } } add_action( 'woocommerce_before_calculate_totals', [ $this, 'woocommerce_calculate_totals' ], 100 ); return $cart_contents; } public function get_cart_attribute_translation( $attr_key, $attribute, $variation_id, $current_language, $product_id, $tr_product_id ) { $attr_translation = $attribute; if ( ! empty( $attribute ) ) { $taxonomy = $this->remove_attribute_prefix( $attr_key ); if ( taxonomy_exists( $taxonomy ) ) { if ( $this->woocommerce_wpml->attributes->is_translatable_attribute( $taxonomy ) ) { $term_id = $this->woocommerce_wpml->terms->wcml_get_term_id_by_slug( $taxonomy, $attribute ); $trnsl_term_id = apply_filters( 'translate_object_id', $term_id, $taxonomy, true, $current_language ); $term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $trnsl_term_id, $taxonomy ); $attr_translation = $term->slug; } } elseif ( $variation_id ) { $trnsl_attr = get_post_meta( $variation_id, $attr_key, true ); if ( $trnsl_attr ) { $attr_translation = $trnsl_attr; } else { $attr_translation = $this->woocommerce_wpml->attributes->get_custom_attr_translation( $product_id, $tr_product_id, $taxonomy, $attribute ); } } } return $attr_translation; } /** * @param string $attr_key * * @return string */ protected function remove_attribute_prefix( $attr_key ) { $taxonomy = $attr_key; $attribute_prefix = 'attribute_'; if ( strpos( $attr_key, $attribute_prefix ) === 0 ) { $taxonomy = substr( $attr_key, strlen( $attribute_prefix ) ); } return $taxonomy; } public function wcml_generate_cart_key( $cart_contents, $key ) { $cart_item_data = $this->get_cart_item_data_from_cart( $cart_contents[ $key ] ); return $this->woocommerce->cart->generate_cart_id( $cart_contents[ $key ]['product_id'], $cart_contents[ $key ]['variation_id'], $cart_contents[ $key ]['variation'], $cart_item_data ); } // get cart_item_data from existing cart array ( from session ) public function get_cart_item_data_from_cart( $cart_contents ) { unset( $cart_contents['product_id'] ); unset( $cart_contents['variation_id'] ); unset( $cart_contents['variation'] ); unset( $cart_contents['quantity'] ); unset( $cart_contents['line_total'] ); unset( $cart_contents['line_subtotal'] ); unset( $cart_contents['line_tax'] ); unset( $cart_contents['line_subtotal_tax'] ); unset( $cart_contents['line_tax_data'] ); unset( $cart_contents['data'] ); unset( $cart_contents['key'] ); return apply_filters( 'wcml_filter_cart_item_data', $cart_contents ); } // refresh cart total to return correct price from WC object public function wcml_refresh_cart_total() { WC()->cart->calculate_totals(); } public function localize_flat_rates_shipping_classes() { if ( wp_doing_ajax() && isset( $_POST['action'] ) && $_POST['action'] == 'woocommerce_update_order_review' ) { $shipping_methods = $this->woocommerce->shipping()->get_shipping_methods(); foreach ( $shipping_methods as $method ) { if ( isset( $method->flat_rate_option ) ) { add_filter( 'option_' . $method->flat_rate_option, [ $this, 'translate_shipping_class' ] ); } } } } public function translate_shipping_class( $rates ) { if ( is_array( $rates ) ) { foreach ( $rates as $shipping_class => $value ) { $term_id = $this->woocommerce_wpml->terms->wcml_get_term_id_by_slug( 'product_shipping_class', $shipping_class ); if ( $term_id && ! is_wp_error( $term_id ) ) { $translated_term_id = apply_filters( 'translate_object_id', $term_id, 'product_shipping_class', true ); if ( $translated_term_id != $term_id ) { $term = $this->woocommerce_wpml->terms->wcml_get_term_by_id( $translated_term_id, 'product_shipping_class' ); unset( $rates[ $shipping_class ] ); $rates[ $term->slug ] = $value; } } } } return $rates; } public function filter_paypal_args( $args ) { $args['lc'] = $this->sitepress->get_current_language(); // filter URL when default permalinks uses $wpml_settings = $this->sitepress->get_settings(); if ( $wpml_settings['language_negotiation_type'] == 3 ) { $args['notify_url'] = str_replace( '%2F&', '&', $args['notify_url'] ); } return $args; } public function add_to_cart_sold_individually_exception( $found_in_cart, $product_id, $variation_id, $cart_item_data ) { $post_id = $product_id; if ( $variation_id ) { $post_id = $variation_id; } foreach ( WC()->cart->cart_contents as $cart_item ) { if ( $this->sold_individually_product( $cart_item, $cart_item_data, $post_id ) ) { $found_in_cart = true; break; } } return $found_in_cart; } public function sold_individually_product( $cart_item, $cart_item_data, $post_id ) { $current_product_trid = $this->sitepress->get_element_trid( $post_id, 'post_' . get_post_type( $post_id ) ); if ( ! empty( $cart_item['variation_id'] ) ) { $cart_element_trid = $this->sitepress->get_element_trid( $cart_item['variation_id'], 'post_product_variation' ); } else { $cart_element_trid = $this->sitepress->get_element_trid( $cart_item['product_id'], 'post_product' ); } if ( apply_filters( 'wcml_add_to_cart_sold_individually', true, $cart_item_data, $post_id, $cart_item['quantity'] ) && $current_product_trid == $cart_element_trid && $cart_item['quantity'] > 0 ) { return true; } else { return false; } } /** * @param string $permalink * @param array $cart_item * * @return string */ public function cart_item_permalink( $permalink, $cart_item ) { if ( ! $this->sitepress->get_setting( 'auto_adjust_ids' ) ) { $permalink = get_permalink( $cart_item['product_id'] ); } return $permalink; } /** * @param string $currency * * @return float */ public function convert_cart_total_to_currency( $currency ) { $total = WC()->cart->get_total( 'raw' ); $total_default = $this->woocommerce_wpml->multi_currency->prices->unconvert_price_amount( $total ); $total_currency = $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $total_default, $currency ); return $total_currency; } /** * @param string $currency * * @return string */ public function format_converted_cart_total_in_currency( $currency ) { return $this->woocommerce_wpml->multi_currency->prices->format_price_in_currency( $this->convert_cart_total_to_currency( $currency ), $currency ); } public function convert_cart_shipping_to_currency( $currency ) { $shipping_amount_in_default_currency = $this->woocommerce_wpml->multi_currency->prices->unconvert_price_amount( WC()->cart->get_shipping_total() ); return $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $shipping_amount_in_default_currency, $currency ); } /** * @param WC_Product $product * * @return WC_Product */ public function adjust_cart_item_product_name( $product ) { $product_id = $product->get_id(); $current_product_id = wpml_object_id_filter( $product_id, get_post_type( $product_id ) ); if ( $current_product_id ) { $product->set_name( wc_get_product( $current_product_id )->get_name() ); } return $product; } /** * @param string $cart_hash_key * * @return string */ public function add_language_to_cart_hash_key( $cart_hash_key ) { return $cart_hash_key . '-' . $this->sitepress->get_current_language(); } /** * @param int[] $productIds * * @return int[] */ public function convert_crosssell_ids( $productIds ) { $returnOriginal = $this->sitepress->is_display_as_translated_post_type( 'product' ); // $convertId :: int -> int|null $convertId = function( $id ) use ( $returnOriginal ) { return $this->sitepress->get_object_id( $id, 'product', $returnOriginal ); }; return wpml_collect( $productIds ) ->map( $convertId ) ->filter() ->toArray(); } }