Badger
Badger

Reputation: 166

Get custom Magento attribute value for calculation

On the Magento product page I need to construct a value of a price per piece. The product is sold as a pack of an X amount. For example I buy 1 pack of coffee cups. The pack contains 100 cups and is (tier)priced as $10. The price per piece would be $0.10 (tier price / amount per pack). For this I'm configuring the /product/view/attributes.phtml template by adding a fixed row to the table.

What would be the best way of retrieving the custom attribute (amount_per_pack) together with the tier price of the product?

Upvotes: 1

Views: 690

Answers (1)

Badger
Badger

Reputation: 166

We've managed solving the issue already ourselves. In case someone else needs such a solution I'll post the answer:

Using the attributes.html template you already have pretty much all product attributes at your disposal. First we call for the pack amount:

<?php $_packamount = $_product->getPackAmount(); ?>

Then call for the tier price

<?php $_tierPrices = $_product->getTierPrice(1); ?>

Putting it all together by dividing the tier price by the pack amount:

<?php $_pieceprice = $_tierPrices / $_packamount; ?>

And echo the result back into the template:

<?php echo (round($_pieceprice,2)); ?>

Pretty simple and straightforward

Upvotes: 1

Related Questions