Magento snippets
Aus Vosp.info
Version vom 20. Oktober 2015, 20:30 Uhr von V (Diskussion | Beiträge)
Inhaltsverzeichnis
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()