Search code examples
shopping-cartshopware

Packaging Costs


I would like to add to any order a service product similar to the row for shipment costs that gives information about the packaging costs.

Could anyone give me some hints which steps are required in order to implement the described goal. Thank you in advance.

enter image description here


Solution

  • OPTION 1

    The best way would be to create a 'free text field' (Configuration -> Free Text Field Management) and then modify the SQL Query in the Storefrontsettings (Configuration -> Basic Settings -> Frontend -> Shipping Costs Modules).

    It depends now how you want to vary it or if the packaging costs are the same. Though I believe you probably want to make them depend on the size of the product.

    So you could something like that:

    //You put this code in the Shipping Costs Modules
    MAX(a.topseller) AS has_topseller, MAX(at.attr3) AS has_comment
    MAX(b.esdarticle) AS has_esd,
    SUM(at.attr4*b.quantity)
    

    The important part here is the last line of code.

    To make this work you have to create a 'free text field' called attr4 and now in the article details in the backend (Items -> Overview -> Pen Symbol) you put in your wished value for the article.

    However this example is very specific because you have to do it for each and every article but the method stays the same for almost every case.

    1. Add new Free Text Field
    2. Edit the Value for it in the Article Details
    3. Modify the SQL in 'Shipping Costs Modules'

    If you understand german you can also checkout there documentation for that topic here.


    OPTION 2

    In Option 1 the price gets just added to the normal shipping costs. Though if you want them to be displayed seperately, the only way I can think of is to create a little Plugin.

    To start off with Option 2 you have to create a free text field though. After you created it go to checkout -> cart_footer.tpl

    There you add your newly added free text field between the shipping costs and the total sum like this:

    {block name='frontend_checkout_cart_footer_field_labels_shipping'}{/block}
    {block name='frontend_checkout_cart_footer_field_labels_package}
    
            {block name='frontend_checkout_cart_footer_field_labels_package_label'}
                            <div class="entry--label block">
                                Packaging Costs
                            </div>
            {/block}
            {block name='frontend_checkout_cart_footer_field_labels_package_label'}
                            <div class="entry--label block">
                                {s name="CartFooterLabelPackage"}{/s}
                            </div>
            {/block}
    {/block}
    {block name='frontend_checkout_cart_footer_field_labels_total'}{/block}
    

    This is basically just for the customer to see the price he pays for the packaging.

    Now it is time for the plugin creation.

    All you basically have to do is to create a local plugin, that submits some easy SQL queries.

    You create the basic plugin structure like shown here

    Then you modify it that on the checkout/finish controller you manipulate the invoice_amount:

    public function addPackage()
    {
      $package       = 2.5;
      $select        = "SELECT invoice_amount FROM s_attribute_configuration ORDER BY id DESC LIMIT 1";
      $select_result = Shopware()->Container()->get('models')->getConnection()->fetchColumn($select);
      $new_price     = $select_result + $package;
      $update        = "UPDATE s_attribute_configuration SET invoice_amount = $new_price ORDER BY id DESC LIMIT 1"
      $update_result = Shopware()->Db()->query($update);
    }
    

    Something like that should do it. Just hook that into the finish-action.

    The last problem you would have now is that the total sum gets updated already in the frontend. I think you could update that with some Javascript.


    OPTION 1.1

    Just got this idea while creating the PHP-Script for Option 2:

    Just add the packaging to the shipping costs like in OPTION 1 and then add a little info under the shipping costs that the packaging costs of (insert packaging sum here) are included there. You could do that with free text fields, too. Would be the best easiest solution that would get you almost perfectly to your wished result.

    I hope any of these help.