Accessing GetPaid Attributes on Objects

by Horacio Duran last modified Dec 30, 2008 05:54 PM
A quick howto on how to access the GetPaid attributes of an object such as "price" or "product id" programmatically .

Purpose


Doing Plone based sites it is not rare to find the necessity of doing their own views, portlets or any kind of way of data display.
Now, this should not be a problem when we are showing regular attributes of our contents but, when our types are particularly something that we are using as GetPaid buyable, donatable or shippable types we may be stuck at the moment of getting some of its  specific attributes such as the price or product_id or even some other attribute even more specific such as shippable item weight.

 

Prerequisities


To try this you will need a Working Plone + GetPaid install and a buyable/donatable/shippable object type.

 

Step by step

 

At the moment of accessing a reglar attribute we know that just by calling

object.attribute

or

object.getAttribute()

will suffice to get the value of it but to get getpaid attributes is bit different.
These attributes are stored as annotations of the object, in order to retrieve them we need to adapt the object to buyable content and then get the annotated property.
So, lets imagine we have a CoffeePot object named PrettyCoffeePot with a price of $3.14, we can get its title by simply doing.

>>> PrettyCoffeePot.Title
>>> 'Pretty Coffee Pot'

But

>>> PrettyCoffeePot.Price
>>> AttributeError: 'CoffeePot' object has no attribute 'Price'

Ouch, so lets try something else, if our object is a GetPaid *able object is marked with one of the following markers (at least to this date)

  •  IShippableMarker
  •  IBuyableMarker
  •  IDonatableMarker
  •  IPremiumMarker

So, what we need to do is, we get the adapted interface and then get our annotated attributes.

If we have content that can be marked by any of the above we may want to find out which marker we have so, to begin with we will:
>>> from Products.PloneGetPaid.interfaces import PayableMarkerMap, IShippableMarker, IBuyableMarker, IDonatableMarker, IPremiumMarker
>>> if IBuyableMarker.providedBy(PrettyCoffeePot):

And then we proceed to get the interface

>>>     iface = PayableMarkerMap.get(IBuyableMarker, None) # Or any of the other markers
>>>     iface(PrettyCoffeePot).price
3.14

You can probe if any marker is provided by the current object, this is only an example adapted from PloneGetPaid cart portlet.

A few remarks:

  • You should check that iface is not None always
  • If you still cant get to the attribute like this make sure you are using the right marker for the object, if that fails your GetPaid install may have problems (Deleted GetPaid attributes have been seen sometimes after reinstall from one version to another) if you are doing one of these movements do backup and don't hesitate to drop a mail to the list or visit irc channel.
  • You can see more examples of this in GetPaid's own portlets or in GetPaidStore Product