Yearly Archives: 2014


Magento check bundled product visibility

I’ve created a custom module that adds a hyperlink to bundled products on the product view page, however it needs to consider the product’s visibility since Magento will return an error page when visiting a product that is hidden in the catalog. Apparently calling $_product->getVisibility() would return the integer 1-4 we’re looking for on Magento version 1.5, but not so on 1.8. The way I was able to accomplish this with Magento 1.8 is by loading the product and then checking isVisibleInSiteVisibility() like this:

//load full product for bundled selection so we can check visibility		
$selectionProduct = Mage::getModel('catalog/product')->load($_selection->getId());
 
if ($selectionProduct->isVisibleInSiteVisibility()) {
	$priceTitle .= '<a href="' . $_selection->getProductUrl() . '">';
}
$priceTitle .= $this->escapeHtml($_selection->getName());
if ($selectionProduct->isVisibleInSiteVisibility()) {
	$priceTitle .= '</a>';
}

This is applied to the four files at Yourmodule/Bundle/Block/Catalog/Product/View/Type/Bundle/Option/ . The full code for Yourmodule/Bundle/Block/Catalog/Product/View/Type/Bundle/Option/Select.php is

<?php
class Yourmodule_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Select
	extends Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Select 
{
 
	public function getSelectionTitlePrice($_selection, $includeContainer = true)
	{
		$price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection, 1);
		$tierPrice = $_selection->getTierPrice();
		if (!empty($tierPrice)) {
			$qty = $_selection->getSelectionQty();
			$price = $qty * (float) $_selection->getPriceModel()->getTierPrice($qty, $_selection);
		}
		$this->setFormatProduct($_selection);
 
		$priceTitle = '';
 
		//load full product for bundled selection so we can check visibility		
		$selectionProduct = Mage::getModel('catalog/product')->load($_selection->getId());
 
		if ($selectionProduct->isVisibleInSiteVisibility()) {
			$priceTitle .= '<a href="' . $_selection->getProductUrl() . '">';
		}
		$priceTitle .= $this->escapeHtml($_selection->getName());
		if ($selectionProduct->isVisibleInSiteVisibility()) {
			$priceTitle .= '</a>';
		}
 
		$priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
		. '+' . $this->formatPriceString($price, $includeContainer)
		. ($includeContainer ? '</span>' : '');
		return $priceTitle;
	}
 
}

bower install or update error: unable to connect a socket (Connection timed out)

When running bower install or update if you receive the error “unable to connect a socket (Connection timed out)” related to a dependency that’s installed via git, run the following command to change the protocol “git config –global url.”https://”.insteadOf git://” . Then bower install and update should be able to find the repository.


How to install Node and NPM on Linux CentOS 5 2

Once logged into the server as root, I initially ran “yum install nodejs” and and was presented with the “no package nodejs available” message, which means yum couldn’t find it. So we need to add the EPEL repository so it can be installed with yum, and then proceed to install nodejs and npm.

#Add the EPEL Repository allow yum to intall nodejs
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

#The epel release version might be slightly different when you download it, so change the command as needed to point at the right file.
rpm -Uhv epel-release-6-8.noarch.rpm

#install node
yum install nodejs

#enter ‘y’ for the two or three prompts

#install npm
yum install npm

#enter ‘y’ for the prompt

And that’s it. If for some reason you get “no package nodejs available” still, then clear the yum cache with “yum clean all” so it uses the new repository and then try to install node again.


PHP floats are not created equal

I was given a bug report today and after digging into the problem I found that two floating point numbers that appeared to be equal were not.

Here is a little code and the output. At the bottom is the actual value of each.

var_dump($paymentAmount);
var_dump($amountDue);

if($paymentAmount > $amountDue) {
echo 'is greater';
} else {
echo 'equal or less';
}
printf("%.40f\n", $paymentAmount) . ' ';
printf("%.40f\n", $amountDue)

float(51.01) 
float(51.01) 
is greater
51.0099999999999980104803398717194795608521 
51.0099999999999909050529822707176208496094

PHP acknowledges the issues here under “comparing floats” http://php.net/manual/en/language.types.float.php

The PHP website provides a way to test the number for equality by limiting them to 5 decimals –

To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, or unit roundoff, and is the smallest acceptable difference in calculations.

$a and $b are equal to 5 digits of precision.

$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;

if(abs($a-$b) < $epsilon) {
    echo "true";
}