1

Because Commerce doesn't support Twig product title formatting right now, I built a custom plugin to automatically update the title field upon save of a product.

Here is the part from the plugin where I am hooking into the commerce_products.onBeforeSaveProduct event:

craft()->on('commerce_products.onBeforeSaveProduct', function(Event $event) {
    $product = $event->params['product'];

    if (count($product->collection))
    {
        $collection = $product->collection[0]->title;
        $titlePieces[] = $collection;
    }

    $modelNumber = $product->modelNumber;
    $titlePieces[] = $modelNumber;

    $titleSuffix = $product->titleSuffix;
    $titlePieces[] = $titleSuffix;

    $product->getContent()->title = implode(' ', $titlePieces);
});

All that works great when I manually edit a product, but we have about 500 products in the system right now, and nobody really wants to open up every entry and manually save it. The hope was that we could do something in the CP to trigger an event to re-save all product entries.

I went into the Commerce settings for each Product Type and re-saved the settings, which did trigger a "Resaving Furniture products" task. But, that didn't appear to update the title field for all the products.

Any other ideas on how I could avoid manually re-saving all entries?

1 Answer 1

1

There are probably many approaches, but you could write a simple console command (great guide here: https://spin.atomicobject.com/2015/06/16/craft-console-plugin/) in a plugin to do this.

Here's one I prepared earlier to use as an example...in my case I am doing this because I am using a pre-parse field that pulls in some external markdown content, but the principle is the same (assuming re-saving the entries updates the title field as you hope - but should take only a few mins to try!) - you'll need to adapt for commerce (Commerce_Product I think):

<?php
namespace Craft;

class BusinessLogicCommand extends BaseCommand
{
public function actionResaveFODEntries()
{
    print("### Resaving entries for Fundamentals Of Digital\n(So search indexes are updated by causing PreParse to re-pull in the generated content).\n\n");

    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'fundamentalsOfDigital';


    foreach ($criteria as $entry)
    {
        $success = craft()->entries->saveEntry($entry);

        if (!$success)
        {
            print("Failed to save entry $entry->title\n");
        }        
        else
        {
            print("Saved $entry->title\n");
        }
    }
  }

}

To actually run the command I do this:

php yiic-env.php --env="console" businesslogic resaveFODEntries

...note the use of a specific environment, console in there - this is defined in my config/general.php with appropriate settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.