kodal.de

it´s all about music, design and coding

Archive for the ‘ php ’ Category

Im Magento-Backend (1.4) gibt es keine Möglichkeit, einmal erstellte Rechnungen wieder zu löschen. Um eine einzelne Rechnung zu eliminieren, ist dieser MySQL-Query hilfreich:

delete from `sales_order_entity_datetime` where `entity_id`='$entityId' and `entity_type_id` IN (17,18,19);
delete from `sales_order_entity_decimal` where `entity_id`='$entityId' and `entity_type_id` IN (17,18,19);
delete from `sales_order_entity_int` where `entity_id`='$entityId' and `entity_type_id` IN (17,18,19);
delete from `sales_order_entity_text` where `entity_id`='$entityId' and `entity_type_id` IN (17,18,19);
delete from `sales_order_entity_varchar` where `entity_id`='$entityId' and `entity_type_id` IN (17,18,19);
delete from `sales_order_entity` where `parent_id`='$entityId' and `entity_type_id`='20';
delete from `sales_order_entity` where `entity_id`='$entityId' and `entity_type_id`='18';

Dahinter verbirgt sich folgende Logik:

"sales_order_entity_*":
delete from `$table` where `entity_id`='$entityId' and `entity_type_id` IN (17,18,19);

"sales_order_entity":
delete from `$table` where `parent_id`='$entityId' and `entity_type_id`='20';
delete from `$table` where `entity_id`='$entityId' and `entity_type_id`='18';

Popularity: 18% [?]

Problems with Product Visibility in Magento

When first setting up a Magento-eCommerce-site, almost everyone wonders why products are not being shown on the actual site, even though everything seems to be filled out correctly. In this little article, I’d like to explain how to make sure a product is shown on the site after it has been created via the Magento-backend.

Here goes a little checklist of what to do in order to have your products being shown in your Magento-eCommerce-Shop..

Checklist

  • in Catalog/Manage Products : fil out all required fields in each tab and click “save and continue edit” when finished
  • in Catalog/Manage Products/General : make sure “Status” is set to “Enabled”
  • in Catalog/Manage Products/General : set “Visibility” to “Catalog”, “Search” or “Catalog, Search”
  • in Catalog/Manage Products/Inventory : set “Qty” to a value greater than 0
  • in Catalog/Manage Products/Inventory : set “Stock Availability” to “In Stock”
  • in Catalog/Catgories : assign your product to at least one category (you might have to create one first)
  • in CMS/Pages : select “Home Page”, then switch to the “Content”-tab and enter {{block type=”catalog/product_list” category_id=”21″ template=”catalog/product/list.phtml”}} into the large content-textarea (you might have to adjust the category_id if your newly created category_id differs from the one in this example.

Popularity: 35% [?]

Customizing Magento by overwriting core-classes

There are several possibilities of overwriting single magento-methods or whole classes. Of course you could be just altering the core-classes themselves in a quick and dirty approach. however, you should avoid this practice under all circumstances, because it corrupts the update-facilities of your eCommerce-site. Once you are upgrading your magento-copy to a newer version, all your changes would get lost.

Luckily, overwriting native magento-classes is pretty straight-forward, and therefore there is no reason not to do it the right way.

In this example, we are overwriting the core method of the block-class “Onepage_Billing” of the Mage_Checkout-core module to add another default address. This is just an example to show how you actually add new functionality by inheriting from a Magento-coreclass and overwriting particular methods.

Extend Magento Core-Class

app/code/local/Test/Checkout/Block/Onepage/Billing.php

First we derive from the class that we actually want to overrule. In our example, we are simply overwriting the core Onepage_Billing-class of the Mage_Checkout-core-module with an inherited class called

Test_Checkout_Block_Onepage_Billing

, which declares and therfore overrules the method

getAddressesHtmlSelect

. In this method we can add and/or alter all the functionality that we actually need.

<?php
class Test_Checkout_Block_Onepage_Billing extends Mage_Checkout_Block_Onepage_Billing
{
    public function getAddressesHtmlSelect($type)
    {
        if ($this->isCustomerLoggedIn()) {
            $options = array();
            foreach ($this->getCustomer()->getAddresses() as $address) {
                $options[] = array(
                    'value' => $address->getId(),
                    'label' => $address->format('oneline')
                );
            }

            $addressId = $this->getAddress()->getCustomerAddressId();
            if (empty($addressId)) {
                if ($type=='billing') {
                    $address = $this->getCustomer()->getPrimaryBillingAddress();
                } else {
                    $address = $this->getCustomer()->getPrimaryShippingAddress();
                }
                if ($address) {
                    $addressId = $address->getId();
                }
            }

            $select = $this->getLayout()->createBlock('core/html_select')
                ->setName($type.'_address_id')
                ->setId($type.'-address-select')
                ->setClass('address-select')
                ->setExtraParams('onchange="'.$type.'.newAddress(!this.value)"')
                ->setValue($addressId)
                ->setOptions($options);

            return $select->getHtml();
        }
        return '';
    }
}

Activate Module

app/etc/modules/Test_All.xml

Now it’s time to tell Magento that there is a module (in our example we have named it “Test”) which adds customized functionality to the Magento-core-functionality.

<?xml version="1.0"?>
<config>
    <!-- Please add items in alphabetic order. -->
    <modules>
        <Test_Checkout>
            <active>true</active>
            <codePool>local</codePool>
        </Test_Checkout>
     </modules>
</config>

Actual Overwrite

app/code/local/Test/Checkout/etc/config.xml

Now this is where the Magento-rewrite-magic pops in. By providing a rewrite-directive in the Test_Checkout-config.xml, Magento’s default behaviour can be overruled for certain pages, and we can make sure that our derived class is being loaded instead of the core Checkout-module from Magento.

<?xml version="1.0"?>
<config>
    <modules>
         <Test_Checkout>
            <version>0.1.0</version>
        </Test_Checkout>
    </modules>
    <global>
	    <blocks>
		    <checkout>
		        <rewrite>
		            <onepage_billing>Test_Checkout_Block_Onepage_Billing</onepage_billing>
		        </rewrite>
		    </checkout>
		</blocks>
   </global>
</config>

Clear Cache

Whenever we are changing Magento-settings by adding or modifying the underlying xml-configuration-files, we have to empty the cache (residing at app/var/cache) in order to have the changes come into effect. So before going wild because your changes don’t show, rather empty Magento’s file-cache.

Popularity: 83% [?]

Smarty – Tips

February 10, 2010 Smarty, coding, php Comments

Find some tricks on how to use Smarty effectively here.

Array handling in Smarty

Arrays are a bit tricky to handle in Smarty. But only at first glance! If you know your way around, it´s pretty easy to cope with them. Note that you can use virtually every native php-function by using modifiers, which allow you to use smarty as a smart wrapper for all php-functions:

array_push

let´s say you want to push the value “new” to an existing array named $offers. You can simply do so by using this term:

{$offers|@array_push:"new"}

Note the @-identifier, which makes smarty process the given array as a whole, rather than iterating through each element of the array.

This approach has a disadvantage, though. After having executed array_push, the returning result will be piped to smarty and therefore we have an unwanted output of the function´s result.

We can simply avoid this by following this approach:

{assign var="void" value=$offers|@array_push:"new"}

By doing so, we are assigning the return value from array_push to a variable (named “void”). As a result, the return-value is being stored in a variable rather than leading to an unwanted output of the latter.

in_array

if you want to check whether a the given value “new” exists within a given $offers, try this:

{"new"|in_array:$offers}

Popularity: 29% [?]

Using PHP for shell-scripting

January 30, 2010 coding, php Comments

This class is a first step to a comprehensive, and yet handy little collection of helper-functions, which can ease your way along while using php for shell-scripts.

Whereas most of you probably use php as a scripting-language for websites, it´s also a very good alternative to other shell-scripting languages like perl or python. In fact, I find it a lot more useful than regular shell-scripts.

This little class can be used both as a static collection of shell-methods, but you can also create instances of it.

So far, it automatically parses any userparams coming from the commandline, and stores them in a publicly available array named self::$arguments.

More features are to be added soon :)

<?
	/* ----------------------------------------------------- */
	/* coding: timor kodal [pulsar-network]					 */
	/* copyright by: (c) 2010 timor kodal				     */
	/* ----------------------------------------------------- */
	/* contact: [http://pulsar.cc | timor [at] kodal.de      */
	/* ----------------------------------------------------- */
	/* to see this and other scripts in action, please visit */
	/* http://pulsar.cc										 */
	/* ----------------------------------------------------- */
	/* you are free to use my scripts 					     */
	/* according to the creative commons licence cc-nc-by    */
	/* ----------------------------------------------------- */
	/* more information: http://creativecommons.org          */
	/* ----------------------------------------------------- */

     class shell {
		public $arguments;
		public $defaults;
		public $aliases;
		function __construct (Array $args = NULL,Array $aliases = NULL,Array $defaults = NULL) {
			$this->aliases = $aliases;
			$this->defaults = $defaults;
			$this->arguments = self::parseArguments($args,$aliases,$defaults);
		}
		public function getArgs(Array $defaults = NULL) {
			$args = $this->arguments;
			if ($defaults) {
				foreach ($defaults as $k=>$v) {
					if (!isset($args[$k]))
						$args[$k]=$v;
					if (!isset($args[$this->aliases[$k]]) && $this->aliases[$k])
						$args[$this->aliases[$k]]=$v;
				}
			}
			return $args;
		}
		public static function parseArguments (Array $args = NULL, Array $aliases = NULL,Array $defaults = NULL) {
			global $argv;
			if (!$aliases)
				$aliases = Array ("u"=>"user");
			if (!$args)
				$args=$argv;

			$arguments = Array();
			$count = 0;

			// -u=1
			// -u test
			// --user=1
			if (is_array($defaults)) {
				foreach ($defaults as $k=>$v) {
					$arguments[$k]=$v;
					if ($aliases[$k])
						$arguments[$aliases[$k]]=$v;
				}
			}

			foreach ($args as $k=>$v) {
				$pattern = "/\-{1,2}([a-z]+)=(.*)/";
				preg_match($pattern,$v,$matches);
				if ($matches[0]) {
					$arguments[$matches[1]]=$matches[2];
					if ($aliases[$matches[1]])
						$arguments[$aliases[$matches[1]]]=$matches[2];
				} else
					$arguments[]=$v;
			}
			return $arguments;
		}
	}

?>

download class.shell.php

Example:

<?
 require_once("class.shell.php");
 $aliases = Array("u"=>"uid","d"=>"default");

 $shell = new shell(NULL,$aliases,Array("u"=>"root"));

 print_r($shell->getArgs(Array("d"=>"yes")));

// php5 example.shell.php -user=teamore

// output:

// Array
// (
// [u] => root
// [uid] => root
// [0] => initprocmail.php
// [user] => teamore
// [d] => yes
// [default] => yes
// )

?>

download example.shell.php

Popularity: 22% [?]