0

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?

0

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); 
| improve this answer | |

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.