magento

[Magento] check if prodcut is new

here is the code :) (à mettre dans un helper de module)

public function isProductNew($product) {
        $today = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        $new_from = $product->getData('news_from_date');
        $new_to   = $product->getData('news_to_date');

        return ($new_from && $new_from <= $today && (!$new_to || $new_to >= $today));
}

Exemple d'appel dans le template :

if($this->helper('MONMODULE')->isProductNew($_product)):
    echo 'Nouveauté';
endif;

[Magento] faire une redirection dans un block (ou modèle)

pour faire une redirection dans un controller, rien de plus simple :

$this->_redirect('*/*/*');
return;

mais pour faire de même dans un modèle ou block, c'est comme ça :

Mage::app()->getResponse()->setRedirect(Mage::getUrl('*/*/*'));

[Magento] aide mémoire, les conditions possibles dans addAttributeToFilter

protected function _getConditionSql($fieldName, $condition) {
        if (is_array($fieldName)) {
            foreach ($fieldName as $f) {
                $orSql = array();
                foreach ($condition as $orCondition) {
                    $orSql[] = "(".$this->_getConditionSql($f[0], $f[1]).")";
                }
                $sql = "(".join(" or ", $orSql).")";
            }
            return $sql;
        }

        $sql = '';
        $fieldName = $this->_getConditionFieldName($fieldName);
        if (is_array($condition) && isset($condition['field_expr'])) {
            $fieldName = str_replace('#?', $this->getConnection()->quoteIdentifier($fieldName), $condition['field_expr']);
        }
        if (is_array($condition)) {
            if (isset($condition['from']) || isset($condition['to'])) {
                if (isset($condition['from'])) {
                    if (empty($condition['date'])) {
                        if ( empty($condition['datetime'])) {
                            $from = $condition['from'];
                        }
                        else {
                            $from = $this->getConnection()->convertDateTime($condition['from']);
                        }
                    }
                    else {
                        $from = $this->getConnection()->convertDate($condition['from']);
                    }
                    $sql.= $this->getConnection()->quoteInto("$fieldName >= ?", $from);
                }
                if (isset($condition['to'])) {
                    $sql.= empty($sql) ? '' : ' and ';

                    if (empty($condition['date'])) {
                        if ( empty($condition['datetime'])) {
                            $to = $condition['to'];
                        }
                        else {
                            $to = $this->getConnection()->convertDateTime($condition['to']);
                        }
                    }
                    else {
                        $to = $this->getConnection()->convertDate($condition['to']);
                    }

                    $sql.= $this->getConnection()->quoteInto("$fieldName <= ?", $to);
                }
            }
            elseif (isset($condition['eq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName = ?", $condition['eq']);
            }
            elseif (isset($condition['neq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName != ?", $condition['neq']);
            }
            elseif (isset($condition['like'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName like ?", $condition['like']);
            }
            elseif (isset($condition['nlike'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName not like ?", $condition['nlike']);
            }
            elseif (isset($condition['in'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName in (?)", $condition['in']);
            }
            elseif (isset($condition['nin'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName not in (?)", $condition['nin']);
            }
            elseif (isset($condition['is'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName is ?", $condition['is']);
            }
            elseif (isset($condition['notnull'])) {
                $sql = "$fieldName is NOT NULL";
            }
            elseif (isset($condition['null'])) {
                $sql = "$fieldName is NULL";
            }
            elseif (isset($condition['moreq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName >= ?", $condition['moreq']);
            }
            elseif (isset($condition['gt'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName > ?", $condition['gt']);
            }
            elseif (isset($condition['lt'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName < ?", $condition['lt']);
            }
            elseif (isset($condition['gteq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName >= ?", $condition['gteq']);
            }
            elseif (isset($condition['lteq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName <= ?", $condition['lteq']);
            }
            elseif (isset($condition['finset'])) {
                $sql = $this->getConnection()->quoteInto("find_in_set(?,$fieldName)", $condition['finset']);
            }
            else {
                $orSql = array();
                foreach ($condition as $orCondition) {
                    $orSql[] = "(".$this->_getConditionSql($fieldName, $orCondition).")";
                }
                $sql = "(".join(" or ", $orSql).")";
            }
        } else {
            $sql = $this->getConnection()->quoteInto("$fieldName = ?", (string)$condition);
        }
        return $sql;
    }

ces conditions sont aussi disponibles dans la documentation magento ici : http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/...

[Magento] faire des requêtes sql dans le code

il est parfois intéressant de faire des requêtes sql dans le code sans utiliser tout la couche d'abstraction de Magento

$conn = Mage::getSingleton('core/resource')->getConnection('core_read');

$results = $conn->fetchAll("SELECT order_id, count(sku) AS nb FROM sales_flat_order_item GROUP BY order_id ORDER BY nb DESC;");


foreach($results as $row) {
    echo $row['order_id'] . " : " . $row['nb'] . "\n";
}

[Magento] url d'une action dans le tpl

$url = $this->getUrl('MODULE/CONTROLLER/ACTION');

Pages

Subscribe to RSS - magento