Question

I want to echo a string in my price.html and im using this if statement

        <?php if($_product->getData('bogof')==1){
            echo 'test';} ?>

but it doesn't print anything i know the IF statement works as i use it in other places on my website. This is the context i am trying to use it in.

<?php $this->setProduct(
    Mage::getModel("catalog/product")->getCollection()
        ->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
        ->addAttributeToFilter("entity_id", $this->getProduct()->getId())
        ->setPage(1, 1)
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->load()
        ->getFirstItem()
);?>
<?php
$_coreHelper = $this->helper('core');
$_weeeHelper = $this->helper('weee');
$_taxHelper = $this->helper('tax');
/* @var $_coreHelper Mage_Core_Helper_Data */
/* @var $_weeeHelper Mage_Weee_Helper_Data */
/* @var $_taxHelper Mage_Tax_Helper_Data */


$_product = $this->getProduct();
$_storeId = $_product->getStoreId();
$_store = $_product->getStore();
$_id = $_product->getId();
$_weeeSeparator = '';
$_simplePricesTax = ($_taxHelper->displayPriceIncludingTax() || $_taxHelper->displayBothPrices());
$_minimalPriceValue = $_product->getMinimalPrice();
$_minimalPriceValue = $_store->roundPrice($_store->convertPrice($_minimalPriceValue));
 $_minimalPrice = $_taxHelper->getPrice($_product, $_minimalPriceValue);
$_convertedFinalPrice = $_store->roundPrice($_store->convertPrice($_product->getFinalPrice()));
$_specialPriceStoreLabel = $this->getProductAttribute('special_price')->getStoreLabel();
?>
<span class="price-excluding-tax" >

                    <span class="price" id="price-excluding-tax-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>">
                        <?php if ($_finalPrice == $_price): ?>
                            <?php echo $_coreHelper->formatPrice($_price, false) ?>
                        <?php else: ?>
                            <?php echo $_coreHelper->formatPrice($_finalPrice, false) ?>
                        <?php endif; ?>

                    </span>  
                <?php if($_product->getData('bogof')==1){
                    echo 'test';} ?>

                <span class="label"><?php echo $this->helper('tax')->__('Excl. VAT') ?></span>
                </span> 

Basically if the product has an attribute bogof and it is set to treu then it needs to print out test. But it isnt working and i am not sure why as it works in other files throughout my website.

When i add $test = Mage::getSingleton("catalog/config")->getProductAttributes();

then var_dump $test it returns

array(31) { [0]=> string(4) "name" [1]=> string(5) "price" [2]=> string(11) "small_image" [3]=> string(6) "status" [4]=> string(12) "tax_class_id" [5]=> string(7) "url_key" [6]=> string(9) "thumbnail" [7]=> string(17) "short_description" [8]=> string(13) "special_price" [9]=> string(17) "special_from_date" [10]=> string(15) "special_to_date" [11]=> string(14) "news_from_date" [12]=> string(12) "news_to_date" [13]=> string(16) "required_options" [14]=> string(10) "price_type" [15]=> string(11) "weight_type" [16]=> string(10) "price_view" [17]=> string(13) "shipment_type" [18]=> string(11) "image_label" [19]=> string(17) "small_image_label" [20]=> string(15) "thumbnail_label" [21]=> string(26) "links_purchased_separately" [22]=> string(11) "links_exist" [23]=> string(11) "is_imported" [24]=> string(12) "msrp_enabled" [25]=> string(30) "msrp_display_actual_price_type" [26]=> string(4) "msrp" [27]=> string(21) "c2c_paper_roll_holder" [28]=> string(10) "short_name" [29]=> string(15) "c2c_foot_switch" [30]=> string(9) "split_leg" }

And i have been told i need to add the bogog attribute to this array but i dont know how to that?

Was it helpful?

Solution

You need to make sure the call to the addAttributeToSelect() method against the product collection is adding your attribute.

->addAttributeToSelect('some_attribute', 'bogof')

or even just use * to select all attributes (although you may not want to do this in practice dependant on the number of attributes you have):

->addAttributeToSelect('*')

I would suggest looking at what is returned from Mage::getSingleton("catalog/config")->getProductAttributes() to make sure bogof isn't already included and if it isn't you can likely just add it in. This will make the attribute data available to the product loaded from the collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top