VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: sandomatyas on January 15, 2019, 10:51:21 AM

Title: Calculate custom price
Post by: sandomatyas on January 15, 2019, 10:51:21 AM
I wrote a vmcustom plugin which displays a html select with product variations but these variations don't loaded from VM's own tables but from external database.
plgVmOnDisplayProductFEVM3 gets the variants and renders the layout
plgVmOnViewCartVM3 handles the cart display
plgVmPrepareCartProduct handles the price

Every option has different price in the remote database.

The question is:

Of course I need to use the taxes, discounts after that.

Could you please help me how show I get these properly?

Thanks
Title: Re: Calculate custom price
Post by: Milbo on January 16, 2019, 13:49:12 PM
Did you see the commented code?


//$product->product_name .= 'Ice Saw';
//vmdebug('plgVmPrepareCartProduct we can modify the product here');


So you can change the product name. I did not check how the system behaves changing the price. But variants are done before product price calculation. So it should work.
Title: Re: Calculate custom price
Post by: PRO on January 16, 2019, 19:19:54 PM
in   plgVmPrepareCartProduct
your return of     $modificatorSum    is the price. (or is the effect on the price)


1 example

//Prepare Cart
   public function plgVmPrepareCartProduct(&$product, &$customfield,$selected,&$modificatorSum = 0){
      if ($customfield->custom_element !==$this->_name) return;
      //$product->product_name = 'Ice Saw';
   //   $product->product_weight = '0';
   if (!empty($customfield->under_disc)){
   if ($product->quantity <= $customfield->under_disc){$product->product_weight=$customfield->under_discmod;}
   }
   if (!empty($customfield->modifier)){
   if ($product->quantity>=$customfield->modifier){$product->product_weight=0;}
   }
   if (!empty($customfield->disc)){
   if ($product->quantity >=$customfield->disc && $product->quantity <$customfield->modifier){$product->product_weight=$customfield->discmod;}
   }
   
   // return prices
$modificatorSum+= $this->applyDiscount($customfield->qdisc1,$customfield->qdisc2,$customfield->qdisc3,$product->quantity,$product->allPrices[$product->selectedPrice]["product_price"])-$product->allPrices[$product->selectedPrice]["product_price"];
   
      return ;
      //vmdebug('plgVmPrepareCartProduct we can modify the product here');

   }


another example
   public function plgVmPrepareCartProduct(&$product, &$customfield,$selected,&$modificatorSum = 0){
      if ($customfield->custom_element !==$this->_name) return;
      //$product->product_name = 'Ice Saw';
      //vmdebug('plgVmPrepareCartProduct we can modify the product here');
      if (!empty ($selected['custom_drop'])) {
         $options = explode(',', $customfield->custom_drop);
         foreach ($options as $valuesWithPrice) {
            $valueWithPrice = explode('@', $valuesWithPrice);
            if ($selected['custom_drop'] == $valueWithPrice[0]) {
               if (isset ($valueWithPrice[1])) {
                  $op = $valueWithPrice[1][0];
                  $qdropmodprice = $product->allPrices[$product->selectedPrice]["product_price"];
                  $qnewmodprice = 0;
                  switch ($op) {
                     case "+":
                        $modificatorSum += (float)substr($valueWithPrice[1], 1);
                        break;
                     case "=":
                        $qdropfixed = (float)substr($valueWithPrice[1], 1);
                        if ($qdropfixed > 0) {
                           $qnewmodprice = $qdropfixed - $qdropmodprice;
                        }
                        $modificatorSum += $qnewmodprice;
                        break;
                     case "-":
                        $modificatorSum -= (float)substr($valueWithPrice[1], 1);
                        break;
                     case "*":
// Use the price for this customer and multiply it by the variant -1 as this then gets added to the price for a single product
// You do not need @*1 but need @*2

                        $qdropmultiplier = substr($valueWithPrice[1], 1);
                        if ($qdropmultiplier > 0) {
                           $qnewmodprice = $qdropmodprice * ($qdropmultiplier -1);
                           }
                           $modificatorSum += $qnewmodprice;
                        break;
                     default:
                        $modificatorSum += (float)$valueWithPrice[1];
                  }
               }
               return;
            }
         }
      }
   }




you could download 
https://forum.virtuemart.net/index.php?topic=127362.0


and modify it