/** * Prepare product price for saving and easier read later * * @param integer $element_id Identifier of current product */ public static function calculate_price($element_id) { global $wpdb; /** * Récupères les attributs suivants : product_price, price_ht et tva * par rapport à $element_id / Get the next attributes : product_price, * price_ht and tva */ $query = $wpdb->prepare("SELECT ATTR.code, ATTR_VAL.value_id AS id, ATTR_VAL.value, ATTR.id AS attribute_id\n\t\t\tFROM " . WPSHOP_DBT_ATTRIBUTE . " AS ATTR\n\t\t\t\tRIGHT JOIN " . WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL . " AS ATTR_VAL ON ((ATTR_VAL.attribute_id = ATTR.id) AND (ATTR_VAL.entity_id = %d))\n\t\t\tWHERE ATTR.code IN ('" . implode("', '", unserialize(WPSHOP_ATTRIBUTE_PRICES)) . "')", $element_id); $prices_attribute = $wpdb->get_results($query, OBJECT_K); /** Si aucun prix trouvé on stop la méthode / If not found price, stop the method */ if (empty($prices_attribute)) { return false; } else { /** * Récupère le prix de base selon le pilotage de prix de la boutique / Get * the base amount according on the shop price control */ $base_price = $prices_attribute[constant('WPSHOP_PRODUCT_PRICE_' . WPSHOP_PRODUCT_PRICE_PILOT)]->value; $rate_vat = wpshop_prices::get_rate_vat($element_id); $divider_price_ttc = 1 + $rate_vat->value / 100; /** * Informations nécessaire pour crée un attribut / Information needed to * create an attribute */ $entity_type_id = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT); $language = WPSHOP_CURRENT_LOCALE; if (!empty($_REQUEST['icl_post_language'])) { $query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $_REQUEST['icl_post_language']); $language = $wpdb->get_var($query); } /** * Vérifie la configuration pour savoir comment calculer les prix pour le produit / * Check configuration to know how to make the calcul for the product */ if (WPSHOP_PRODUCT_PRICE_PILOT == 'HT') { $price_with_vat = $base_price * $divider_price_ttc; $price_no_vat = $base_price; } else { if (WPSHOP_PRODUCT_PRICE_PILOT == 'TTC') { $price_with_vat = $base_price; $price_no_vat = $price_with_vat / $divider_price_ttc; } } /** * Le dernier paramètre permet de ne pas supprimer les attributs du même * type que celui-ci / The last parameter allows not to delete the * attributes of the same type as this one */ wpshop_attributes::saveAttributeForEntity(array('decimal' => array('product_price' => $price_with_vat)), $entity_type_id, $element_id, $language, 'wpshop_products'); wpshop_attributes::saveAttributeForEntity(array('decimal' => array('price_ht' => $price_no_vat)), $entity_type_id, $element_id, $language, 'wpshop_product'); /** Ajout ou met à jour de l'attribut tva / Add or update the attribute vat */ $vat_amount = $price_with_vat - $price_no_vat; wpshop_attributes::saveAttributeForEntity(array('decimal' => array('tva' => $vat_amount)), $entity_type_id, $element_id, $language, 'wpshop_product'); /** Update the product meta information with the calculated prices */ $product_postmeta = get_post_meta($element_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, true); $product_postmeta[WPSHOP_PRODUCT_PRICE_TTC] = number_format(round($price_with_vat, 5), 5, '.', ''); $product_postmeta[WPSHOP_PRODUCT_PRICE_HT] = number_format(round($price_no_vat, 5), 5, '.', ''); $product_postmeta[WPSHOP_PRODUCT_PRICE_TAX_AMOUNT] = number_format(round($vat_amount, 5), 5, '.', ''); $product_postmeta[WPSHOP_PRODUCT_PRICE_TAX] = $rate_vat->id; update_post_meta($element_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $product_postmeta); /** Met à jour la meta _wps_price_infos */ $p = wpshop_products::get_product_data($element_id); $price = wpshop_prices::get_product_price($p, 'just_price_infos', array('mini_output', 'grid')); update_post_meta($element_id, '_wps_price_infos', $price); /** Met à jour la meta _wpshop_displayed_price */ wps_filter_search::save_displayed_price_meta($element_id); } return true; }