Magento 2 Entwicklung: Unterschied zwischen den Versionen

Aus Vosp.freesn.de
Zur Navigation springen Zur Suche springen
V (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
V (Diskussion | Beiträge)
 
(19 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
== Code Snippets Magento2 ==
== Code Snippets Magento2 ==
=== .phtml get arguments Xml/Cms===  
=== phtml get arguments Xml/Cms===  
 
'''CMS Page => Design Layout XML'''
<source lang=xml>
<source lang=xml>
<referenceContainer name="content">
<referenceContainer name="content">
     <block class="Anc\Apname\Block\Blockname" name="ancname" as="ancname" template="Magento_Theme::anc//ancname.phtml" >
     <block class="Anc\Apname\Block\Blockname" name="ancname" as="ancname" template="Magento_Theme::anc//ancname.phtml" >
            <arguments>
      <arguments>
                <argument name="ancspec" xsi:type="array">
            <argument name="ancspec" xsi:type="array">
                        <item name="ancFnc" xsi:type="array">
                <item name="ancFnc" xsi:type="array">
                            <item name="product" xsi:type="string">5</item>
                      <item name="product" xsi:type="string">5</item>
                        </item>
                </item>
                </argument>
            </argument>
            </arguments>
      </arguments>
        </block>
    </block>
</referenceContainer>
</referenceContainer>
</source>
'''Phtml File'''
<source lang=php>
$argsFromXml=$block->getData('ancspec') ;
</source>


== Code Migration Snippets 1.x => 2.x ==
=== Text Ausgabe ===
==== 1.x ====
<source lang=php>
echo $this->__('Outputtext');
</source>
</source>
==== 2.x ====
<source lang=php>
<source lang=php>
$argsFromXml=$block->getData('ancspec') ;
echo __('Outputtext');
</source>
</source>


== Code Migration Snippets 1.x => 2.x ==
=== Bilder ===
=== Bilder ===
==== 1.x ====
==== 1.x ====
<source lang=php>
<source lang=php>
<img src="<?php echo $this->getSkinUrl('images/product/dir/name.jpg'); ?>" alt="<?php echo $this->__('Name'); ?>"
<img src="<?php echo $this->getSkinUrl('images/product/dir/name.jpg'); ?>" alt="<?php echo $this->__('Name'); ?>"
</source>
<source lang=xml>
{{skin url='images/product/dir/name.jpg'}}
</source>
</source>
==== 2.x ====
==== 2.x ====
Zeile 30: Zeile 44:
<img src="<?php echo $this->getViewFileUrl('images/product/dir/name.jpg'); ?>" alt="<?php echo __('Name'); ?>" />
<img src="<?php echo $this->getViewFileUrl('images/product/dir/name.jpg'); ?>" alt="<?php echo __('Name'); ?>" />
</source>
</source>
<source lang=xml>
{{view url='images/product/dir/name.jpg'}}
</source>
=== Url ===
==== 1.x ====
<source lang=php>
<a href="<?php echo Mage::getUrl('produkte'); ?>"  ?>"
</source>
==== 2.x ====
<source lang=php>
<a href="<?php echo $this->getUrl('produkte'); ?>"  ?>" />
</source>
=== Helper ===
=== Helper ===
==== 1.x ====
==== 1.x ====
Zeile 38: Zeile 67:
<source lang=php>
<source lang=php>
$this->helper('Anc\Appname\Helper\Anchelper');
$this->helper('Anc\Appname\Helper\Anchelper');
</source>
=== Currency ===
==== 1.x ====
<source lang=php>
Mage::helper('core')->currency($_product->getPrice(),true,true)
</source>
==== 2.x ====
<source lang=php>
$this->helper('Magento\Framework\Pricing\Helper\Data')->currency($_product->getPrice(),true,false);
</source>
=== get === 
==== * current====
<source lang=php>
class Data extends \Magento\Framework\App\Helper\AbstractHelper
public function __construct(
  \Magento\Framework\Registry $registry
)
{
  $this->_registry = $registry;
}
</source>
===== ** Produkt =====
<source lang=php>
return $this->_registry->registry('current_product');
</source>
===== ** Category =====
<source lang=php>
return $this->_registry->registry('current_category');
</source>
==== * product by ====
<source lang=php>
class Data extends \Magento\Framework\App\Helper\AbstractHelper
....
public function __construct(
  \Magento\Catalog\Model\ProductRepository $productRepository,
)
{
  $this->_productRepository = $productRepository;
}
</source>
===== ** sku =====
<source lang=php>
return $this->_productRepository->get($sku);
</source>
===== ** id =====
<source lang=php>
  return $this->_productRepository->getById($id);
</source>
=== Customized configurable product ===
==== Preis wird nicht mehr bei Wechsel aktualisiert ====
<source lang=php>
$templatestring ="<div class="priceReferenceValue price-box"  data-role="priceBox"  data-product-id="' . $_product->getId() . '" ><span class="value"> {BASE_PRICE}</span></div>";
</source>
* app/design/frontend/Anc/ancdesign/Magento_ConfigurableProduct/web/js/configurable.js
<source lang=js>
_calculatePrice: function (config) {
            var displayPrices = $(this.options.priceHolderSelector).priceBox('option').prices,
                newPrices = this.options.spConfig.optionPrices[_.first(config.allowedProducts)];
            _.each(displayPrices, function (price, code) {
                if (newPrices[code]) {
                    displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
                }
            });
            /**
            * neuen Preis setzen
            */
    $(".priceReferenceValue .value .price").html(newPrices.finalPrice.amount.replace(/\./g, ',') + '&nbsp;&euro;');
            return displayPrices;
        },
</source>
=== Ajax Call ===
==== * in phtml ====
<source lang=js>
<script type="text/javascript">
require(['jquery', 'jquery/ui'], function($){
$('#tool-calc').bind('submit', function(e) {
e.preventDefault();
var form = $('#tool-calc');
var data = form.serialize();
$('#ancajax').css({'display':'block'});
$.post('<?php echo $this->getUrl('anctools/tools/calctool/') ?>', data, function(response) {
                              $('#ancToolReplace').replaceWith(response);
                        });
                  return false;
      });
});
   
</script>
</source>
==== * controller ====
<source lang=php>
use Magento\Framework\Controller\ResultFactory;
class Anctools extends Action
public function __construct(
Context $context,
) {
parent::__construct($context);
}
public function execute()
    {
$string = '<div>test</div>'
$this->resultFactory = $this->context->getResultFactory();
$result = $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
return $result->setHeader('Content-Type','text/html')->setContents($string);;
}
</source>
</source>

Aktuelle Version vom 19. September 2017, 17:31 Uhr

Code Snippets Magento2

phtml get arguments Xml/Cms

CMS Page => Design Layout XML

 <referenceContainer name="content">
    <block class="Anc\Apname\Block\Blockname" name="ancname" as="ancname" template="Magento_Theme::anc//ancname.phtml" >
       <arguments>
            <argument name="ancspec" xsi:type="array">
                 <item name="ancFnc" xsi:type="array">
                      <item name="product" xsi:type="string">5</item>
                 </item>
            </argument>
       </arguments>
    </block>
 </referenceContainer>

Phtml File

 $argsFromXml=$block->getData('ancspec') ;

Code Migration Snippets 1.x => 2.x

Text Ausgabe

1.x

 echo $this->__('Outputtext');

2.x

 echo __('Outputtext');

Bilder

1.x

<img src="<?php echo $this->getSkinUrl('images/product/dir/name.jpg'); ?>" alt="<?php echo $this->__('Name'); ?>"
{{skin url='images/product/dir/name.jpg'}}

2.x

<img src="<?php echo $this->getViewFileUrl('images/product/dir/name.jpg'); ?>" alt="<?php echo __('Name'); ?>" />
{{view url='images/product/dir/name.jpg'}}

Url

1.x

<a href="<?php echo Mage::getUrl('produkte'); ?>"  ?>"

2.x

<a href="<?php echo $this->getUrl('produkte'); ?>"  ?>" />

Helper

1.x

Mage::helper('anchelper')->ancfnc();

2.x

$this->helper('Anc\Appname\Helper\Anchelper');

Currency

1.x

Mage::helper('core')->currency($_product->getPrice(),true,true)

2.x

$this->helper('Magento\Framework\Pricing\Helper\Data')->currency($_product->getPrice(),true,false);


get

* current

class Data extends \Magento\Framework\App\Helper\AbstractHelper

public function __construct(
  \Magento\Framework\Registry $registry
)
{
   $this->_registry = $registry;
}
** Produkt
 return $this->_registry->registry('current_product');
** Category
return $this->_registry->registry('current_category');


* product by

class Data extends \Magento\Framework\App\Helper\AbstractHelper
....
public function __construct(
   \Magento\Catalog\Model\ProductRepository $productRepository,
)
{
   $this->_productRepository = $productRepository;
}
** sku
return $this->_productRepository->get($sku);
** id
  return $this->_productRepository->getById($id);

Customized configurable product

Preis wird nicht mehr bei Wechsel aktualisiert

$templatestring ="<div class="priceReferenceValue price-box"  data-role="priceBox"  data-product-id="' . $_product->getId() . '" ><span class="value"> {BASE_PRICE}</span></div>";
  • app/design/frontend/Anc/ancdesign/Magento_ConfigurableProduct/web/js/configurable.js
_calculatePrice: function (config) {
            var displayPrices = $(this.options.priceHolderSelector).priceBox('option').prices,
                newPrices = this.options.spConfig.optionPrices[_.first(config.allowedProducts)];

            _.each(displayPrices, function (price, code) {
                if (newPrices[code]) {
                    displayPrices[code].amount = newPrices[code].amount - displayPrices[code].amount;
                }
            });
            /**
            * neuen Preis setzen
            */
	    $(".priceReferenceValue .value .price").html(newPrices.finalPrice.amount.replace(/\./g, ',') + '&nbsp;&euro;');
            return displayPrices;
        },


Ajax Call

* in phtml

<script type="text/javascript">
require(['jquery', 'jquery/ui'], function($){ 
$('#tool-calc').bind('submit', function(e) {
			e.preventDefault();
			var form = $('#tool-calc');
			var data = form.serialize();
			$('#ancajax').css({'display':'block'});
			$.post('<?php echo $this->getUrl('anctools/tools/calctool/') ?>', data, function(response) {
                               $('#ancToolReplace').replaceWith(response);
                        });
                   return false;
      }); 
});
    
</script>

* controller

use Magento\Framework\Controller\ResultFactory;
class Anctools extends Action 


	public function __construct(
			Context $context,
) {
parent::__construct($context);
	}


public function execute()
    {
$string = '<div>test</div>'
$this->resultFactory = $this->context->getResultFactory();
$result = $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
return $result->setHeader('Content-Type','text/html')->setContents($string);;
}