News:

Looking for documentation? Take a look on our wiki

Main Menu

Calculate custom price

Started by sandomatyas, January 15, 2019, 10:51:21 AM

Previous topic - Next topic

sandomatyas

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:

  • How do I set the proper price in plgVmPrepareCartProduct? I checked the textinput plugin but it uses $modificatorSum and I need to set the exact price insted modify it (except VAT, etc). So I can't say the the red one is more expensive by 2 EUR but the white one is cheaper. I have exact prices for the red one and the white one products.
  • I need to display these prices in the dropdown too like, Red - 6.9 EUR, White 5.88 EUR

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

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

Thanks

Milbo

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.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

PRO

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