howto auto-populate custom fields from product templates

I have added a custom field to product templates and to products as well. How can I achieve that the custom field in products gets auto-populated from the product template field when the product is added to the quote?

I have version 7.6.

Parents
  • Hi,

    I'd recommend not updating the front-end and doing this customization on the back-end for two reasons:

    1) If you end up using the API, these customizations would not work

    2) Eventually Quotes are going to be moved from BWC  to Sidecar, at which point your customizations will stop working

    Instead, I recommend using a logic hook.

    In custom/modules/Products/logic_hooks.php, put the following file:

    <?php

        $hook_array['before_save'][] = Array(
            //Processing index. For sorting the array.
            1,
          
            //Label. A string value to identify the hook.
            'Update Custom Fields',
          
            //The PHP file where your class is located.
            'custom/modules/Products/before_save_class.php',
          
            //The class the method is in.
            'before_save_class',
          
            //The method to call.
            'updateCustomFields'
        );

    ?>

    Then, in custom/modules/Products/before_save_class.php, put the following code:

    <?php

    if (!defined('sugarEntry') || !sugarEntry)
        die('Not A Valid Entry Point');
      
    class before_save_class {
            function updateCustomFields($bean, $event, $arguments)
            {
                $productTemplate = BeanFactory::getBean('ProductTemplates', $bean->product_template_id);
                if (!empty($productTemplate->id)){
                    $bean->custom_field_on_product = $productTemplate->custom_field_on_product_template;
                }       
            }
    }

    ?>

    This assumes that custom_field_on_product is the custom field on the product, and custom_field_on_product_template is the custom field on the product template.

    Let me know if you have any issues with this.

Reply
  • Hi,

    I'd recommend not updating the front-end and doing this customization on the back-end for two reasons:

    1) If you end up using the API, these customizations would not work

    2) Eventually Quotes are going to be moved from BWC  to Sidecar, at which point your customizations will stop working

    Instead, I recommend using a logic hook.

    In custom/modules/Products/logic_hooks.php, put the following file:

    <?php

        $hook_array['before_save'][] = Array(
            //Processing index. For sorting the array.
            1,
          
            //Label. A string value to identify the hook.
            'Update Custom Fields',
          
            //The PHP file where your class is located.
            'custom/modules/Products/before_save_class.php',
          
            //The class the method is in.
            'before_save_class',
          
            //The method to call.
            'updateCustomFields'
        );

    ?>

    Then, in custom/modules/Products/before_save_class.php, put the following code:

    <?php

    if (!defined('sugarEntry') || !sugarEntry)
        die('Not A Valid Entry Point');
      
    class before_save_class {
            function updateCustomFields($bean, $event, $arguments)
            {
                $productTemplate = BeanFactory::getBean('ProductTemplates', $bean->product_template_id);
                if (!empty($productTemplate->id)){
                    $bean->custom_field_on_product = $productTemplate->custom_field_on_product_template;
                }       
            }
    }

    ?>

    This assumes that custom_field_on_product is the custom field on the product, and custom_field_on_product_template is the custom field on the product template.

    Let me know if you have any issues with this.

Children
No Data