PrestaShop 1.7 add new dimension to combination

PrestaShop 1.7 is fantastic with blend of their own made framework with symfony 2.8.

Recently, i was working on one of the client's project and i need to submit pallets to Fedex Shipping in order to fetch Freight.

It is always easy to add combinations to the product using PrestaShop however, i wanted to add Maximum Pallet weight for the particular SKU created with PrestaShop combination.

Step 1: Let's add the field 

open file "/src/PrestaShopBundle/Form/Admin/Product/ProductCombination.php"

Search Line (my case it is line number 71 to 74):
 ->add('attribute_reference', 'Symfony\Component\Form\Extension\Core\Type\TextType', array(
            'required' => false,
            'label' => $this->translator->trans('Reference', [], 'Admin.Global')
        ))

Added new Line:

->add('max_bundles_per_pallet', 'Symfony\Component\Form\Extension\Core\Type\TextType', array(
            'required' => false,
            'label' => $this->translator->trans('Max Bundles Per Pallet', [], 'Admin.Global')
        ))

Step 1 Finished and this has assiged field to TWIG template.

===========================================================================

Step 2: Call Assigned field into the Twig Template

Open "/src/PrestaShopBundle/Resources/views/Admin/Product/Include/form_combination.html.twig"

Added following code to the file: (You would simply change max_bundles_per_pallet to your desire field name)

<div class="col-md-4">
                  <fieldset class="form-group">
                      <label class="form-control-label">{{ form.max_bundles_per_pallet.vars.label }}</label>
                      {{ form_errors(form.max_bundles_per_pallet) }}
                      {{ form_widget(form.max_bundles_per_pallet) }}
                  </fieldset>
                </div>

Step 2 finished and now you can go to admin->products->edit product->edit combination and you will be able to see new field there.

===============================================================================================

Step 3: Insert/Save Field into Database Table ps_product_attribute 

For that first add new Column in table (ps_product_attribute) as "max_bundles_per_pallet"

Now open, "/src/Adapter/Product/AdminProductWrapper.php" on line number 96 

$product->updateAttribute() is called

You should add your new field parameter to it by adding line $combinationValues['max_bundles_per_pallet'] 

$product->updateAttribute(

..

$combinationValues['attribute_reference'],
$combinationValues['max_bundles_per_pallet'], <== Added this line
$combinationValues['attribute_ean13'],

..

)

Now open: "/classes/Product.php" and search for method/function "updateAttribute" which i could find on line number 1610

public function updateAttribute(

...

$max_bundles_per_pallet,

...

)

{

....

$combination->reference = pSQL($reference);
$combination->max_bundles_per_pallet = pSQL($max_bundles_per_pallet); <== Added this line
$combination->location = pSQL($location);

....

}

This method is actually passing data further to Combination.php class

Now, open "/Classes/Combination.php"

First declared public variable, public $max_bundles_per_pallet;

and than inside public static $definition = array (

//

'max_bundles_per_pallet' =>            array('type' => self::TYPE_STRING, 'size' => 32),

//

)

This will save the fields into database.

========================================================================

Step 5: Fetch Data from the Database table and present into form

Open: "/src/Adapter/CombinationDataProvider.php" find method 

public function completeCombination() {

return array(

...

'max_bundles_per_pallet' => $combination['max_bundles_per_pallet'],

...

);

}

This will now fetch the data from database.

In my next forum topic, i will suggest how to use given for freight calculation in the cart.

Comments are appreciated!