Category : PHP

Php – read all files and folders from a directory

Read all files and folders from a directory using scandir: $dir = “/temp”; $files = scandir($dir); print_r($files); Read all files and folders from a directory using opendir: $dir = “/temp/” if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo “Filename: ” . $file . “: filetype: ” .

Read More →

Magento – cms tags

Display a magento block in CMS Page: {{block id=’block_id’}} {{block type=’module/package_classname’ template=’path/to/template.phtml’}} Escapes all HTML tags in the value of the var attribute, except for those specified in the value of the allowed_tags attribute. (The latter expects a comma separated list of allowed HTML tags). {{htmlescape var=” allowed_tags=”}} Inserts HTML layout output. The handle attribute

Read More →

Magento – product attribute

Get attribute collection: $attribute = $_product->getResource()->getAttribute(‘attribute_name’); Get attribute type: $attribute->getAttributeType(); Get attribute Label: $attribute->getFrontendLabel(); Get attribute default value: $attribute->getDefaultValue(); Check if the attribute is visible: $attribute->getIsVisible(); Check if the attribute is required: $attribute->getIsRequired();

Magento – get product images

This code can be used to get all images for product: $_product = Mage::getModel(‘catalog/product’); $_product->load($product_id); // Get image gallery $_gallery = $_product->getMediaGalleryImages(); // Go through each image foreach ($_gallery as $_image) { // Display image echo ‘<img src=\” . $_image->getUrl() . ‘\’ alt=\” . $_image->getLabel() . ‘\’ />’; }

Magento – products block

Go to “CMS – Manage Pages” and select the page do you want to display the products from the list of pages. Use next code to display products labeled as “new”: {{block type=”catalog/product_new” name=”catalog.product.new” alias=”new_products” template=”catalog/product/new.phtml”}} Use next code to display all products from catalog: {{block type=”catalog/product_list” name=”catalog.product.list” alias=”all_products” template=”catalog/product/list.phtml”}} Use next code to display

Read More →

PHP – runnig a script in command line

To run a script in command line: php test.php or php -f test.php To run a script in command line with parameters: php test.php param1 param2 param3 or php -f test.php param1 param2 param3 The list of used parameters can be found in: $_SERVER[‘argv’]

Php – script execution time

To view php script execution time insert next code into your script. Insert next code at the top of the page: <?php $mtime = microtime(); $mtime = explode(” “,$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; ?> Insert next code at the bottom of the page: <?php $mtime = microtime(); $mtime = explode(” “,$mtime);

Read More →

Magento check if customer is logged in

This is a method to verify if a customer is logged in or not: if(Mage::getSingleton(‘customer/session’)->isLoggedIn()) { // Code to execute }

Magento get all products

Next code is a method to get all product from magento database: $products = Mage::getModel(‘catalog/product’)->getCollection(); $products->addAttributeToFilter(‘status’, 1);//enabled $products->addAttributeToFilter(‘visibility’, 4); //catalog, search $products->addAttributeToFilter(‘type_id’, ‘simple’); //catalog, search $products->addAttributeToSelect(‘*’); $products->addStoreFilter($storeId); $prodIds = $products->getAllIds(); foreach($prodIds as $productId) { $product = Mage::getModel(‘catalog/product’); $product->load($product_id); }

1 2 3 4