Jump to content

Add product with my custom price to cart


abdess

Recommended Posts

Hello everyone,

 

in prestashop (1.6.1.5) on product detail page I have my custom price for the current product.

Now when I click on "add to cart" the standard product is added to the cart with standard price. I will add the product with my custom price to cart .

 

Could anyone give me some needable ideas in the right direction please...

 

Thank you very much

Link to comment
Share on other sites

 

thanks for your reply
 
can you give me an example?

 

 

Example

public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products = null, $id_carrier = null, $use_cache = true)
    {
    	$order_total = parent::getOrderTotal($with_taxes, $type, $products, $id_carrier, $use_cache);
    	// custom here
	$order_total += 5; // example
    	return Tools::ps_round((float)$order_total, $compute_precision);
    }
Link to comment
Share on other sites

thnx for you reply :  
 

so i have created an module which calculate an specific price for Product  and this  price is stored  in variable  $MyCustom_price and i want add this custom price  to CART when customer click on  "ADD TO CART"
 

here is my code 

function hookdisplayProductButtons(){   
        
        //generate Fields User
        $id_product = (int)Tools::getValue('id_product'); 
        
        $this->fields=$this->getUserFields($id_product);
        $this->context->smarty->assign("fields",$this->fields);
        $this->lagermaterial=$this->getLagermaterial();
        $this->context->smarty->assign("lagermaterial",$this->lagermaterial);
        if (Tools::getValue('submitKalkulation')) {
        	
        	$valueFields=Tools::getValue('data');
        	//print_r($valueFields);die();
        	$countFields=sizeof($valueFields); // count of posting data 
        	$name_field_user=array(); 
        	$value_field_user=array();
        	foreach ($valueFields as $val) {
        		//echo $val;
        		$name_field_user[]=array_search($val, $valueFields);  // name field on user page
        		$value_field_user[]=$val;                             //value field on user page 
        	}
            
            $this->id_material=Tools::getValue('selectMaterial');
            $getPrice=$this->getPriceMaterial($this->id_material)["price"]; // get price from selected lagermaterial by user 
             //start generate formula 
                  //get variables or fields admin 
                  $this->getVariables=$this->getVariables($id_product);
                  foreach($this->getVariables as $key => $value){
                    
                    $name_field[] =$value['name_field'];    // all field admin by product 
                    $value_field[]=$value['value_field'];   // all value admin by product 
                   }
                    
                   //push array fields and values user page into array  fields and values admin  
                   $allNames=array_merge($name_field,$name_field_user);
                   $allValues=array_merge($value_field,$value_field_user);   
                   $allNames[]="preis"; // add price  to array of allNames 
                   $allValues[]=$getPrice;  // add price  Value to array of allValues 
                                
                   
                   // get formula product 
                   $getFormula=$this->getProductFormula($id_product);
                   $formulaProduct[0]=$getFormula["formula"];
                
                  for ($i=0; $i <count($allValues) ; $i++) { 
      
                         $formulaProduct[$i+1]=str_replace($allNames[$i],$allValues[$i],$formulaProduct[$i]);
                  }
                  
                  //print_r($formulaProduct);echo "<br>"; 
                  $countValues=count($allValues);
                  eval("\$formulaProduct[$countValues] = $formulaProduct[$countValues];");
                  $MyCustom_price =$formulaProduct[$countValues];
                  $this->context->smarty->assign("MyCustom_price ",$MyCustom_price); // MyCustom price = final price
                  //get data in textbox
                 
                
                 // $this->context->smarty->assign('valueFields',$valueFields);
                 // $this->context->smarty->assign('SelectedMaterial',$this->id_material);
                  
                 
                  return $this->display(__FILE__, 'kalk_client.tpl');
                 
               
                   
                //end generate formula 
        }
       
		return $this->display(__FILE__, 'kalk_client.tpl');
	}
Link to comment
Share on other sites

  • 1 year later...
  • 1 month later...
On 13/03/2017 at 4:06 AM, phinq1910 said:

 

Example


public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products = null, $id_carrier = null, $use_cache = true)
    {
    	$order_total = parent::getOrderTotal($with_taxes, $type, $products, $id_carrier, $use_cache);
    	// custom here
	$order_total += 5; // example
    	return Tools::ps_round((float)$order_total, $compute_precision);
    }

Where i put this code?

Link to comment
Share on other sites

On 13/03/2017 at 4:31 AM, abdess said:

thnx for you reply :  
 

so i have created an module which calculate an specific price for Product  and this  price is stored  in variable  $MyCustom_price and i want add this custom price  to CART when customer click on  "ADD TO CART"
 

here is my code 


function hookdisplayProductButtons(){   
        
        //generate Fields User
        $id_product = (int)Tools::getValue('id_product'); 
        
        $this->fields=$this->getUserFields($id_product);
        $this->context->smarty->assign("fields",$this->fields);
        $this->lagermaterial=$this->getLagermaterial();
        $this->context->smarty->assign("lagermaterial",$this->lagermaterial);
        if (Tools::getValue('submitKalkulation')) {
        	
        	$valueFields=Tools::getValue('data');
        	//print_r($valueFields);die();
        	$countFields=sizeof($valueFields); // count of posting data 
        	$name_field_user=array(); 
        	$value_field_user=array();
        	foreach ($valueFields as $val) {
        		//echo $val;
        		$name_field_user[]=array_search($val, $valueFields);  // name field on user page
        		$value_field_user[]=$val;                             //value field on user page 
        	}
            
            $this->id_material=Tools::getValue('selectMaterial');
            $getPrice=$this->getPriceMaterial($this->id_material)["price"]; // get price from selected lagermaterial by user 
             //start generate formula 
                  //get variables or fields admin 
                  $this->getVariables=$this->getVariables($id_product);
                  foreach($this->getVariables as $key => $value){
                    
                    $name_field[] =$value['name_field'];    // all field admin by product 
                    $value_field[]=$value['value_field'];   // all value admin by product 
                   }
                    
                   //push array fields and values user page into array  fields and values admin  
                   $allNames=array_merge($name_field,$name_field_user);
                   $allValues=array_merge($value_field,$value_field_user);   
                   $allNames[]="preis"; // add price  to array of allNames 
                   $allValues[]=$getPrice;  // add price  Value to array of allValues 
                                
                   
                   // get formula product 
                   $getFormula=$this->getProductFormula($id_product);
                   $formulaProduct[0]=$getFormula["formula"];
                
                  for ($i=0; $i <count($allValues) ; $i++) { 
      
                         $formulaProduct[$i+1]=str_replace($allNames[$i],$allValues[$i],$formulaProduct[$i]);
                  }
                  
                  //print_r($formulaProduct);echo "<br>"; 
                  $countValues=count($allValues);
                  eval("\$formulaProduct[$countValues] = $formulaProduct[$countValues];");
                  $MyCustom_price =$formulaProduct[$countValues];
                  $this->context->smarty->assign("MyCustom_price ",$MyCustom_price); // MyCustom price = final price
                  //get data in textbox
                 
                
                 // $this->context->smarty->assign('valueFields',$valueFields);
                 // $this->context->smarty->assign('SelectedMaterial',$this->id_material);
                  
                 
                  return $this->display(__FILE__, 'kalk_client.tpl');
                 
               
                   
                //end generate formula 
        }
       
		return $this->display(__FILE__, 'kalk_client.tpl');
	}

Please, Where i do put this code???

Link to comment
Share on other sites

  • 2 weeks later...

Hi 

On 20/07/2018 at 5:39 PM, abdess said:

 

silviosmith

 

what do you want to do exactly?

i want to put a custom field in the product to show in add to cart  button, and this field have to be sum with others procucts, like a price... like a secund price...

 

Sem título.png

Sem título2.png

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
×
×
  • Create New...