Where to change automatically created campaign names?

MCM

Member
Hi guys, when a campaign name is automatically created (for example when a new email is created after a sending of postponed one), it creates the name + "_#62", "_#63" and so on.

I would need to change the format, mainly get rid of space and # - where can I do it?

1693331860127.png

Thank you a lot!
 
You should be able to connect to the copy_campaign action hook, from apps/init-custom.php
PHP:
<?php
  
hooks()->addAction('copy_campaign', function(CAttributeCollection $collection){
    /** @var Campaign $campaign **/
    $campaign = $collection->itemAt('copied');
  
    $prefix = '#';

    if (preg_match('/'.$prefix.'(\d+)$/', $campaign->name, $matches)) {
        $counter = (int)$matches[1];
        $counter++;
        $campaign->name = (string)preg_replace('/'.$prefix.'(\d+)$/', $prefix . $counter, $campaign->name);
    } else {
        $campaign->name .= ' ' . $prefix . '1';
    }
  
    $campaign->save(false);
});
 
Last edited:
@twisted1919

1. The first code you shared (below) was better, this one adds new prefixes next to it again and again

PHP:
<?php
 

hooks()->addAction('copy_campaign', function(CAttributeCollection $collection){
    /** @var Campaign $campaign **/
    $campaign = $collection->itemAt('copied');
 
    $prefix = '#';

    if (preg_match('/#(\d+)$/', $campaign->name, $matches)) {
        $counter = (int)$matches[1];
        $counter++;
        $campaign->name = (string)preg_replace('/#(\d+)$/', $prefix . $counter, $campaign->name);
    } else {
        $campaign->name .= ' ' . $prefix . '1';
    }

    $campaign->save(false);

});

2. The second one adds 2 to prefix, so #2, #4,#6

PHP:
<?php

hooks()->addAction('copy_campaign', function(CAttributeCollection $collection){
    /** @var Campaign $campaign **/
    $campaign = $collection->itemAt('copied');
 
    $prefix = '#';

    if (preg_match('/'.$prefix.'(\d+)$/', $campaign->name, $matches)) {
        $counter = (int)$matches[1];
        $counter++;
        $campaign->name = (string)preg_replace('/'.$prefix.'(\d+)$/', $prefix . $counter, $campaign->name);
    } else {
        $campaign->name .= ' ' . $prefix . '1';
    } 

    $campaign->save(false);

});


3. How to get rid of the space before the prefix?

Thank you!
 
3. How to get rid of the space before the prefix?
After the if/else, before $campaign->save(false); you can add this to remove space.
PHP:
$campaign->name = str_replace(' ', '', $campaign->name);
 
Yeah, I did say it requires some tweaking here and there, yesterday I was in a hurry when I wrote the code, sorry, but here's the correct one for what you need:

PHP:
<?php

hooks()->addAction('copy_campaign', function(CAttributeCollection $collection){
    /** @var Campaign $campaign **/
    $campaign = $collection->itemAt('copied');
 
    if (preg_match('/#(\d+)$/', $campaign->name, $matches)) {
        $counter = (int)$matches[1];
        $campaign->name = (string)preg_replace('/#(\d+)$/', $counter, $campaign->name);
    }
    
    // remove double white space
    $campaign->name = str_replace('  ', ' ', $campaign->name);

    $campaign->save(false);

});
 
Sorry, it still doesn't work, it adds {space}1, but after another copy, it adds an additional {space}1, and so on...

So there is still {space}, and it doesn't increment the number o_O

1693381605012.png
 
I see what you mean, here, take this ;)
PHP:
hooks()->addAction('copy_campaign', function(CAttributeCollection $collection){

    /** @var Campaign $initialCampaign **/
    $initialCampaign = $collection->itemAt('campaign');
    
    /** @var Campaign $newCampaign **/
    $newCampaign = $collection->itemAt('copied');
    
    $newCampaign->name = $initialCampaign->name;
    
    if (preg_match('/(#)?(\d+)$/', $newCampaign->name, $matches)) {
        $counter = (int)$matches[2] + 1;
        $newCampaign->name = (string)preg_replace('/(#)?(\d+)$/', $counter, $newCampaign->name);
    } else {
        $newCampaign->name .= ' 1';
    }

    // remove double white space
    $newCampaign->name = str_replace('  ', ' ', $newCampaign->name);

    $newCampaign->save(false);

});
 
Still it doesn't do it :-(

1. Initial copy adds nothing and all the next copies nothing as well
2. Second copy (from already saved copy from a few days ago) it adds {space} + incremented number

Sorry to bother you, I understand if you do not want to do it again...

EDIT: At least it increments the number at the end without the space, it is enough for me, thank you!!!
 
Back
Top