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

<p>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):</p>

<pre><code>&lt;?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()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = 'fundamentalsOfDigital';


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

        if (!$success)
        {
            print("Failed to save entry $entry-&gt;title\n");
        }        
        else
        {
            print("Saved $entry-&gt;title\n");
        }
    }
  }
</code></pre>

<p>}</p>

<p>To actually run the command I do this:</p>

<p><code>php yiic-env.php --env="console" businesslogic resaveFODEntries</code></p>

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

CC BY-SA 3.0
2017-04-04T01:19:44.013Z
2017-04-04T01:28:32.100Z
2017-04-04T01:28:32.100Z
Jeremy Daalder
<p>Because Commerce doesn't support <a href="https://craftcms.uservoice.com/forums/295386-commerce/suggestions/17939776-product-title-format" rel="nofollow noreferrer">Twig product title formatting</a> right now, I built a custom plugin to automatically update the title field upon save of a product.</p>

<p>Here is the part from the plugin where I am hooking into the <code>commerce_products.onBeforeSaveProduct</code> event:</p>

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

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

    $modelNumber = $product-&gt;modelNumber;
    $titlePieces[] = $modelNumber;

    $titleSuffix = $product-&gt;titleSuffix;
    $titlePieces[] = $titleSuffix;

    $product-&gt;getContent()-&gt;title = implode(' ', $titlePieces);
});
</code></pre>

<p>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.</p>

<p>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.</p>

<p>Any other ideas on how I could avoid manually re-saving all entries?</p>

<plugin-craftcommerce>
Craft Commerce Trigger Event to Re-save All Products
# Craft Commerce Trigger Event to Re-save All Products

(Asked by: username_0 on 2017-04-03)

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?

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2017-04-04. Score: 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):
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


---

