<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");
$orderNumber = 260038;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
$store_id = $order->getStoreId();
foreach ($order->getAllItems() as $item){
$product = Mage::getModel('catalog/product')->setStoreId($store_id)->load($item->product_id);
$options = Mage::getModel('bundle/option')->getResourceCollection()
->setProductIdFilter($item->product_id)
->setPositionOrder();
$options->joinValues($store_id);
$selections = $product->getTypeInstance(true)
->getSelectionsCollection($product->getTypeInstance(true)
->getOptionsIds($product), $product);
foreach ($options->getItems() as $option) {
$option_id = $option->getId();
echo 'Option: ' . $option->getTitle() . ' [id: ' . $option_id . ']<br />';
foreach($selections as $selection){
if($option_id == $selection->getOptionId()){
$selection_id = $selection->getId();
$selection_name = $selection->getName();
$selection_qty = $selection->getSelectionQty();
$selection_sku = $selection->getSku();
$selection_product_id = $selection->getProductId();
$selection_weight = $selection->getWeight();
$selection_price = $selection->getPrice();
$data = 'Selection Name: ' . $selection_name;
$data .= ', SKU: ' . $selection_sku;
$data .= ', Qty: ' . $selection_qty;
$data .= ', ID: ' . $selection_id;
$data .= ', Product ID: ' . $selection_product_id;
$data .= ', Weight: ' . $selection_weight;
$data .= ', Price: ' . $selection_price;
echo $data . '<br />';
}
}
}
}
?>
Month: September 2016
Image upload in magento 2
<?php
namespace Bharat\Bannerslider\Controller\Adminhtml\Bannerslider;
use Magento\Framework\App\Filesystem\DirectoryList;
class Save extends \Magento\Backend\App\Action
{
protected $resultRawFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context
) {
parent::__construct($context);
}
public function execute()
{
try
{
$uploader = $this->_objectManager->create('Magento\MediaStorage\Model\File\Uploader',['fileId' => 'image']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
$uploader->addValidateCallback('image', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(DirectoryList::MEDIA);
$config = $this->_objectManager->get('Magento\Catalog\Model\Product\Media\Config');
$pth = $mediaDirectory->getAbsolutePath('Bannerslider/images');
$result = $uploader->save($mediaDirectory->getAbsolutePath('Bannerslider/images'));
$imgpath = $this->_prepareFile($result['file']);
$imgpathArray = explode("/", $imgpath);
unset($imgpathArray[count($imgpathArray)-1]);
$imgpath = implode("/", $imgpathArray);
$this->chmod_r($result['path'].'/' . $imgpath);
chmod($result['path'].'/' .$this->_prepareFile($result['file']), 0777);
unset($result['tmp_name']);
unset($result['path']);
$url = $this->_objectManager->get('Magento\Framework\UrlInterface')->getBaseUrl().$this->getBaseMediaUrlAddition();
$result['url'] = $url . '/' . $this->_prepareFile($result['file']);
$result['file'] = $result['file'];
}
catch(\Exception $e)
{
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
$response = $this->_objectManager->get('Magento\Framework\Controller\Result\RawFactory')->create();
$response->setHeader('Content-type', 'text/plain');
$response->setContents(json_encode($result));
return $response;
}
private function chmod_r($path) {
$dir = new \DirectoryIterator($path);
foreach ($dir as $item) {
chmod($item->getPathname(), 0777);
if ($item->isDir() && !$item->isDot()) {
chmod_r($item->getPathname());
}
}
}
private function getBaseMediaUrlAddition()
{
return 'pub/media/Bannerslider/images';
}
private function _prepareFile($file)
{
return ltrim(str_replace('\\', '/', $file), '/');
}
}
Get formatted price in Magento 2
<?php
echo $this->helper(‘Magento\Framework\Pricing\Helper\Data’)->currency(number_format($product->getprice(),0,”,”),true,false);
?>======= OR ========
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create(‘Magento\Framework\Pricing\Helper\Data’);
$formattedPrice = $priceHelper->currency($_product->getprice(), true, false);
?>
Get product image in magento 2
$imageUrl = $this->helper(‘Magento\Catalog\Helper\Image’)
->init($product, ‘product_page_image_large’)
->setImageFile($block->getImageFile())
->getUrl();