Jump to content

Joomla PHP error


Alec
 Share

Recommended Posts

Hi guys,I am having an issue with my php code on Joomla.I am trying to hack the code to build to set the discounted price of a product if two or more product are chosen.the way I am doing it is by matching the category ID...so if the shopping cart has the same category ID for two times or more then apply the discounted prices for those products. "in my case the categoryID = 142"somehow it is not working...I would appreciate your helpthe code of the page is below and the red text is holding the get product method.. function get_price($product_id, $check_multiple_prices=false, $overrideShopperGroup='' ) { if( empty( $product_id)) return array(); $auth = $_SESSION['auth']; $cart = $_SESSION['cart']; if( empty( $GLOBALS['product_info'][$product_id]['price'] ) || !empty($GLOBALS['product_info'][$product_id]['price']["product_has_multiple_prices"]) || $check_multiple_prices) { $db = new ps_DB; $vendor_id = $this->get_vendor_id($product_id); if( $overrideShopperGroup === '') { $shopper_group_id = $auth["shopper_group_id"]; $shopper_group_discount = $auth["shopper_group_discount"]; } else { $shopper_group_id = $overrideShopperGroup; $shopper_group_discount = 0; } ps_shopper_group::makeDefaultShopperGroupInfo(); // Get the product_parent_id for this product/item $product_parent_id = $this->get_field($product_id, "product_parent_id"); // LW PATCH - Get the category of this product - used later to drive category-based multiple purchases $sesq = "SELECT category_id FROM #__{vm}_product_category_xref WHERE product_id='$product_id'"; $db->setQuery($sesq); $db->query(); $db->next_record(); $sesprodcat = $db->f("category_id"); if ($sesprodcat == 142) { $ses_cat_discount = TRUE; } else { $ses_cat_discount = FALSE; } // END LW PATCH if( !$check_multiple_prices ) { /* Added for Volume based prices */ // This is an important decision: we add up all product quantities with the same product_id, // regardless to attributes. This gives "real" volume based discount, because our simple attributes // depend on one and the same product_id $quantity = 0; $parent_id = ""; if ($product_parent_id) { $parent = true; } else { $parent = false; } for ($i=0;$i<$cart["idx"];$i++) { // LW PATCH - If any product in the cart is in the same category then treat as volume purchase if ($ses_cat_discount) { $sesq = "SELECT category_id FROM #__{vm}_product_category_xref WHERE product_id='".$cart[$i]["product_id"]."'"; $db->setQuery($sesq); $db->query(); $db->next_record(); $sesprodchildcat = $db->f("category_id"); if ($sesprodchildcat == $sesprodcat) { $quantity += $cart[$i]["quantity"]; } } else { if ($cart[$i]["product_id"] == $product_id) { if ($parent) { $parent_id = $cart[$i]["parent_id"]; } else { $quantity += $cart[$i]["quantity"]; } } } } // End LW PATCH if ($parent) { for ($i=0;$i<$cart["idx"];$i++) { if (@$cart[$i]['parent_id'] == $parent_id) { $quantity += $cart[$i]["quantity"]; } } } $volume_quantity_sql = " ORDER BY price_quantity_start"; if( $quantity > 0 ) { $volume_quantity_sql = " AND (('$quantity' >= price_quantity_start AND '$quantity' <= price_quantity_end) OR (price_quantity_end='0') OR ('$quantity' > price_quantity_end)) ORDER BY price_quantity_end DESC"; } } else { $volume_quantity_sql = " ORDER BY price_quantity_start"; } // Get the price array $price = $this->getPriceByShopperGroup( $product_id, $shopper_group_id, $check_multiple_prices, $volume_quantity_sql ); if( !$price && $product_parent_id ) { // If this is a child product and it has not a price for the requested shopper group, get the price for the default shopper group $price = $this->getPriceByShopperGroup( $product_id, $GLOBALS['vendor_info'][$vendor_id]['default_shopper_group_id'], $check_multiple_prices, $volume_quantity_sql ); if( !$price ) { // if the child product has no priceat all, get the price of the parent product for that shopper group $price = $this->getPriceByShopperGroup( $product_parent_id, $shopper_group_id, $check_multiple_prices, $volume_quantity_sql ); } if( !$price ) { // if the parent product has no price for the requested shopper group, get the price of the default shopper group $price = $this->getPriceByShopperGroup( $product_parent_id, $shopper_group_id, $check_multiple_prices, $volume_quantity_sql ); } } elseif( !$price ) { // if the product has no price for the requested shopper group, get the price of the default shopper group $price = $this->getPriceByShopperGroup( $product_id, $GLOBALS['vendor_info'][$vendor_id]['default_shopper_group_id'], $check_multiple_prices, $volume_quantity_sql ); } return $price; } else { return $GLOBALS['product_info'][$product_id]['price']; } } /** * Returns the price for a specific shopper group, * Returns nothing, when the shopper group has no price * * @param int $product_id * @param int $shopper_group_id * @param boolean $check_multiple_prices * @param string $additionalSQL * @return mixed */ function getPriceByShopperGroup( $product_id, $shopper_group_id, $check_multiple_prices=false, $additionalSQL='' ) { global $auth; static $resultcache = array(); $db = new ps_DB; $vendor_id = $_SESSION['ps_vendor_id']; if( empty( $shopper_group_id )) { ps_shopper_group::makeDefaultShopperGroupInfo(); $shopper_group_id = $GLOBALS['vendor_info'][$vendor_id]['default_shopper_group_id']; } $whereClause='WHERE product_id=%s AND shopper_group_id=%s '; $whereClause = sprintf( $whereClause, intval($product_id), intval($shopper_group_id) ); $q = "SELECT `product_price`, `product_price_id`, `product_currency` FROM `#__{vm}_product_price` $whereClause $additionalSQL"; $sig = sprintf("%u\n", crc32($q)); if( !isset($resultcache[$sig])) { $db->query($q); if( !$db->next_record() ) return false; $price_info["product_price"]= $db->f("product_price") * ((100 - $auth["shopper_group_discount"])/100); $price_info["product_currency"] = $db->f("product_currency"); $price_info["product_base_price"]= $db->f("product_price") * ((100 - $auth["shopper_group_discount"])/100); $price_info["product_has_multiple_prices"] = $db->num_rows() > 1; $price_info["product_price_id"] = $db->f("product_price_id"); $price_info["item"]=true; $GLOBALS['product_info'][$product_id]['price'] = $price_info; // Store the result for later $resultcache[$sig] = $price_info; return $GLOBALS['product_info'][$product_id]['price']; } else { return $resultcache[$sig]; } } /** * Adjusts the price from get_price for the selected attributes * @author Nathan Hyde <nhyde@bigDrift.com> * @author curlyroger from his post at <http://www.phpshop.org/phpbb/viewtopic.php?t=3052> * * @param int $product_id * @param string $description * @return array The adjusted price information */ function get_adjusted_attribute_price ($product_id, $description='') { global $mosConfig_secret; $auth = $_SESSION['auth']; $price = $this->get_price($product_id); $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) ) { // 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 $discount_info = $this->get_discount( $product_id ); $my_taxrate = $this->get_product_taxrate($product_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() ) { $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 ()) { 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; } } } } // 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; } /** * This function can parse an "advanced / custom attribute" * description like * Size:big[+2.99]; Color:red[+0.99] * and return the same string with values, tax added * Size: big (+3.47), Color: red (+1.15) * * @param string $description * @param int $product_id * @return string The reformatted description */ function getDescriptionWithTax( $description, $product_id=0 ) { global $CURRENCY_DISPLAY, $mosConfig_secret; require_once(CLASSPATH.'ps_product_attribute.php'); $auth = $_SESSION['auth']; $description = stripslashes($description); // if we've been given a description to deal with, get the adjusted price if ($description != '' && $auth["show_price_including_tax"] == 1 && $product_id != 0 ) { $my_taxrate = $this->get_product_taxrate($product_id); $price = $this->get_price( $product_id ); $product_currency = $price['product_currency']; } else { $my_taxrate = 0.00; $product_currency = ''; } // 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() ); } $product_attributes = ps_product_attribute::getAdvancedAttributes($product_id); $attribute_keys = explode( ";", $description ); foreach( $attribute_keys as $temp_desc ) { $finish = strpos($temp_desc,"]"); $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']; $value_notax = $GLOBALS['CURRENCY']->convert( $modifier, $product_currency ); if( abs($value_notax) >0 ) { $value_taxed = $value_notax * ($my_taxrate+1); $temp_desc_new = str_replace( $operand.$modifier, $operand.' '.$CURRENCY_DISPLAY->getFullValue( $value_taxed ), $temp_desc ); $description = str_replace( $this_key.':'.$this_value, $this_key.':'.$this_value.' ('.$operand.' '.$CURRENCY_DISPLAY->getFullValue( $value_taxed ).')', $description); } $temp_desc = substr($temp_desc, $finish+1); } } $description = str_replace( $CURRENCY_DISPLAY->symbol, '@saved@', $description ); $description = str_replace( "[", " (", $description ); $description = str_replace( "]", ")", $description ); $description = str_replace( ":", ": ", $description ); $description = str_replace( ";", "<br/>", $description ); $description = str_replace( '@saved@', $CURRENCY_DISPLAY->symbol, $description ); return $description; } function calcEndUserprice( $product_id, $overrideShoppergroup ) { global $VM_LANG, $CURRENCY_DISPLAY; $auth = $_SESSION['auth']; // Get the DISCOUNT AMOUNT $discount_info = $this->get_discount( $product_id ); // Get the Price according to the quantity in the Cart $price_info = $this->get_price( $product_id, false, $overrideShoppergroup ); if (isset($price_info["product_price_id"])) { $base_price = $price_info["product_price"]; $price = $price_info["product_price"]; if ($auth["show_price_including_tax"] == 1) { $my_taxrate = $this->get_product_taxrate($product_id); $base_price += ($my_taxrate * $price); } else { $my_taxrate = 0; } $tax = $my_taxrate * 100; $price_info['tax_rate'] = $VM_LANG->_('PHPSHOP_TAX_LIST_RATE').': '.$tax.'%'; // Calculate discount if( !empty($discount_info["amount"])) { switch( $discount_info["is_percent"] ) { case 0: $base_price -= $discount_info["amount"]; $price_info['discount_info'] = $VM_LANG->_('PHPSHOP_PRODUCT_DISCOUNT_LBL').': '.$CURRENCY_DISPLAY->getFullValue($discount_info['amount']); break; case 1: $base_price *= (100 - $discount_info["amount"])/100; $price_info['discount_info'] = $VM_LANG->_('PHPSHOP_PRODUCT_DISCOUNT_LBL').': '.$discount_info['amount'].'%'; break; } } $price_info['product_price'] = $base_price; } return $price_info; }

Edited by Alec
Link to comment
Share on other sites

well, there is no error but the function works when I add items to the shoppping cart, ...adding more items and deleting some in the shopping cart and updating the cart is updating the price to the regular price instead of the discounted one.Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...