We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1847
    • 5 Posts
    Hi all,

    I created model with xPDO. Everything is working fine. But I have got one question:

    I’ve got fields ’item_price’ and ’item_price_after_discount’. I want to create pseudo-field called ’item_discount_percent’, which is not in DB, which will return a percentage of discount. And I would like to this field be available through get() and toArray() methods.

    It surely must be done by exteding xPDOObject or the Item class. But I don’t know how to extend it.

    Thanks for your advice smiley
      • 28215
      • 4,149 Posts
      In your custom class (i’ll call it Item), you need to override the get method. I assume your logic would look something like this:
      class Item extends xPDOObject {
       /* ... */
      
       public function get($k, $format = null, $formatTemplate= null) {
         switch ($k) {
           case 'item_discount_percent':
             $v = ($this->get('item_price_after_discount') / $this->get('item_price')) * 100;
             break;
           default:
             $v = parent::get($k,$format,$formatTemplate);
             break;
         }
         return $v;
       }
      }
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 1847
        • 5 Posts
        Thanks a lot smiley