Object

/** * @param $className * @return mixed */public function getObject($className){    return \Magento\Framework\App\ObjectManager::getInstance()->get($className);}/** * @param $className * @return mixed */public function createObject($className){    return \Magento\Framework\App\ObjectManager::getInstance()->create($className);}

Url

/** * Generate url by route and parameters * * @param   string $route * @param   array $params * @return  string */public function getUrl($route = '', $params = []){    return $this->urlManager->getUrl($route, $params);}/** * @return string */public function getRefer(){    return $this->urlEncoder->encode($this->urlManager->getCurrentUrl());}/** * @return string */public function decodeUrl($refer){    return $this->urlDecoder->decode($refer);}注:urlManager —> \Magento\Framework\UrlInterfaceurlEncoder -> \Magento\Framework\Url\EncoderInterfaceurlDecoder -> \Magento\Framework\Url\DecoderInterface

Product object

$product = \Magento\Framework\App\ObjectManager::getInstance()->create('Magento\Catalog\Model\Product')->load($productId); // by id$product = \Magento\Framework\App\ObjectManager::getInstance()->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($sku); // by sku

Product custom attribute and attribute label

// attribute$product = \Magento\Framework\App\ObjectManager::getInstance()->create('Magento\Catalog\Model\Product')->load($productId);$attribute = $product->getData($attributeName);// attribute label$attributeLabel = $product->getAttributeText($attributeName);// or$attributeLabel = $product->getResource()->getAttribute($attributeName)->getFrontend()->getValue($product);

Categories

$objectManger = \Magento\Framework\App\ObjectManager::getInstance();$categoryHelper = $objectManger->create('Magento\Catalog\Helper\Category');$categories = $categoryHelper->getStoreCategories($sorted = false, $asCollection = false, $toLoad = true);$menus = array();foreach ($categories as $category){    $sub = array();    $sub['name'] = $category->getName();    $sub['link'] = $categoryHelper->getCategoryUrl($category);    $menus[] = $sub;}

Category custom attribute

$objectManger = \Magento\Framework\App\ObjectManager::getInstance();$categoryModel = $objectManger->get('Magento\Catalog\Model\Category')->load($categoryId);$attribute = $categoryModel->getData('attribute_name');

Logo and Copyright

// Logo$objectManger = \Magento\Framework\App\ObjectManager::getInstance();$logoObject = $objectManger->create('Magento\Theme\Block\Html\Header\Logo');$logo = $logoObject->getLogoSrc();// Copyright$footer = $objectManger->create('Magento\Theme\Block\Html\Footer');$copyright = $footer->getCopyright();

Format price

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');$price = 998;$formattedPrice = $priceHelper->currency($price, true, false); // eg:EGP998.00注: 已convert价格$priceHelper = $objectManager->get('Magento\Framework\Pricing\PriceCurrencyInterface');$formatPrice = $priceHelper->format($price);注: 未convert价格

Resize product image

/** * Resize product image * * @param $imageFile * @param $size * @return mixed */public function resizeProductImage($imageFile, $size){    $imageObj = $objectManager->get("Magento\Catalog\Model\Product\Image");    $imageObj->setDestinationSubdir("image");    $imageObj->setSize($size);    $imageObj->setBaseFile($imageFile);    if (!$imageObj->isCached()) {        $imageObj->resize();        $imageObj->saveFile();    }    return $imageObj->getUrl();}

Customer session

/** * Return customer session model * * @return mixed */public function getCustomerSession(){    return \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Customer\Model\Session');}

Get params

$params = $this->_request->getParams();$param = $this->_request->getParam($key);

用户验证

$authentication = $objectManager->get("Magento\Customer\Model\Authentication");$customerId = $this->getCustomerSession()->getCustomer()->getId();$currentPassword = $this->_request->getParam('current_password');$checkResult = $authentication->authenticate($customerId, $currentPassword); // 返回值为 ture/false

自定义前缀 (Custom prefix of order/invoice/shipment/creditmemo)

UPDATE sales_sequence_profile SET prefix = 'order-' WHERE meta_id in (SELECT meta_id FROM sales_sequence_meta WHERE entity_type='order' AND store_id !=0);UPDATE sales_sequence_profile SET prefix = 'invoice-' WHERE meta_id in (SELECT meta_id FROM sales_sequence_meta WHERE entity_type='invoice' AND store_id !=0);UPDATE sales_sequence_profile SET prefix = 'shipment-' WHERE meta_id in (SELECT meta_id FROM sales_sequence_meta WHERE entity_type='shipment' AND store_id !=0);UPDATE sales_sequence_profile SET prefix = 'creditmemo-' WHERE meta_id in (SELECT meta_id FROM sales_sequence_meta WHERE entity_type='creditmemo' AND store_id !=0);