Skip to content

Instantly share code, notes, and snippets.

@Clayder
Last active December 27, 2018 16:31
Show Gist options
  • Select an option

  • Save Clayder/6dff3776a98871505ec175ba9137732b to your computer and use it in GitHub Desktop.

Select an option

Save Clayder/6dff3776a98871505ec175ba9137732b to your computer and use it in GitHub Desktop.
Magento

Minhas anotações sobre o magento.

  1. Criar a estrutura de pastas do módulo, em app/code/local/Empresa/Observacaopedido
    • Block
    • controllers
    • etc
    • Helper
    • Model
    • sql
  1. Dentro da pasta controllers, criar um outro diretório, chamado Adminhtml.

  2. Dentro de Adminhtml é onde vai ficar armazenado os controladores do módulo: ex: Criar classe ObservacaopedidoController.php

    <?php 
     class Supercommerce_Observacaopedido_Adminhtml_ObservacaopedidoController Extends Mage_Adminhtml_Controller_Action{
      // Acessar via url: http:/seusite/admin/observacaoPedido/index
      public function indexAction(){
          echo "Teste Peter";
      }
    }
    
  1. /app/design/adminhtml/default/default/template/sales/order/view/obs.phtml

    • Nesse arquivo que será criado o layout do bloco.
  2. Para esse bloco ser exibido no painel do pedido, ele tem que ser registrado, para realizar o registro, tem que acessar o arquivo: app/design/adminhtml/default/default/layout/sales.xml

    • <block type="adminhtml/sales_order_view_obs" name="order_obs" template="sales/order/view/obs.phtml"></block>
  3. Para finalizar a exibição do bloco, tem que utilizar o método

    Obs: Ainda falta finalizar ( parei no commit 1ebe4fc)

  1. Configurar o XML

    <?xml version="1.0"?>
      <config>
         <modules>
         <Supercommerce_Observacaopedido>
             <version>0.0.1</version>
         </Supercommerce_Observacaopedido>
     </modules>
     <global>
     	<resources>
     		<observacaopedido_write>
     			<connection>
     				<use>core_write</use>
     			</connection>
     		</observacaopedido_write>
     		<observacaopedido_read>
     			<connection>
     				<use>core_read</use>
     			</connection>
     		</observacaopedido_read>
             <observacaopedido_setup>
     		  <setup>
                     <module>Supercommerce_Observacaopedido</module>
                     <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
             </observacaopedido_setup>
         </resources>
     </global>
     <admin>
         <routers>
             <adminhtml>
                 <args>
                     <modules>
                         <supercommerce_observacaopedido before="Mage_Adminhtml">Supercommerce_Observacaopedido_Adminhtml</supercommerce_observacaopedido>
                     </modules>
                 </args>
             </adminhtml>
         </routers>
     </admin> 
    </config>
    
  2. Criar o arquivo mysql4-install-0.0.1.php dentro da pasta sql do módulo. Esse script irá criar um campo (observacaopedido do tipo text) na tabela sales_flat_order.

    $installer  = $this;
    $connection = $installer->getConnection();
    
     $installer->startSetup();
    
     $installer->getConnection()
     	->addColumn($installer->getTable('sales/order'),
     		'observacaopedido', 
     		array( 	
     			'type' 		=> Varien_Db_Ddl_Table::TYPE_TEXT, null,
     			'default' 	=> NULL,
     			'after' 	=> '',
     			'comment' 	=> 'observacaopedido',
     		)
     	);
     $installer->endSetup();
    

Debugar código

Zend_Debug::dump($array);

Debug no back-end (Exibir o caminho de cada bloco)

Acessar a classe: app\code\core\Mage\Core\Block\Template.php e mudar o método:

 public  function  getShowTemplateHints()
{
    return true;
}

Encontrar os XMLs que o controller está carregando

Utilizar o método $this->getLayout()->getUpdate()->getHandles() no método do controlador

ex:

public function indexAction()
{
        $this->loadLayout();
        $this->renderLayout();
        Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
}
  • Retornar id do cliente através da compra

     <?php $orderT = Mage::getModel('sales/order')->load(62788); ?>
     <?php Zend_Debug::dump($orderT->getCustomerId()); ?>
    
  • Passando variáveis para um bloco filho html em Magento

    • No arquivo que será carregado o bloco filho.

      // Carrega o valor para que será exibido no bloco filho
      $this->getChild("nomeBlocoFilho")->setData("var", $valor);
      
      // exibe o bloco filho products
      echo $this->getChildHtml("nomeBlocoFilho");  
      
    • No arquivo do bloco filho (nomeBlocoFilho)

      // $_product recebe o valor passado. 
      $_product = $this->getVar();
      
  • Enviar produto para o carrinho via formulário

     	<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>" method="post">
     		  <?php $formKey = Mage::getSingleton('core/session')->getFormKey();?>
     	    <input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />
     	    <input type="hidden" name="product" value="<?php echo $_product->getId(); ?>" />
     	    <input type="hidden" name="qty" value="<?php echo ($_price['price_qty'] - 1);?>" />
     	    <button type="submit" ></button>
     	</form>
    
  • Recuperar o ID de uma loja

    $storeId =  Mage::app()->getStore()->getId();
    
  • Verificar se uma requisição é do tipo post

    $this->getRequest()->isPost()
    
  • Forçar redirecionamento para página não encontrada

    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
    $this->getResponse()->setHeader('Status','404 File not found');
    $this->_forward('defaultNoRoute');
    
  1. Retornar dados dos clientes

    $collections =  Mage::getResourceModel('customer/customer_collection')
    
     ->addNameToSelect()
    
     ->addAttributeToSelect('email')
    
     ->addAttributeToSelect('created_at')
    
     ->addAttributeToSelect('group_id')
     
     ->addAttributeToSelect('tipopessoa', 'left')
             
     ->addAttributeToSelect('empresa', 'left')
    
     ->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left')
    
     ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
    
     ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
    
     ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
    
     ->joinAttribute('billing_fax', 'customer_address/fax', 'default_billing', null, 'left')
    
     ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
    
     ->joinAttribute('billing_country_code', 'customer_address/country_id', 'default_billing', null, 'left')
    
     ->joinAttribute('shipping_street', 'customer_address/street', 'default_shipping', null, 'left')
    
     ->joinAttribute('shipping_postcode', 'customer_address/postcode', 'default_shipping', null, 'left')
    
     ->joinAttribute('shipping_city', 'customer_address/city', 'default_shipping', null, 'left')
    
     ->joinAttribute('shipping_telephone', 'customer_address/telephone', 'default_shipping', null, 'left')
    
     ->joinAttribute('shipping_fax', 'customer_address/fax', 'default_shipping', null, 'left')
    
     ->joinAttribute('shipping_region', 'customer_address/region', 'default_shipping', null, 'left')
    
     ->joinAttribute('shipping_country_code', 'customer_address/country_id', 'default_shipping', null, 'left')
    
     ->joinAttribute('taxvat', 'customer/taxvat', 'entity_id', null, 'left');
    
    foreach($collections as $collection){
    
        $result[] = $collection->getData();
    
    }
    
    echo  "<pre>";
    
        print_r($result);
    
    echo  "</pre>";
    
## Atualizar todos os e-mails da loja
UPDATE peter.customer_entity SET email = CONCAT("peter_test-", email) where entity_id >= 1
UPDATE peter.sales_flat_order SET customer_email = CONCAT("peter_test-", customer_email) where entity_id >= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment