function get_adjusted_attribute_price($product_id, $quantity, $description = '', $result_attributes) { global $mosConfig_secret; $auth = $_SESSION['auth']; $price = $this->get_price($product_id, $quantity, true, $result_attributes); $base_price = $price["product_price"]; $setprice = 0; $set_price = false; $adjustment = 0; // We must care for custom attribute fields! Their value can be freely given // by the customer, so we mustn't include them into the price calculation // Thanks to AryGroup@ua.fm for the good advice //*********************** //*********************** //*********************** //*********************** // A VOIR //*********************** //*********************** //*********************** //*********************** if (empty($_REQUEST["custom_attribute_fields"])) { if (!empty($_SESSION["custom_attribute_fields"])) { $custom_attribute_fields = vmGet($_SESSION, "custom_attribute_fields", array()); $custom_attribute_fields_check = vmGet($_SESSION, "custom_attribute_fields_check", array()); } else { $custom_attribute_fields = $custom_attribute_fields_check = array(); } } else { $custom_attribute_fields = $_SESSION["custom_attribute_fields"] = vmGet($_REQUEST, "custom_attribute_fields", array()); $custom_attribute_fields_check = $_SESSION["custom_attribute_fields_check"] = vmGet($_REQUEST, "custom_attribute_fields_check", array()); } //*********************** //*********************** //*********************** //*********************** // A VOIR //*********************** //*********************** //*********************** //*********************** // if we've been given a description to deal with, get the adjusted price if ($description != '') { // description is safe to use at this point cause it's set to '' $attribute_keys = explode(";", $description); foreach ($attribute_keys as $temp_desc) { $temp_desc = trim($temp_desc); // Get the key name (e.g. "Color" ) $this_key = substr($temp_desc, 0, strpos($temp_desc, ":")); if (in_array($this_key, $custom_attribute_fields)) { if (@$custom_attribute_fields_check[$this_key] == md5($mosConfig_secret . $this_key)) { // the passed value is valid, don't use it for calculating prices continue; } } $i = 0; $start = strpos($temp_desc, "["); $finish = strpos($temp_desc, "]", $start); $o = substr_count($temp_desc, "["); $c = substr_count($temp_desc, "]"); //echo "open: $o<br>close: $c<br>\n"; // check to see if we have a bracket if (True == is_int($finish)) { $length = $finish - $start; // We found a pair of brackets (price modifier?) if ($length > 1) { $my_mod = substr($temp_desc, $start + 1, $length - 1); //echo "before: ".$my_mod."<br>\n"; if ($o != $c) { // skip the tests if we don't have to process the string if ($o < $c) { $char = "]"; $offset = $start; } else { $char = "["; $offset = $finish; } $s = substr_count($my_mod, $char); for ($r = 1; $r < $s; $r++) { $pos = strrpos($my_mod, $char); $my_mod = substr($my_mod, $pos + 1); } } $oper = substr($my_mod, 0, 1); $my_mod = substr($my_mod, 1); // if we have a number, allow the adjustment if (true == is_numeric($my_mod)) { // Now add or sub the modifier on if ($oper == "+") { $adjustment += $my_mod; } else { if ($oper == "-") { $adjustment -= $my_mod; } else { if ($oper == '=') { // NOTE: the +=, so if we have 2 sets they get added // this could be moded to say, if we have a set_price, then // calc the diff from the base price and start from there if we encounter // another set price... just a thought. $setprice += $my_mod; $set_price = true; } } } } $temp_desc = substr($temp_desc, $finish + 1); $start = strpos($temp_desc, "["); $finish = strpos($temp_desc, "]"); } } $i++; // not necessary, but perhaps interesting? ;) } } // no set price was set from the attribs if ($set_price == false) { $price["product_price"] = $base_price + $adjustment; } else { // otherwise, set the price // add the base price to the price set in the attributes // then subtract the adjustment amount // we could also just add the set_price to the adjustment... not sure on that one. // $setprice += $adjustment; $setprice *= 1 - $auth["shopper_group_discount"] / 100; $price["product_price"] = $setprice; } // don't let negative prices get by, set to 0 if ($price["product_price"] < 0) { $price["product_price"] = 0; } // Get the DISCOUNT AMOUNT $ps_product = new ps_product(); $discount_info = $ps_product->get_discount($product_id); $my_taxrate = $ps_product->get_product_taxrate($product_id); if (!empty($discount_info["amount"])) { if ($auth["show_price_including_tax"] == 1) { switch ($discount_info["is_percent"]) { case 0: $price["product_price"] = ($price["product_price"] * ($my_taxrate + 1) - $discount_info["amount"]) / ($my_taxrate + 1); break; //case 1: $price["product_price"] = ($price["product_price"]*($my_taxrate+1) - $discount_info["amount"]/100*$price["product_price"])/($my_taxrate+1); break; //case 1: $price["product_price"] = ($price["product_price"]*($my_taxrate+1) - $discount_info["amount"]/100*$price["product_price"])/($my_taxrate+1); break; case 1: $price["product_price"] = $price["product_price"] - $discount_info["amount"] / 100 * $price["product_price"]; break; } } else { switch ($discount_info["is_percent"]) { case 0: $price["product_price"] = $price["product_price"] - $discount_info["amount"]; break; case 1: $price["product_price"] = $price["product_price"] - $discount_info["amount"] / 100 * $price["product_price"]; break; } } } return $price; }
function get_adjusted_attribute_price($product_id, $quantity = 0, $description = '', $result_attributes = '') { global $mosConfig_secret; $auth = $_SESSION['auth']; $price = $this->get_price($product_id, $quantity, true, $result_attributes); $base_price = $price["product_price"]; $setprice = 0; $set_price = false; $adjustment = 0; // We must care for custom attribute fields! Their value can be freely given // by the customer, so we mustn't include them into the price calculation // Thanks to AryGroup@ua.fm for the good advice if (empty($_REQUEST["custom_attribute_fields"])) { if (!empty($_SESSION["custom_attribute_fields"])) { $custom_attribute_fields = vmGet($_SESSION, "custom_attribute_fields", array()); $custom_attribute_fields_check = vmGet($_SESSION, "custom_attribute_fields_check", array()); } else { $custom_attribute_fields = $custom_attribute_fields_check = array(); } } else { $custom_attribute_fields = $_SESSION["custom_attribute_fields"] = vmGet($_REQUEST, "custom_attribute_fields", array()); $custom_attribute_fields_check = $_SESSION["custom_attribute_fields_check"] = vmGet($_REQUEST, "custom_attribute_fields_check", array()); } // if we've been given a description to deal with, get the adjusted price if ($description != '') { // description is safe to use at this point cause it's set to '' require_once CLASSPATH . 'ps_product_attribute.php'; $product_attributes = ps_product_attribute::getAdvancedAttributes($product_id, true); $attribute_keys = explode(";", $description); for ($i = 0; $i < sizeof($attribute_keys); $i++) { $temp_desc = $attribute_keys[$i]; $temp_desc = trim($temp_desc); // Get the key name (e.g. "Color" ) $this_key = substr($temp_desc, 0, strpos($temp_desc, ":")); $this_value = substr($temp_desc, strpos($temp_desc, ":") + 1); if (in_array($this_key, $custom_attribute_fields)) { if (@$custom_attribute_fields_check[$this_key] == md5($mosConfig_secret . $this_key)) { // the passed value is valid, don't use it for calculating prices continue; } } if (isset($product_attributes[$this_key]['values'][$this_value])) { $modifier = $product_attributes[$this_key]['values'][$this_value]['adjustment']; $operand = $product_attributes[$this_key]['values'][$this_value]['operand']; // if we have a number, allow the adjustment if (true == is_numeric($modifier)) { // $modifier = $GLOBALS['CURRENCY']->convert( $modifier, $price['product_currency'], $GLOBALS['product_currency'] ); // Now add or sub the modifier on if ($operand == "+") { $adjustment += $modifier; } else { if ($operand == "-") { $adjustment -= $modifier; } else { if ($operand == '=') { // NOTE: the +=, so if we have 2 sets they get added // this could be moded to say, if we have a set_price, then // calc the diff from the base price and start from there if we encounter // another set price... just a thought. $setprice += $modifier; $set_price = true; } } } } } else { continue; } } } // no set price was set from the attribs if ($set_price == false) { $price["product_price"] = $base_price + $adjustment * (1 - $auth["shopper_group_discount"] / 100); } else { // otherwise, set the price // add the base price to the price set in the attributes // then subtract the adjustment amount // we could also just add the set_price to the adjustment... not sure on that one. if (!empty($adjustment)) { $setprice += $adjustment; } $setprice *= 1 - $auth["shopper_group_discount"] / 100; $price["product_price"] = $setprice; } // don't let negative prices get by, set to 0 if ($price["product_price"] < 0) { $price["product_price"] = 0; } // Get the DISCOUNT AMOUNT $ps_product = new ps_product(); $discount_info = $ps_product->get_discount($product_id); // Read user_info_id from db $dbu = new ps_DB(); $q = "SELECT user_info_id FROM #__{vm}_orders WHERE order_id = '" . $this->order_id . "' "; $dbu->query($q); $dbu->next_record(); $user_info_id = $dbu->f("user_info_id"); $prod_weight = $ps_product->get_weight($product_id); $my_taxrate = $ps_product->get_product_taxrate($product_id, $prod_weight, $user_info_id); // If discounts are applied after tax, but prices are shown without tax, // AND tax is EU mode and shopper is not in the EU, // then ps_product::get_product_taxrate() returns 0, so $my_taxrate = 0. // But, the discount still needs to be reduced by the shopper's tax rate, so we obtain it here: if (PAYMENT_DISCOUNT_BEFORE != '1' && $auth["show_price_including_tax"] != 1 && !ps_checkout::tax_based_on_vendor_address($user_info_id)) { $db = new ps_DB(); $ps_vendor_id = $_SESSION["ps_vendor_id"]; require_once CLASSPATH . 'ps_checkout.php'; if (!ps_checkout::tax_based_on_vendor_address($user_info_id)) { if ($auth["user_id"] > 0) { $q = "SELECT state, country FROM #__{vm}_user_info WHERE user_id='" . $auth["user_id"] . "'"; $db->query($q); $db->next_record(); $state = $db->f("state"); $country = $db->f("country"); $q = "SELECT tax_rate FROM #__{vm}_tax_rate WHERE tax_country='{$country}' "; if (!empty($state)) { $q .= "AND (tax_state='{$state}' OR tax_state=' {$state} ' OR tax_state='-')"; } $db->query($q); if ($db->next_record()) { $my_taxrate = $db->f("tax_rate"); } else { $my_taxrate = 0; } } else { $my_taxrate = 0; } } else { if (empty($_SESSION['taxrate'][$ps_vendor_id])) { // let's get the store's tax rate $q = "SELECT `tax_rate` FROM #__{vm}_vendor, #__{vm}_tax_rate "; $q .= "WHERE tax_country=vendor_country AND #__{vm}_vendor.vendor_id=1 "; // !! Important !! take the highest available tax rate for the store's country $q .= "ORDER BY `tax_rate` DESC"; $db->query($q); if ($db->next_record()) { $my_taxrate = $db->f("tax_rate"); } else { $my_taxrate = 0; } } else { $my_taxrate = $_SESSION['taxrate'][$ps_vendor_id]; } } } // Apply the discount if (!empty($discount_info["amount"])) { $undiscounted_price = $base_price; switch ($discount_info["is_percent"]) { case 0: if (PAYMENT_DISCOUNT_BEFORE == '1') { // If we subtract discounts BEFORE tax // Subtract the whole discount $price["product_price"] -= $discount_info["amount"]; } else { // But, if we subtract discounts AFTER tax // Subtract the untaxed portion of the discount $price["product_price"] -= $discount_info["amount"] / ($my_taxrate + 1); } break; case 1: $price["product_price"] -= $price["product_price"] * ($discount_info["amount"] / 100); break; } } return $price; }