<p>After hours of perusing, I managed to find a working solution to this issue.  I threw this implementation from InstallService located in <code>craft/app/services</code>.</p>

<p>I realized that CommerceProduct and Variant Field Layouts were set into the Commerce Product Type Model via <code>setFieldLayout()</code> function, therefore all I would need to do is create a new Field Layout with the fields that I required and set them into <code>defaultProductTypes()</code>.</p>

<pre><code>    //Write this code in between CommerceProductFieldLayout and Variant type field Layout

    //Any custom fields for your products go here.
    //Format: $yourFieldName = FieldsVariable::getFieldByHandle("Your field's 
handle");
    $bodyField = FieldsVariable::getFieldByHandle('body');
    $tagsField= FieldsVariable::getFieldByHandle('tags');

    //Assemble a new layout that contains the fields you need.
    $infoLayout = craft()-&gt;fields-&gt;assembleLayout(
        array(
            'Content' =&gt; array($bodyField-&gt;id, $tagsField-&gt;id) //Add fields into this array to display your plugins. (e.g. - $YourFieldVariable-&gt;id)
        ),
        array($bodyField-&gt;id) //Add any required fields here.
    );

    // Save Layout into specified product type.
    $productType-&gt;setFieldLayout($infoLayout); 
</code></pre>

CC BY-SA 3.0
2017-07-17T17:47:05.533Z
2017-07-17T17:47:05.533Z
Chris Fam
<p>So I've been building my plugin to import product listings directly from Salesforce. So far I've been making progress. However, I've approached another roadblock and I was wondering how I would approach this issue. I want to write some code to add default fields into my product layout (e.g.- Body richtext, Image Asset field), </p>

<p>Someone showed me an example of this function that adds default content for a ProductType</p>

<pre><code>/**
 * @throws \Craft\Exception
 * @throws \Exception
 */
private function defaultProductTypes()
{
    $productType = new Commerce_ProductTypeModel;
    $productType-&gt;name = 'Clothing';
    $productType-&gt;handle = 'clothing';
    $productType-&gt;hasDimensions = true;
    $productType-&gt;hasUrls = true;
    $productType-&gt;hasVariants = false;
    $productType-&gt;hasVariantTitleField = false;
    $productType-&gt;titleFormat = "{product.title}";
    $productType-&gt;template = 'shop/products/_product';

    $fieldLayout = FieldLayoutModel::populateModel(['type' =&gt; 'Commerce_Product']);
    \Craft\craft()-&gt;fields-&gt;saveLayout($fieldLayout);
    $productType-&gt;asa('productFieldLayout')-&gt;setFieldLayout($fieldLayout);

    $variantFieldLayout = FieldLayoutModel::populateModel(['type' =&gt; 'Commerce_Variant']);
    \Craft\craft()-&gt;fields-&gt;saveLayout($variantFieldLayout);
    $productType-&gt;asa('variantFieldLayout')-&gt;setFieldLayout($variantFieldLayout);

    \Craft\craft()-&gt;commerce_productTypes-&gt;saveProductType($productType);

    $productTypeLocales = \Craft\craft()-&gt;i18n-&gt;getSiteLocaleIds();

    foreach ($productTypeLocales as $locale) {
        \Craft\craft()-&gt;db-&gt;createCommand()-&gt;insert('commerce_producttypes_i18n', [
            'productTypeId' =&gt; $productType-&gt;id,
            'locale' =&gt; $locale,
            'urlFormat' =&gt; 'shop/products/{slug}'
        ]);
    }
}
</code></pre>

<p>I've tried to read through to see where I could include extra fields, but I'm a bit lost on where to start. Can anyone provide some insight or approach to this? </p>

<plugin-development><plugin-craftcommerce><custom-field>
Anyway to programmatically add default fields to a layout?
# Anyway to programmatically add default fields to a layout?

(Asked by: username_0 on 2017-07-13)

So I've been building my plugin to import product listings directly from Salesforce. So far I've been making progress. However, I've approached another roadblock and I was wondering how I would approach this issue. I want to write some code to add default fields into my product layout (e.g.- Body richtext, Image Asset field), 
Someone showed me an example of this function that adds default content for a ProductType
/**
 * @throws \Craft\Exception
 * @throws \Exception
 */
private function defaultProductTypes()
{
    $productType = new Commerce_ProductTypeModel;
    $productType->name = 'Clothing';
    $productType->handle = 'clothing';
    $productType->hasDimensions = true;
    $productType->hasUrls = true;
    $productType->hasVariants = false;
    $productType->hasVariantTitleField = false;
    $productType->titleFormat = "{product.title}";
    $productType->template = 'shop/products/_product';

    $fieldLayout = FieldLayoutModel::populateModel(['type' => 'Commerce_Product']);
    \Craft\craft()->fields->saveLayout($fieldLayout);
    $productType->asa('productFieldLayout')->setFieldLayout($fieldLayout);

    $variantFieldLayout = FieldLayoutModel::populateModel(['type' => 'Commerce_Variant']);
    \Craft\craft()->fields->saveLayout($variantFieldLayout);
    $productType->asa('variantFieldLayout')->setFieldLayout($variantFieldLayout);

    \Craft\craft()->commerce_productTypes->saveProductType($productType);

    $productTypeLocales = \Craft\craft()->i18n->getSiteLocaleIds();

    foreach ($productTypeLocales as $locale) {
        \Craft\craft()->db->createCommand()->insert('commerce_producttypes_i18n', [
            'productTypeId' => $productType->id,
            'locale' => $locale,
            'urlFormat' => 'shop/products/{slug}'
        ]);
    }
}

I've tried to read through to see where I could include extra fields, but I'm a bit lost on where to start. Can anyone provide some insight or approach to this?

**Tags:** plugin-development, plugin-craftcommerce, custom-field

---

## 1 answers

### Answer by username_0 on 2017-07-17. Score: 0 | Accepted answer

After hours of perusing, I managed to find a working solution to this issue.  I threw this implementation from InstallService located in craft/app/services.
I realized that CommerceProduct and Variant Field Layouts were set into the Commerce Product Type Model via setFieldLayout() function, therefore all I would need to do is create a new Field Layout with the fields that I required and set them into defaultProductTypes().
    //Write this code in between CommerceProductFieldLayout and Variant type field Layout

    //Any custom fields for your products go here.
    //Format: $yourFieldName = FieldsVariable::getFieldByHandle("Your field's 
handle");
    $bodyField = FieldsVariable::getFieldByHandle('body');
    $tagsField= FieldsVariable::getFieldByHandle('tags');

    //Assemble a new layout that contains the fields you need.
    $infoLayout = craft()->fields->assembleLayout(
        array(
            'Content' => array($bodyField->id, $tagsField->id) //Add fields into this array to display your plugins. (e.g. - $YourFieldVariable->id)
        ),
        array($bodyField->id) //Add any required fields here.
    );

    // Save Layout into specified product type.
    $productType->setFieldLayout($infoLayout);


---

