Welcome to magento.
Unfortunately your implementation of the functionality is very wrong :(
You should not be placing logic as this in the actual template(s), especially not the base root template 2columns-left.phtml (eeeek) If any other page uses this root template, your code will also run there, thus not just the homepage.
You should create a module, with blocks, and insert the blocks via magento's layout system into the pages you require as child html block.
Regarding your issue, (I have not checked this, but I am sure of it) - your action for the post is calling $this->getSubmitUrl(), which is not a valid method for that block. In fact, I don't think the line above that $_product = $this->getProduct(); would yield any result as well.
Your collection looks a bit odd as well. (or rather overcomplicated)
If you just want to display one (latest added) product, all you need to do is order a collection of products descending, limit to 1, and then you got the latest added. (product ids are sequential)
Thus you could do:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$collection->getSelect()->order('entity_id desc')->limit(1);
I highly suggest you read these tutorials:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates
to understand how layouts, blocks, and templates work.
I also highly suggest you google for some tutorials on how to create magento modules.
Ok, so now, lets see how to create what you need.
Magento has core functionality that displays NEW products. See the Core block Catalog/product/new.php
This block has the method 'protected function _getProductCollection()' which builds the product collection for NEW products.
So, al you need to do is create your own module (lots of tutorials via google how to create a module) and in that module have a block based off this core block (simply copy that block new.php to your module and call it latest.php, and update the class name)
Then change the method that builds the collection to a collection similar to what I had noted above, which will give you the latest product. (or products(s) if you increase the limit in the collection)
Now, the last thing to do is to copy the template file that goes with the new.php class (design/frontend/base/default/template/catalog/product/new.phtml) to your own theme, named to latest.phtml (seems like a cool name ;) )
Adjust the template to your style/theme.
And in the homepage cms block you can do this to display the block/template
{{block type=”YOUR_MODULE/product_latest” name=”home.catalog.product.latest” alias=”product_homepage_latest” template=”catalog/product/latest.phtml”}}
Bang - done, with limited of custom code :)
Good luck, and please ask away as you continue your exploration of magento.
If you want to use SQL approach, You can use the following standalone script which can be run either via Web or CLI
<?php
/**
*
* @author Raj KB<magepsycho@gmail.com>
* @website http://www.magepsycho.com
* @extension MassImporterPro: Pricing - http://www.magepsycho.com/mass-importer-pro-price-importer-regular-special-tier-group.html
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Capture warning / notice as exception
set_error_handler('ctv_exceptions_error_handler');
function ctv_exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() == 0) {
return;
}
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
/**************************************************************************************************/
// UTILITY FUNCTIONS - START
/**************************************************************************************************/
function _mpLog($data, $includeSep = false)
{
$fileName = BP . '/var/log/m2-magepsycho-import-prices.log';
if ($includeSep) {
$separator = str_repeat('=', 70);
file_put_contents($fileName, $separator . '<br />' . PHP_EOL, FILE_APPEND | LOCK_EX);
}
file_put_contents($fileName, $data . '<br />' .PHP_EOL, FILE_APPEND | LOCK_EX);
}
function mpLogAndPrint($message, $separator = false)
{
_mpLog($message, $separator);
if (is_array($message) || is_object($message)) {
print_r($message);
} else {
echo $message . '<br />' . PHP_EOL;
}
if ($separator) {
echo str_repeat('=', 70) . '<br />' . PHP_EOL;
}
}
function getIndex($field)
{
global $headers;
$index = array_search($field, $headers);
if ( !strlen($index)) {
$index = -1;
}
return $index;
}
function readCsvRows($csvFile)
{
$rows = [];
$fileHandle = fopen($csvFile, 'r');
while(($row = fgetcsv($fileHandle, 0, ',', '"', '"')) !== false) {
$rows[] = $row;
}
fclose($fileHandle);
return $rows;
}
function _getResourceConnection()
{
global $obj;
return $obj->get('Magento\Framework\App\ResourceConnection');
}
function _getReadConnection()
{
return _getConnection('core_read');
}
function _getWriteConnection()
{
return _getConnection('core_write');
}
function _getConnection($type = 'core_read')
{
return _getResourceConnection()->getConnection($type);
}
function _getTableName($tableName)
{
return _getResourceConnection()->getTableName($tableName);
}
function _getAttributeId($attributeCode)
{
$connection = _getReadConnection();
$sql = "SELECT attribute_id FROM " . _getTableName('eav_attribute') . " WHERE entity_type_id = ? AND attribute_code = ?";
return $connection->fetchOne(
$sql,
[
_getEntityTypeId('catalog_product'),
$attributeCode
]
);
}
function _getEntityTypeId($entityTypeCode)
{
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
return $connection->fetchOne(
$sql,
[
$entityTypeCode
]
);
}
function _getIdFromSku($sku)
{
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne(
$sql,
[
$sku
]
);
}
function checkIfSkuExists($sku)
{
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, [$sku]);
}
function updatePrices($sku, $price, $storeId = 0)
{
$connection = _getWriteConnection();
$entityId = _getIdFromSku($sku);
$attributeId = _getAttributeId('price');
$sql = "INSERT INTO " . _getTableName('catalog_product_entity_decimal') . " (attribute_id, store_id, entity_id, value) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value)";
$connection->query(
$sql,
[
$attributeId,
$storeId,
$entityId,
$price
]
);
}
/**************************************************************************************************/
// UTILITY FUNCTIONS - END
/**************************************************************************************************/
try {
$csvFile = 'var/import/prices.csv'; #EDIT - The path to import CSV file (Relative to Magento2 Root)
$csvData = readCsvRows(BP . '/' . $csvFile);
$headers = array_shift($csvData);
$count = 0;
foreach($csvData as $_data) {
$count++;
$sku = $_data[getIndex('sku')];
$price = $_data[getIndex('price')];
if ( ! checkIfSkuExists($sku)) {
$message = $count .'. FAILURE:: Product with SKU (' . $sku . ') doesn\'t exist.';
mpLogAndPrint($message);
continue;
}
try {
updatePrices($sku, $price);
$message = $count . '. SUCCESS:: Updated SKU (' . $sku . ') with price (' . $price . ')';
mpLogAndPrint($message);
} catch(Exception $e) {
$message = $count . '. ERROR:: While updating SKU (' . $sku . ') with Price (' . $price . ') => ' . $e->getMessage();
mpLogAndPrint($message);
}
}
} catch (Exception $e) {
mpLogAndPrint(
'EXCEPTION::' . $e->getTraceAsString()
);
}
And the sample CSV can be used as:
"sku","price"
"24-MB01","31"
"24-MX02","32"
"24-UG04","94"
"24-UG05","99"
Source: http://bit.ly/M2-Bulk-Price-Update
Best Answer
use below code
Or