Magento snippets: Unterschied zwischen den Versionen

Aus Vosp.info
Wechseln zu:Navigation, Suche
(Die Seite wurde neu angelegt: „ == Allgemein == === Logging === <source lang="php" line=1> Mage::log('Template options.phtml'); </source> === Get Ids (Product Customer etc in adminhtml)=== <s…“)
 
Zeile 1: Zeile 1:
 
 
== Allgemein ==
 
== Allgemein ==
 
=== Logging ===
 
=== Logging ===
Zeile 34: Zeile 33:
 
   */
 
   */
 
<?php  echo $this->getChildHtml('tab_testprices_list'); ?>
 
<?php  echo $this->getChildHtml('tab_testprices_list'); ?>
 +
</source>
 +
 +
 +
 +
 +
 +
 +
== Models ==
 +
 +
=== Standard Magento ===
 +
==== Produkte ====
 +
===== Aktuelles Produkt =====
 +
  <source lang ="php">
 +
$current_product = Mage::getModel('catalog/product')->load();
 +
</source>
 +
===== Neues Produkt speichern =====
 +
 +
  <source lang ="php">
 +
//create a new product
 +
try {
 +
  $newProduct = Mage::getModel('catalog/product')
 +
    ->setStoreId('default')
 +
    ->setCategoryIds('4')
 +
    ->setAttributeSetId(41)
 +
    ->setPrice(15.49)
 +
    ->setSku('4you')
 +
    ->setName('4You T Shirt')
 +
    ->setManufacturer(20)
 +
    ->setTypeId('simple')
 +
    ->save();
 +
   
 +
  echo 'OK Product ID: '.$newProduct->getId();
 +
}
 +
catch (Mage_Core_Exception $e) {
 +
  echo $e->getMessage();
 +
}
 +
catch (Exception $e) {
 +
  echo $e;
 +
}
 +
</source>
 +
 +
 +
 +
==== Attribute ====
 +
$model = Mage::getModel('catalog/resource_eav_attribute');
 +
==== Attribute Sets ====
 +
  <source lang ="php">
 +
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
 +
                ->setCodeFilter($param_attrCode)
 +
                ->getFirstItem();
 +
 +
// Add attribute to attriuteset
 +
$model = Mage::getModel('eav/entity_setup', 'core_setup');
 +
$attributeSetId = Mage::getModel('catalog/product')->load($param_productId)->getAttributeSetId();
 +
$attributeGroupId = $model->getAttributeGroupId('catalog_product', $attributeSetId, 'General');
 +
$model->addAttributeToSet('catalog_product', $attributeSetId, $attributeGroupId, $param_attribute_id);
 +
</source>
 +
 +
==== Warenkorb ====
 +
<source lang ="php">
 +
$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
 +
</source>
 +
 +
==== Warenkorb ====
 +
<source lang ="php">
 +
$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
 +
</source>
 +
 +
=== ANC Models ===
 +
 +
====Anc Dataimport====
 +
<source lang ="php">
 +
$address = Mage::getModel('anc_dataimport_addresslist/entity');
 +
</source>
 +
==== Anc Pricespercustomer ====
 +
<source lang ="php">
 +
 +
$collection = Mage::getModel('anc_pricespercustomer/entity')
 +
            ->getCollection()
 +
            ->addFieldToFilter('main_table.customer_id', $this->getRequest()->getParam('id'));
 +
$this->setCollection($collection);
 +
 +
 +
//Daten speichern:
 +
 +
$id = $this->getRequest()->getParam('id', null);
 +
        $model = Mage::getModel('anc_pricespercustomer/entity');
 +
        if ($id) {
 +
            $model->load((int) $id);
 +
            if ($model->getId()) {
 +
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
 +
                if ($data) {
 +
                    $model->setData($data);
 +
                }
 +
            }else {
 +
                    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricespercustomer')->__('Item Does niot exist'));
 +
                    $this->_redirect('*/*/');
 +
                }
 +
        }
 +
 +
 +
</source>
 +
 +
== Registry ==
 +
=== Standard Magento===
 +
==== Current Customer ====
 +
Mage::registry('current_customer')->getId();
 +
 +
 +
 +
== Magento Arbeiten mit der Datenbank ==
 +
=== Collections ===
 +
<source lang="php">
 +
//Beispiel 1
 +
$collection = Mage::getModel('catalog/product')->getCollection();
 +
$collection->addAttributeToSelect('name');
 +
$collection->setOrder('name','asc');
 +
$collection->load();
 +
//Beispiel  2
 +
$category =Mage::getModel('catalog/category')->load(5);
 +
$productCollection = $category->getProductCollection();
 +
// Beispiel 3 mit Join (Get Bestseller Produkts:
 +
// Join Version 1
 +
$category =Mage::getModel('catalog/category')->load(5);
 +
$productCollection = $category->getProductCollection();
 +
productCollection->getSelect()
 +
                  ->join(array('o'='sales_flat_order_item'),'main_table.entity_id = o.product_id', array('o.row_total','o.product_id'))->group(array('sku'));
 +
 +
// Join Version 2:
 +
$category = Mage::getModel('catalog/category')->load(5);
 +
$productCollection = $category->getProductCollection();
 +
productCollection->joinField('o'='sales_flat_order_item', array('o.row_total','o.product_id'),'main_table.entity_id = o.product_id')->group(array('sku'));
 +
 +
 +
// Select Statement Anzeigen:
 +
echo $collection->getSelect()->__toString();
 +
 +
// Für nicht EAV
 +
addFieldToFilter('name','Horst')
 +
 +
//Für EAV
 +
addAttributeToSelect('name');
 +
addAtributeToFilter('name','Horst')
 +
//oder
 +
addAtributeToFilter('small_image',array('notnull',=>'','neq'=>'no_selction'))
 +
addAtributeToSort()
 +
 +
 +
 +
 +
 
</source>
 
</source>

Version vom 20. Oktober 2015, 20:30 Uhr

Allgemein

Logging

1  Mage::log('Template options.phtml');

Get Ids (Product Customer etc in adminhtml)

1 <?php $_product = $this->getProduct(); ?>
2 <?php echo $_product->getId() ?>


Layouts

Templates

/app/design/adminhtml/default/default/template/?/?/?.phtml

Url mit Extension Key

1  Mage::helper("adminhtml")->getUrl("adminhtml/testprices/new")
Ausgabe:
http://magento.lc/index.php/admin/testprices/new/key/bfbfb5f5b38ae2369151e2fcf431e486/


Tabelle Ausgeben

1  /**
2   * im Block Ordner  
3   * liegt in diesem Fall unter 
4   *  /app/code/local/Anc/Testprices/Customer/Edit/Tab/Testprices/List.php
5   */
6 <?php  echo $this->getChildHtml('tab_testprices_list'); ?>




Models

Standard Magento

Produkte

Aktuelles Produkt
$current_product = Mage::getModel('catalog/product')->load();
Neues Produkt speichern
//create a new product
try {
  $newProduct = Mage::getModel('catalog/product')
    ->setStoreId('default')
    ->setCategoryIds('4')
    ->setAttributeSetId(41)
    ->setPrice(15.49)
    ->setSku('4you')
    ->setName('4You T Shirt')
    ->setManufacturer(20)
    ->setTypeId('simple')
    ->save(); 
    
  echo 'OK Product ID: '.$newProduct->getId();
}
catch (Mage_Core_Exception $e) {
  echo $e->getMessage();
}
catch (Exception $e) {
  echo $e;
}


Attribute

$model = Mage::getModel('catalog/resource_eav_attribute');

Attribute Sets

$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
                ->setCodeFilter($param_attrCode)
                ->getFirstItem();

// Add attribute to attriuteset
$model = Mage::getModel('eav/entity_setup', 'core_setup');
$attributeSetId = Mage::getModel('catalog/product')->load($param_productId)->getAttributeSetId();
$attributeGroupId = $model->getAttributeGroupId('catalog_product', $attributeSetId, 'General');
$model->addAttributeToSet('catalog_product', $attributeSetId, $attributeGroupId, $param_attribute_id);

Warenkorb

$cart = Mage::getModel('checkout/cart')->getQuote()->getData();

Warenkorb

$cart = Mage::getModel('checkout/cart')->getQuote()->getData();

ANC Models

Anc Dataimport

 $address = Mage::getModel('anc_dataimport_addresslist/entity');

Anc Pricespercustomer

 
 $collection = Mage::getModel('anc_pricespercustomer/entity')
            ->getCollection()
            ->addFieldToFilter('main_table.customer_id', $this->getRequest()->getParam('id'));
 $this->setCollection($collection);


//Daten speichern:

$id = $this->getRequest()->getParam('id', null);
        $model = Mage::getModel('anc_pricespercustomer/entity');
        if ($id) {
            $model->load((int) $id);
            if ($model->getId()) {
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
                if ($data) {
                    $model->setData($data);
                } 
            }else {
                    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricespercustomer')->__('Item Does niot exist'));
                    $this->_redirect('*/*/');
                }
        }

Registry

Standard Magento

Current Customer

Mage::registry('current_customer')->getId();


Magento Arbeiten mit der Datenbank

Collections

//Beispiel 1
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->setOrder('name','asc');
$collection->load();
//Beispiel  2
$category =Mage::getModel('catalog/category')->load(5);
$productCollection = $category->getProductCollection();
// Beispiel 3 mit Join (Get Bestseller Produkts:
// Join Version 1
$category =Mage::getModel('catalog/category')->load(5);
$productCollection = $category->getProductCollection();
productCollection->getSelect()
                  ->join(array('o'='sales_flat_order_item'),'main_table.entity_id = o.product_id', array('o.row_total','o.product_id'))->group(array('sku'));

// Join Version 2:
$category = Mage::getModel('catalog/category')->load(5);
$productCollection = $category->getProductCollection();
productCollection->joinField('o'='sales_flat_order_item', array('o.row_total','o.product_id'),'main_table.entity_id = o.product_id')->group(array('sku'));


// Select Statement Anzeigen:
echo $collection->getSelect()->__toString();

// Für nicht EAV 
addFieldToFilter('name','Horst')

//Für EAV
addAttributeToSelect('name');
addAtributeToFilter('name','Horst')
//oder
addAtributeToFilter('small_image',array('notnull',=>'','neq'=>'no_selction'))
addAtributeToSort()