Simply looking to create an area/block in the admin area of a product page that displays what the current gross profit is on that product. I.e. calculating the difference between the cost price and selling price of the item. So for example we have a cost attribute, and either a price or special_price attribute for the selling price. Some products have a special (sale) price active, some don't, so this probably requires an if statement. Initially I explored creating an attribute to do this, but that didn't really work out, so looking to hard code something into the admin page. Ideally creating small module to do this. Any ideas on the best way to go about or detail some of the code involved to get me started?
Magento – Is it possible to display the gross profit of a product as an attribute in the admin page of a product
adminadminhtmlpriceproducttotals
Related Solutions
Creating a new product will not generate product->id that you are passing in updateStock Method so that is why updateStock is working only for existing products.
I would suggest to check
$product = Mage::getModel('catalog/product');
$product->load(18); //18 is my existing product id
echo '<pre>';
print_r($product->getData());
exit;
This will give you idea about data associated to product.
Recently, I have created a method that is used to create product. Please review it.
public function createProduct($categories = array(3),$productName,$sku,$price=0,$productDescription,$productShortDescription,$status,$visibility,$attributeSet,$taxClass,$qty,$stock,$websites = array(1),$productImage='',$productMetaTitle,$productMetaKeyword,$productMetaDescription) {
$product = new Mage_Catalog_Model_Product();
// Build the product
$prod = array();
$prod['sku'] = $sku;
$prod['attribute_set_id'] = $attributeSet;
$prod['type_id'] = 'simple';
$prod['category_ids'] = $categories;
$prod['website_ids'] = $websites;
$prod['name'] = $productName;
$prod['description'] = $productDescription;
$prod['short_description'] = $productShortDescription;
$prod['weight'] = '1';
$prod['status'] = $status;
$prod['url_key'] = Mage::getModel('catalog/product_url')->formatUrlKey($productName);
$prod['url_path'] = Mage::getModel('catalog/product_url')->formatUrlKey($productName); //TO-DO :: find correct method
$prod['visibility'] = $visibility;
$prod['price'] = $price;
$prod['tax_class_id'] = $taxClass;
$prod['meta_title'] = $productMetaTitle;
$prod['meta_keyword'] = $productMetaKeyword;
$prod['meta_description'] = $productMetaDescription;
$product->setData($prod);
$product->setStockData(array(
'is_in_stock' => $stock,
'qty' => $qty,
'manage_stock' => 1
));
if($productImage) {
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery($productImage,array('image','small_image','thumbnail'),false,false);
}
try {
$product->save();
return $product->getId();
}
catch (Exception $e){
echo $e->getMessage();
}
}
Hope above method helps you.
Also if you set manage stock from System->Configuration->Catalog->Inventory->Product Stock Options to Yes, then you don't need to pass manage_stock in above method. Product will be automatically created with manage stock set to yes.
you need to add this in your config.xml
under the config
tag.
<adminhtml>
<layout>
<updates>
<tbe_report>
<file>tbe_report.xml</file>
</tbe_report>
</updates>
</layout>
</adminhtml>
then create the file app/design/adminhtml/default/default/layout/tbe_report.xml
and put your layout updates in there.
[EDIT]
your layout handle is determined from the url of the page.
Just replace admin
with adminhtml
and replace the slashes with underscores.
If the action is index
and is not present in the url just add it in the layout handle.
Here are some examples to make it clearer.
If the url is admin/tbe_report/something
then the layout handle is adminhtml_tb_report_something
.
if the url is admin/tbe_report/
then the layout handle is adminhtml_tb_report_index
.
Best Answer
I just completed a module to calculate the profit and profit ratio on a product. The calculation is done when the product is saved and stores the values in 2 attributes (product_profit and product_profit_ratio). The module also adds 2 columns to the productgrid to display both the profit and ratio.
I published the source on BitBucket, see https://bitbucket.org/genmato/productprofit/.
I tested it on both 1.7.0.2 and 1.8, and might work on 1.6 also. Not compatibel with 1.5 and lower.