use Magento\Framework\App\Action\Context; class Createproduct extends \Magento\Framework\App\Action\Action { protected $directoryList; protected $file; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Framework\App\Filesystem\DirectoryList $directoryList, \Magento\Framework\Filesystem\Io\File $file) { $this->_resultpageFactory = $pageFactory; $this->directoryList = $directoryList; $this->file = $file; return parent::__construct($context); } protected function getMediaDirTmpDir() { return $this->directoryList->getPath($this->directoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; } public function setImage($product, $imageUrl, $visible = false, $imageType = []) { $tmpDir = $this->getMediaDirTmpDir(); $this->file->checkAndCreateFolder($tmpDir); $newFileName = $tmpDir . baseName($imageUrl); $result = $this->file->read($imageUrl, $newFileName); if ($result) { $product->addImageToMediaGallery($newFileName, $imageType, true, $visible); } return $result; } public function execute() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('\Magento\Catalog\Model\Product')->load(1); $imagePath = "https://domain.com/image.jpg"; // path of the image $this->setImage($product, $imagePath, false, $imageType = ['image', 'small_image', 'thumbnail']); $product->save(); } }
Shopify, Shopify Apps, Magento, WordPress, Codeigniter, Joomla, Big Commerce | PHP
Showing posts with label Products. Show all posts
Showing posts with label Products. Show all posts
Saturday, 23 June 2018
How to Import / Insert / Add product image from URL in Magento 2
Friday, 15 June 2018
When create child product of configurable product so why its create virtual product in magento 2
You do not set the weight before generation of children's products. Depending on this field the Magento decides what type of product is created.
Thursday, 14 June 2018
Forloop query not workin in Magento 2 | Get same Ordered Qty of all products in Magento 2
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$soldModel = $objectManager->get('Magento\Reports\Model\ResourceModel\Product\Sold\Collection');
$collections = $productCollection->addAttributeToSelect('*')->setOrder('entity_id', 'desc')->load();
$productdata = array();
foreach ($collections as $product){
$soldproduct = clone $soldModel;
$soldproduct = $soldproduct->addOrderedQty()->addAttributeToFilter('product_id', $product->getId())->setOrder('ordered_qty', 'desc')->getFirstItem();
$sold = (int)$soldproduct->getOrderedQty();
$product->setSold($sold);
$productdata['products'][] = $product->getData();
}
echo '<pre>';print_r($productdata);
Tuesday, 26 December 2017
How to get product Tire / Final / Group price custom in Magento
How to get product Tire / Final / Group price custom order in Magento
$productid = 4108;
$qty = 3;
$customergroup = 1;
$_productB = Mage::getModel('catalog/product')->load($productid);
$_productB->setCustomerGroupId($customergroup);
echo $_productB->getPriceModel()->getFinalPrice($qty, $_productB);exit;
Saturday, 8 October 2016
How to upload products multiple images in codeigniter Gocart in admin panel
Open : gocart/views/admin/iframe/product_image_uploader.php
Update product_image_upload() function
Open : gocart/views/admin/product_form.php
Update : add_product_image(data) function
<?php include('header.php');?>Open : gocart/controllers/admin/products.php
<script type="text/javascript">
<?php if( $this->input->post('submit') ):?>
$(window).ready(function(){
$('#iframe_uploader', window.parent.document).height($('body').height()); });
<?php endif;?>
<?php if($file_name):?>
parent.add_product_image('<?php echo json_encode($file_name);?>');
<?php endif;?>
</script>
<?php if (!empty($error)): ?>
<div class="alert alert-error">
<a class="close" data-dismiss="alert">×</a>
<?php echo $error; ?>
</div>
<?php endif; ?>
<div class="row-fluid">
<div class="col-md-12">
<?php echo form_open_multipart($this->config->item('admin_folder') . '/products/product_image_upload', 'class="form-inline "'); ?>
<?php echo form_upload(array('name' => 'userfile[]', 'id' => 'userfile', 'class' => 'input-file', 'multiple'=>"true" )); ?>
<input class="btn" name="submit" type="submit" value="<?php echo lang('upload'); ?>" />
</form>
</div>
</div>
<?php include('footer.php');
Update product_image_upload() function
function product_image_upload() {
$data['file_name'] = false;
$data['error'] = false;
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = $this->config->item('size_limit');
$config['upload_path'] = 'uploads/images/full';
$config['encrypt_name'] = true;
$config['remove_spaces'] = true;
$this->load->library('upload', $config);
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
//$this->upload->initialize($this->set_upload_options());
if ($this->upload->do_upload()) {
$upload_data = $this->upload->data();
$this->load->library('image_lib');
/*
I find that ImageMagick is more efficient that GD2 but not everyone has it
if your server has ImageMagick then you can change out the line
$config['image_library'] = 'gd2';
with
$config['library_path'] = '/usr/bin/convert'; //make sure you use the correct path to ImageMagic
$config['image_library'] = 'ImageMagick';
*/
//this is the larger image
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/images/full/' . $upload_data['file_name'];
$config['new_image'] = 'uploads/images/medium/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 600;
$config['height'] = 500;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//small image
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/images/medium/' . $upload_data['file_name'];
$config['new_image'] = 'uploads/images/small/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 235;
$config['height'] = 235;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//cropped thumbnail
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/images/small/' . $upload_data['file_name'];
$config['new_image'] = 'uploads/images/thumbnails/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 150;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$data['file_name'][] = $upload_data['file_name'];
}
}
if ($this->upload->display_errors() != '') {
$data['error'] = $this->upload->display_errors();
}
$this->load->view($this->config->item('admin_folder') . '/iframe/product_image_uploader', $data);
}
Open : gocart/views/admin/product_form.php
Update : add_product_image(data) function
function add_product_image(data) {
var $images = $.parseJSON(data);
$.each($images, function(i, image) {
p = image.split('.');
var photo = '<?php add_image("' + p[0] + '", "' + p[0] + '.' + p[1] + '", '
', '
', '
', base_url('
uploads / images / thumbnails '));?>';
$('#gc_photos').append(photo);
$('#gc_photos').sortable('destroy');
photos_sortable();
});
}
Thursday, 5 May 2016
How to Shopify API Product / Order / Collection get sorted when passing in an created_at / updated_at filter
What would be ideal would be to be able to get results in ascending/descending order one way or another, but without having to pass in since_id, because we want to be able to pull in new data on orders on an ongoing basis as they may be updated.
Its possible to sort the Products/Orders/Collection by passing an "order" parameter. The "order" parameter should contain the field to sort by (supported fields are "created_at", "updated_at" and "processed_at"), followed by a space and then by an "asc" or "desc".
Please check following Example for solution.
Its possible to sort the Products/Orders/Collection by passing an "order" parameter. The "order" parameter should contain the field to sort by (supported fields are "created_at", "updated_at" and "processed_at"), followed by a space and then by an "asc" or "desc".
Please check following Example for solution.
https://example.myshopify.com/admin/orders.json?limit=2&fields=id,created_at&order=created_at%20asc
Friday, 29 April 2016
Adding products to cart via url in magento
If your Planning to do a website that opens directly on the cart page, in
the cart it needs to have some products already selected and it should
be ready to go to the checkout page.
Every time the user goes to the page, some products.
Every time the user goes to the page, some products.
<a href="http://example.com/index.php/checkout/cart/add/product/1/form_key/<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>/">My cart</a>
Thursday, 11 February 2016
How to display product option/variant in category page in woocommerce wordpress
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_rating - 5
* @hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</a>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
with this:
<?php
/**
* ADD PRODUCT VARIABLE WITH ADD TO CART ON CATALOG PAGE
ADDED BY GW
**/
?>
<div class="catbox">
<?php
//ADD SHORT DESCRIPTION TO PRODUCT
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5); ?>
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* @hooked woocommerce_template_loop_rating - 5
* @hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
<?php
//ADD VARIATIONS BEFORE ADD TO CART
if($product->product_type == "variable"){
woocommerce_variable_add_to_cart();
} else {
woocommerce_template_loop_add_to_cart();
}
?>
</div>
Monday, 28 September 2015
Creating xml file of all products fields in magento programmatically
I will be using Varien_Simplexml_Element class to read write xml nodes. The path to this class file is lib/Varien/Simplexml/Element.php
Here is a sample XML file which I am going to read through Magento code. I will also be adding an XML node to the following XML data.
Here is a sample XML file which I am going to read through Magento code. I will also be adding an XML node to the following XML data.
$file = "products.xml";
if (file_exists($file)) { unlink ($file); }
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => '1'))
->addAttributeToFilter('type_id', array('eq' => 'simple'));
//process your product collection as per your bussiness logic
$doc = new DOMDocument();
$doc->formatOutput = true;
$productsX = $doc->createElement( "products" );
$doc->appendChild( $productsX );
foreach($products as $_product){
$product = $doc->createElement( "product" );
$sku = $doc->createElement( "sku" );
$sku->appendChild(
$doc->createTextNode($_product->getSku())
);
$product->appendChild($sku);
$name = $doc->createElement("name");
$name->appendChild(
$doc->createTextNode( trim($_product->getName()) )
);
$product->appendChild($name);
$image = $doc->createElement( "image" );
$image->appendChild(
$doc->createTextNode( trim($_product->getData('image')) )
);
$product->appendChild( $image );
$smallimage = $doc->createElement( "smallimage" );
$smallimage->appendChild(
$doc->createTextNode( trim($_product->getData('small_image')) )
);
$product->appendChild( $smallimage );
$thumbnail = $doc->createElement( "thumbnail" );
$thumbnail->appendChild(
$doc->createTextNode( trim($_product->getData('thumbnail')) )
);
$product->appendChild( $thumbnail );
$urlkey = $doc->createElement( "urlkey" );
$urlkey->appendChild(
$doc->createTextNode( trim($_product->getData('url_key')) )
);
$product->appendChild( $urlkey );
$shippingdelivery = $doc->createElement( "shippingdelivery" );
$shippingdelivery->appendChild(
$doc->createTextNode( trim($_product->getShippingDelivery()) )
);
$product->appendChild( $shippingdelivery );
$shippingweight = $doc->createElement( "shippingweight" );
$shippingweight->appendChild(
$doc->createTextNode( trim($_product->getShippingWeight()) )
);
$product->appendChild( $shippingweight );
$alu = $doc->createElement( "alu" );
$alu->appendChild(
$doc->createTextNode( trim($_product->getAlu()) )
);
$product->appendChild( $alu );
$upsize = $doc->createElement( "upsize" );
$upsize->appendChild(
$doc->createTextNode( trim($_product->getUpsize()) )
);
$product->appendChild( $upsize );
$price = $doc->createElement( "price" );
$price->appendChild(
$doc->createTextNode( trim($_product->getPrice()) )
);
$product->appendChild( $price );
$specialprice = $doc->createElement( "specialprice" );
$specialprice->appendChild(
$doc->createTextNode( trim($_product->getSpecialPrice()) )
);
$product->appendChild( $specialprice );
$color = $doc->createElement( "color" );
$color->appendChild(
$doc->createTextNode( trim($_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)))
);
$product->appendChild( $color );
$status = $doc->createElement( "status" );
$status->appendChild(
$doc->createTextNode( trim($_product->getResource()->getAttribute('status')->getFrontend()->getValue($_product)) )
);
$product->appendChild( $status );
$description = $doc->createElement( "description" );
$description->appendChild(
$doc->createTextNode( trim($_product->getDescription()) )
);
$product->appendChild( $description );
$qty = $doc->createElement( "qty" );
$qty->appendChild(
$doc->createTextNode( trim(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()) )
);
$product->appendChild( $qty );
$availability = $doc->createElement( "availability" );
$availability->appendChild(
$doc->createTextNode( trim(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock()) )
);
$product->appendChild( $availability );
$productsX->appendChild($product);
}
file_put_contents($file,$doc->saveXML(),FILE_APPEND);
Saturday, 11 July 2015
How to sort Magento products by popularity in frontend?
I have created a module to add a new option in 'sort by' dropdown box
in the category page. the new option should show in each and every
category page same as 'position' option which is already there. I want
to add 'popularity' as an option. My Model class is executing and it's
adding the option to the option array. But the block is not working.
Please I need your help to find the issue in my code?
Here is the Block class
<?php
class Tal_Popularity_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if($this->getCurrentOrder() == 'popularity'){
$this->_collection->sortByReview($this->getCurrentDirection());
}
else if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
return $this;
}
}
following is my config.xml
<?xml version="1.0" ?>
<config>
<modules>
<Tal_Popularity>
<version>0.1.0</version>
</Tal_Popularity>
</modules>
<frontend>
<routers>
<popularity>
<use>standard</use>
<args>
<module>Tal_Popularity</module>
<frontName>popularity</frontName>
</args>
</popularity>
</routers>
</frontend>
<global>
<models>
<catalog>
<rewrite>
<config>Tal_Popularity_Model_Config</config>
</rewrite>
</catalog>
</models>
<blocks>
<catalog> <rewrite> <product_list_toolbar>Tal_Popularity_Block_Product_List_Toolbar</product_list_toolbar> </rewrite> </catalog>
<catalog> <rewrite> <catalog>Tal_Popularity_Block</catalog> </rewrite> </catalog> </blocks> <resources> <popularity_setup> <setup> <module>Tal_Popularity</module> </setup> <connection> <use>core_setup</use> </connection> </popularity_setup> <popularity_write> <connection> <use>core_write</use> </connection> </popularity_write> <popularity_read> <connection> <use>core_read</use> </connection> </popularity_read> </resources> </global> </config>
<rewrite> <product_list_toolbar>Tal_Popularity_Block_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
<catalog>
<rewrite>
<catalog>Tal_Popularity_Block</catalog>
</rewrite>
</catalog>
</blocks>
<resources>
<popularity_setup>
<setup>
<module>Tal_Popularity</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</popularity_setup>
<popularity_write>
<connection>
<use>core_write</use>
</connection>
</popularity_write>
<popularity_read>
<connection>
<use>core_read</use>
</connection>
</popularity_read>
</resources>
</global>
</config>
Subscribe to:
Posts (Atom)