Magento snippets

Aus Vosp.info
Wechseln zu:Navigation, Suche

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()


Magento Events

Allgemein Events für den Observer gefunden mit:

grep -r 'dispatchEvent' app/*

Events für den Warenkorb

grep -r 'dispatchEvent' app/* | grep cart
 app/code/core/Mage/XmlConnect/controllers/CartController.php:            Mage::dispatchEvent('checkout_cart_add_product_complete',
 app/code/core/Mage/XmlConnect/controllers/CartController.php:            Mage::dispatchEvent('checkout_cart_update_item_complete', array(
 app/code/core/Mage/Checkout/controllers/CartController.php:            Mage::dispatchEvent('checkout_cart_add_product_complete',
 app/code/core/Mage/Checkout/controllers/CartController.php:            Mage::dispatchEvent('checkout_cart_update_item_complete',
 app/code/core/Mage/Checkout/Model/Cart.php:        Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
 app/code/core/Mage/Checkout/Model/Cart.php:        Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
 app/code/core/Mage/Checkout/Model/Cart.php:        Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
 app/code/core/Mage/Checkout/Model/Cart.php:        Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));
 app/code/core/Mage/Checkout/Model/Cart.php:        Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
 app/code/core/Mage/Checkout/Model/Cart.php:        Mage::dispatchEvent('checkout_cart_product_update_after', array(
 app/code/core/Mage/Paypal/Model/Cart.php:        Mage::dispatchEvent('paypal_prepare_line_items', array('paypal_cart' => $this));


Events für die Bestellung

grep -r 'dispatchEvent' app/* | grep order
app/code/core/Mage/Sales/Model/Convert/Quote.php:        Mage::dispatchEvent('sales_convert_quote_to_order', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Convert/Quote.php:        Mage::dispatchEvent('sales_convert_quote_address_to_order', array('address'=>$address, 'order'=>$order));
app/code/core/Mage/Sales/Model/Convert/Quote.php:        Mage::dispatchEvent('sales_convert_quote_address_to_order_address',
app/code/core/Mage/Sales/Model/Convert/Quote.php:        Mage::dispatchEvent('sales_convert_quote_payment_to_order_payment',
app/code/core/Mage/Sales/Model/Convert/Quote.php:        Mage::dispatchEvent('sales_convert_quote_item_to_order_item',
app/code/core/Mage/Sales/Model/Convert/Order.php:        Mage::dispatchEvent('sales_convert_order_to_quote', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_place_start', array('payment' => $this));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_place_end', array('payment' => $this));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_capture', array('payment' => $this, 'invoice' => $invoice));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_pay', array('payment' => $this, 'invoice' => $invoice));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_cancel_invoice', array('payment' => $this, 'invoice' => $invoice));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_void', array('payment' => $this, 'invoice' => $document));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_refund', array('payment' => $this, 'creditmemo' => $creditmemo));
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_cancel_creditmemo',
app/code/core/Mage/Sales/Model/Order/Payment.php:        Mage::dispatchEvent('sales_order_payment_cancel', array('payment' => $this));
app/code/core/Mage/Sales/Model/Order/Item.php:            Mage::dispatchEvent('sales_order_item_cancel', array('item'=>$this));
app/code/core/Mage/Sales/Model/Order/Invoice.php:        Mage::dispatchEvent('sales_order_invoice_pay', array($this->_eventObject=>$this));
app/code/core/Mage/Sales/Model/Order/Invoice.php:        Mage::dispatchEvent('sales_order_invoice_cancel', array($this->_eventObject=>$this));
app/code/core/Mage/Sales/Model/Order/Invoice.php:        Mage::dispatchEvent('sales_order_invoice_register', array($this->_eventObject=>$this, 'order' => $order));
app/code/core/Mage/Sales/Model/Order/Creditmemo.php:        Mage::dispatchEvent('sales_order_creditmemo_refund', array($this->_eventObject=>$this));
app/code/core/Mage/Sales/Model/Order/Creditmemo.php:        Mage::dispatchEvent('sales_order_creditmemo_cancel', array($this->_eventObject=>$this));
app/code/core/Mage/Sales/Model/Service/Quote.php:        Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Service/Quote.php:        Mage::dispatchEvent('sales_model_service_quote_submit_before', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Service/Quote.php:            Mage::dispatchEvent('sales_model_service_quote_submit_success', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Service/Quote.php:            Mage::dispatchEvent('sales_model_service_quote_submit_failure', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Service/Quote.php:        Mage::dispatchEvent('sales_model_service_quote_submit_after', array('order'=>$order, 'quote'=>$quote));
app/code/core/Mage/Sales/Model/Resource/Order/Collection/Abstract.php:            Mage::dispatchEvent($this->_eventPrefix . '_set_sales_order', array(
app/code/core/Mage/Sales/Model/Order.php:        Mage::dispatchEvent('sales_order_place_before', array('order'=>$this));
app/code/core/Mage/Sales/Model/Order.php:        Mage::dispatchEvent('sales_order_place_after', array('order'=>$this));
app/code/core/Mage/Sales/Model/Order.php:            Mage::dispatchEvent('order_cancel_after', array('order' => $this));
app/code/core/Mage/Adminhtml/Block/Sales/Reorder/Renderer/Action.php:        Mage::dispatchEvent('adminhtml_customer_orders_add_action_renderer', array('renderer' => $this, 'row' => $row));
app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreditmemoController.php:        Mage::dispatchEvent('adminhtml_sales_order_creditmemo_register_before', $args);
app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreateController.php:        Mage::dispatchEvent('adminhtml_sales_order_create_process_data_before', $eventData);
app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreateController.php:        Mage::dispatchEvent('adminhtml_sales_order_create_process_data', $eventData);
app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php:        Mage::dispatchEvent('sales_convert_order_to_quote', array(
app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php:            Mage::dispatchEvent('sales_convert_order_item_to_quote_item', array(
app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php:        Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $quote));
app/code/core/Mage/Checkout/controllers/MultishippingController.php:        Mage::dispatchEvent('checkout_multishipping_controller_success_action', array('order_ids' => $ids));
app/code/core/Mage/Checkout/controllers/OnepageController.php:        Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));
app/code/core/Mage/Checkout/Model/Cart/Api.php:                Mage::dispatchEvent('checkout_type_onepage_save_order_after',
app/code/core/Mage/Checkout/Model/Type/Onepage.php:            Mage::dispatchEvent('checkout_type_onepage_save_order_after',
app/code/core/Mage/Checkout/Model/Type/Multishipping.php:            Mage::dispatchEvent('checkout_submit_all_after', array('orders' => $orders, 'quote' => $this->getQuote()));
app/code/core/Mage/Checkout/Model/Type/Multishipping.php:            Mage::dispatchEvent('checkout_multishipping_refund_all', array('orders' => $orders));
app/code/core/Mage/Rss/Block/Order/New.php:        Mage::dispatchEvent('rss_order_new_collection_select', array('collection' => $collection));
app/code/core/Mage/GoogleCheckout/Model/Api/Xml/Callback.php:        Mage::dispatchEvent('googlecheckout_create_order_before', array('quote' => $quote));
app/code/core/Mage/GoogleCheckout/Model/Api/Xml/Callback.php:        Mage::dispatchEvent('googlecheckout_save_order_after', array('order' => $order));
app/code/core/Mage/GoogleCheckout/Model/Api/Xml/Callback.php:        Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $quote));


File Upload

grep -r 'dispatchEvent' app/* | grep image
app/code/core/Mage/Cms/Helper/Wysiwyg/Images.php:        Mage::dispatchEvent('cms_wysiwyg_images_static_urls_allowed', array(
app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php:        Mage::dispatchEvent('catalog_product_media_save_before', array('product' => $object, 'images' => $value));
app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php:        Mage::dispatchEvent('catalog_product_media_add_image', array('product' => $product, 'image' => $file));
app/code/core/Mage/Adminhtml/controllers/Catalog/Product/GalleryController.php:            Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array(
app/code/core/Mage/Adminhtml/controllers/CacheController.php:            Mage::dispatchEvent('clean_catalog_images_cache_after');


grep -r 'dispatchEvent' app/* | grep media
 app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php:        Mage::dispatchEvent('catalog_product_media_save_before', array('product' => $object, 'images' => $value));
 app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php:        Mage::dispatchEvent('catalog_product_media_add_image', array('product' => $product, 'image' => $file));
 app/code/core/Mage/Adminhtml/controllers/CacheController.php:            Mage::dispatchEvent('clean_media_cache_after');