Author Topic: Can I calculate a price based also on a Custom Attribute value?  (Read 5886 times)

von Gruz

  • Guest
Can I calculate a price based also on a Custom Attribute value?

e.g.
I have product Windows 2005 Server
The price is calculated from
CD (yes|no)
Server License (1000 USD per each)
Client License (30 USD per each)

A cutomer must select the number of Server Licenses and Client licenses, each of the license has it's cost. The quantity of the licenses has no limit, that's why I can't use a regular Attribute as it's pre-defined.

I want a customer to enter the number of licenses he wants to buy manually in a Custom Attribute filed and to calculate the price based also on the value of the field. It that possible without a hack?
If no, what have I to hack?

von Gruz

  • Guest
Re: Can I calculate a price based also on a Custom Attribute value?
« Reply #1 on: August 25, 2005, 18:49:17 pm »
Can I calculate a price based also on a Custom Attribute value?

e.g.
I have product Windows 2005 Server
The price is calculated from
CD (yes|no)
Server License (1000 USD per each)
Client License (30 USD per each)

A cutomer must select the number of Server Licenses and Client licenses, each of the license has it's cost. The quantity of the licenses has no limit, that's why I can't use a regular Attribute as it's pre-defined.

I want a customer to enter the number of licenses he wants to buy manually in a Custom Attribute filed and to calculate the price based also on the value of the field. It that possible without a hack?
If no, what have I to hack?

I've found where to modify
The function is in ps_product.php and is called get_adjusted_attribute_price

To solve my problem I made the following:
1.I enter my custom attributes in the following format
Server License<!*5000>;Client license<!*25> where 5000 and 25 is a price per one. The quantity of the licenses my customer enters when ordering a product.
2.I hack the function get_adjusted_attribute_price so that it works with my new format of custom attributes.
Quote
  function get_adjusted_attribute_price ($product_id, $description='') {
   
    global $auth;
    $price = $this->get_price($product_id);
   
    $base_price = $price["product_price"];
    $setprice = 0;
    $set_price = false;
    $adjustment = 0;
   
      // 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 ''
      $temp_desc = $description;
      $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";
     
      while (true == (false !== $start)) {
        // 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? ;)
      }
    }

   //##my Початок. Тут обробляю випадок для custom attributes.
   $start = ''; $finish='';$start_value = ''; $finish_value=''; $my_mod='';
      $temp_desc_for_custom = $description.";";//##Окремо зберігаю параметри для роботи з custom attributes
   $start = strpos($temp_desc_for_custom, "<");
   $finish = strpos($temp_desc_for_custom,">", $start);

   while (true == (false !== $start)) {

      // check to see if we have a bracket
      if (True == is_int($finish) ) {
            $length = $finish-$start;

         $start_value = strpos($temp_desc_for_custom, ":", $finish); //##my Тут шукаю позицію знака : який передує введеному юзером значенню
         $finish_value = strpos($temp_desc_for_custom,";", $start_value ); //##my Тут шукаю позицію знака ; який завершує введене юзером значення
         $length_value = $finish_value-$start_value;

            // We found a pair of brackets (price modifier?)
         if ($length > 1) {
            $my_mod=substr($temp_desc_for_custom, $start+1, $length-1);
            $my_mod_value=trim(substr($temp_desc_for_custom, $start_value+1, $length_value-1));
            //echo "before: ".$my_mod."<br>\n";
         }

         $oper=substr($my_mod,1,1);
   
         $my_mod=substr($my_mod,2);

         // if we have a number, allow the adjustment
         if (true == is_numeric($my_mod) && true == is_numeric($my_mod_value)) {
            // Now add or sub the modifier on
            if ($oper=="*") {
               $adjustment += $my_mod*$my_mod_value;
            }
            else if ($oper=="/") {
               $adjustment += $my_mod/$my_mod_value;
            }
            else if ($oper=="\\\\") {
               $adjustment += $my_mod_value/$my_mod;
            }
         }
         else echo "Ви ввели не числове значення!";

         $temp_desc_for_custom = substr($temp_desc_for_custom, $finish+1);
         $start = strpos($temp_desc_for_custom, "<");
         $finish = strpos($temp_desc_for_custom,">");
      }
   }
   //##my Кінець

    // 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.
      $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( !empty($discount_info["amount"])) {
      if ( PAYMENT_DISCOUNT_BEFORE == '1' ) {
        switch( $discount_info["is_percent"] ) {
          case 0: $price["product_price"] = ($price["product_price"]-$discount_info["amount"]); break;
          case 1: $price["product_price"] *= (100 - $discount_info["amount"])/100; break;
        }
      } elseif( $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;
        }
      }
      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"]/$price["product_price"]); break;
        }
      }
    }
   
    return $price;
  }


If someone have problems you may ask me. My ICQ is 21971586, my mail is AryGroup[a]ua.fm

May the Force be with you :-)

dubey

  • Beginner
  • *
  • Posts: 21
Re: Can I calculate a price based also on a Custom
« Reply #2 on: April 28, 2007, 21:15:24 pm »
does this work?