0

Anyone can help me achieve my query? I'm no expert in magento but I know php conditions but I don't know how to apply my need in magento. I'm thinking of using attributes because I believe there is no other solution. So here it goes:

In magento admin product page, I want to add manually cost price attribute, shipping cost attribute, and total cost attribute, but Total Cost automated, it should have a computation wherein it will add cost price and shipping cost. Then lastly, auto computation for the Profit attribute, where it will calculate, Selling price (Default Product price attribute) minus Total Cost divide by Selling price.

Looking forward to hear from all the magento ninja. Thank you!


So far this is what I've done based on the Module Tutorial Link given by @Sander. And followed the code of @Andarsh

in config.xml

    <modules>
    <ProfitAndTotalCost_ProfitAndTotalComputation>
        <version>0.0.1</version>
    </ProfitAndTotalCost_ProfitAndTotalComputation>
</modules>

<global>
    <models>
        <ProfitAndTotalCost_ProfitAndTotalComputation>
            <class>ProfitAndTotalCost_ProfitAndTotalComputation_Model</class>
        </ProfitAndTotalCost_ProfitAndTotalComputation>
    </models>

    <events>
        <catalog_product_save_before>
            <observers>
                <profitandtotalcost_profitandtotalcomputation>
                    <type>singleton</type>
                    <class>profitandtotalcost_profitandtotalcomputation/observer</class>
                    <method>addCostAndShipping</method>
                </profitandtotalcost_profitandtotalcomputation>
            </observers>
        </catalog_product_save_before>      
    </events>
</global>

For the Observer.php

class ProfitAndTotalCost_ProfitAndTotalComputation_Model_Observer
{
    public function addCostAndShipping($observer)
    {
       $product = $observer->getProduct();
       $cost_price = $product->getAttributeText('cost');
       $shipping_cost = $product->getAttributeText('supp_main_landed_cost');

       $total_cost = $cost_price + $shipping_cost;

       //save to total cost
       $product->setTotalCostMain($total_cost)->save();
       Mage::log($product->getId() . " - ".$product->getTotalCostMain(), null, 'custom_log.log'); //create a file in /var/log folder
    }   
}

This is my module: ProfitAndTotalCost_ProfitAndTotalComputation.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <ProfitAndTotalCost_ProfitAndTotalComputation>
            <active>true</active>
            <codePool>local</codePool>
        </ProfitAndTotalCost_ProfitAndTotalComputation>
    </modules>
</config>
  • You will have to create a module with all your business logic. Check this: amitbera.com/… – Adarsh Khatri Sep 9 '15 at 6:38
  • Can you walk me thru? Basic would help. Thanks! – KiD Cajes Sep 9 '15 at 6:40
  • @KiD Cajes , what you need to do is just write a small javascript for your admin side, what that will do is once you put in values into the cost fields, it will auto-populate the selling and profit fields, rest when you save the product the values would get saved., hope this should guide you in right direction, for more details let me know – huzefam Sep 9 '15 at 6:41
  • --AdarshKhatri --huzefam thank you for the inputs but I'm no code expert I'm just a user with a very super minimal code knowledge (acquired by youtube :-) – KiD Cajes Sep 9 '15 at 6:45
1

Alright, if I understand your requirement you could do this way:

Create 3 attributes- cost_price, shipping_cost and total_cost.

Now have a observer to add cost_price and shipping_cost and fetch to total_cost.

Your observer:

public function addCostAndShipping($observer)
{
   $product = $observer->getProduct();
   $cost_price = $product->getCostPrice();
   $shipping_cost = $product->getShippingCost();

   $total_cost = $cost_price + $shipping_cost;

   //save to total cost
   $product->setTotalCostMain($total_cost);
   Mage::log($product->getId() . " - ".$product->getTotalCostMain(), null, 'custom_log.log'); //create a file in /var/log folder
}

In your config.xml

<global>
    <events>
        <catalog_product_save_before>
            <observers>
                <yourmodule>
                    <type>singleton</type>
                    <class>yourmodule/observer</class>
                    <method>addCostAndShipping</method>
                </yourmodule>
            </observers>
        </catalog_product_save_before>      
    </events>
</global>

How it will work

When you enter cost_price and shipping_cost and save the product, before saving it magento will add them both and save it to your another attribute total_cost.

[Update]

change your config.xml

<global>
    <models>
        <profitandtotalcost_profitandtotalcomputation>
       <class>ProfitAndTotalCost_ProfitAndTotalComputation_Model</class>
        </profitandtotalcost_profitandtotalcomputation>
    </models>

    <events>
        <catalog_product_save_before>
            <observers>
                <profitandtotalcost_profitandtotalcomputation_observer>
                    <type>singleton</type>
                    <class>profitandtotalcost_profitandtotalcomputation/observer</class>
                    <method>addCostAndShipping</method>
                </profitandtotalcost_profitandtotalcomputation_observer>
            </observers>
        </catalog_product_save_before>      
    </events>
</global>

[Update 2]

Your folder/file structure as per your settings:

app/code/local/ProfitAndTotalCost/ProfitAndTotalComputation/etc/config.xml
app/code/local/ProfitAndTotalCost/ProfitAndTotalComputation/Model/Observer.php
//**Capital and small letter combination should be same as specified in config.xml**

Update 3

Your observer

class ProfitAndTotalCost_ProfitAndTotalComputation_Model_Observer
{
    public function addCostAndShipping($observer)
    {
       $product = $observer->getProduct();
       $cost_price = $product->getCostPrice();
       $shipping_cost = $product->getShippingCost();

       $total_cost = $cost_price + $shipping_cost;

       //save to total cost
       $product->setTotalCostMain($total_cost);

       Mage::log($product->getId() . " - ".$product->getTotalCostMain(), null, 'custom_log.log'); //create a file in /var/log folder
    }   
}
  • I'll read on module and try your code. Cause I don't know the steps on how and where to put this codes of yours. Thanks – KiD Cajes Sep 9 '15 at 6:55
  • $product->getAttributeText('shipping_cost'); ---- is this the attribute code or the attribute label? – KiD Cajes Sep 9 '15 at 7:32
  • Att code. They should be exactly same as when you created them. – Adarsh Khatri Sep 9 '15 at 7:34
  • $product->setTotalCost($total_cost)->save(); ------- the total cost attribute code I use is total_cost_main, how do I use it in your code? – KiD Cajes Sep 9 '15 at 7:48
  • $product->seTotalCostMain($total_cost)->save(). – Adarsh Khatri Sep 9 '15 at 7:50
2

The catalog_product_save_before event is probably what you are looking for. It' s fired every time a product is saved, giving you the chance to change data before it's actually saved in the database

In your modules config.xml

<global>
    <events>
        <catalog_product_save_before>
            <observers>
                <[namespace]_[module]_catalog_product_save_before>
                    <type>singleton</type>
                    <class>[Namespace]_[Module]_Model_Observer</class>
                    <method>catalogProductSaveBefore</method>
                </[namespace]_[module]_catalog_product_save_before>
            </observers>
        </catalog_product_save_before>
    </events>
</global>

in the Observer.php

class [Namespace]_[Module]_Model_Observer
{
    public function catalogProductSaveBefore($observer)
    {
        $product = $observer->getProduct();

        $cost = // calculate your cost here
        $product->setCost($cost);

        // and so on for all attributes you want to fill

        return $this;
    }
}

More on building a module here: http://www.smashingmagazine.com/2012/03/basics-creating-magento-module/

  • wow, I didn't expect this to be that complicated. Where do I exactly find this files? – KiD Cajes Sep 9 '15 at 6:47
  • @KiDCajes in that case please follow the tutorial about module creation first. Then the observer should explain itself – Sander Mangel Sep 9 '15 at 6:48
  • i guess we can simply do this in admin itself without going into the observer loop, just put in a basic javascript, which populates the cost and profit fields, and once you click on product save, fields are saved, extra benefit , admin knows what would be values of those attributes before he finally does a save – huzefam Sep 9 '15 at 6:52
  • @SanderMangel I will proof read the tutorial. Thanks! – KiD Cajes Sep 9 '15 at 6:54
  • @huzefam can you show me how to do your solution? – KiD Cajes Sep 9 '15 at 6:54
0

Create one js file, say cost.js

You can place the file in {{root_dir}}/js/cost.js

In cost.js (i am assuming you have included a jquery file)

jQuery('#shipping_cost').keyup(function() {
        var shipping_cost = parseInt(jQuery(this).val());
        var cost = jQuery('#cost').val();
        var total_cost = cost + shipping_cost
        jQuery('#total_cost').val(total_cost);
    });

Include this file, by creating a local.xml in

app/design/adminhtml/default/default/layout/
<layout>
     <adminhtml_catalog_product_new>
            <reference name="head">

                <action method="addJs"><script>lib/jquery/jquery-1.10.2.min.js</script></action>
                <action method="addJs"><script>lib/jquery/noconflict.js</script></action>
       <action method="addJs"><script>cost.js</script></action>      
            </reference>
        </adminhtml_catalog_product_new>
</layout>

So you should have you values populated once admin fills in the above two fields, you can proceed same for profit calculation.

  • I'll try this one and get back to you. – KiD Cajes Sep 9 '15 at 8:40
  • where do i get this neo/tax.js – KiD Cajes Sep 9 '15 at 9:02
  • sorry my bad, edited – huzefam Sep 9 '15 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.