MailChimp Campaign Content Update

夙愿已清 提交于 2019-12-05 03:02:40

MailChimp doesn't allow updating the campaign's HTML content because the campaign type is based on a template.

In order to update the HTML content, the campaign has to be set to custom HTML instead of a template. You can check the type by sending a GET API request to /campaigns or /campaigns/{campaign_id} and finding the content_type attribute in the response (documentation).

Alternatively, in the dashboard, the type can be determined by editing the design of the email. Anything under 'Code your own' is HTML and templates are of course templates.

I'm not entirely sure why the first response to a PUT request on a template campaign shows the updated content, but changing the content type should let you update as you want to.

Hope this helps!

If anyone's still looking for an answer to this.

I managed to solve the issue several weeks ago without creating the campaign via API, but actually updating it.

I used placeholders like [product_card id=123], 3 cards per block/row, all repeatable, which are wrapped in a class that I named product-card. In the MailChimp dashboard, you may still see the placeholders, but on preview and any form of preview like thumbnail, it will display correctly.

On the server, I crawl through the campaign's content, "detect" section names based on how they seemed to me in MailChimp and update each section with the content that I want.

PHP snippet below, some Yii2 stuff, but mostly plain PHP. I use $preview to display a preview of how the template would look, I know it's not visible in the function.

/**
 * @param $id - Id of the campaign
 * @param $s  - Whether to save or just preview
 *
 * @return bool
 */
function changeContent($id, $s = false)
{
    $mcCampaign          = new McCampaign();
    $mcCampaign::$itemId = $id;

    $content = $this->api->get("/campaigns/{$id}/content");

    if (!isset($content['html'])) return false;

    $template = $content['html'];

    $forgedDom = new \DOMDocument();
    $forgedDom->loadHTML($template);

    $mcSections = [];

    $finder = new \DOMXPath($forgedDom);

    $nodes = $finder->query('//td[contains(@class, "product-card")]');

    // disabling this shit in order to avoid strict errors
    libxml_use_internal_errors(true);

    $mcEditId    = 1;
    $mcEditIndex = 0;
    foreach ($nodes as $key => $node) {
        /** @var \DOMElement $node */
        $textContent = $node->textContent;
        if (!preg_match("#\[product_card id=\d+\]#", $textContent)) continue;

        $productId         = filter_var($textContent, FILTER_SANITIZE_NUMBER_INT);
        $node->textContent = false;

        $product = Product::findOne($productId);

        $productDetails = $product ? $this->renderPartial('/partials/_mc-product', [
            'product' => $product
        ]) : 'Product not found.';

        if ($key != 0) {
            if ($key % 3 == 0) {
                $mcEditId = 1;
                $mcEditIndex++;
            } else {
                $mcEditId++;
            }
        }

        $mcSections["repeat_1:{$mcEditIndex}:product_card_{$mcEditId}"] = $productDetails;


        $fragment = $forgedDom->createDocumentFragment();
        $fragment->appendXML($productDetails);

        $node->appendChild($fragment);
    }
    libxml_use_internal_errors(false);

    $preview = $forgedDom->saveHTML();

    // just in case
    /* $preview = str_replace(["\n", "\t", "\r"], "", $preview); */

    if ($s) {
        if (!empty($mcSections)) {
            $res = $this->api->put("/campaigns/{$id}/content", [
                'template' => [
                    'id'       => *template_id_here*,
                    'sections' => $mcSections
                ],
            ]);

            // forcing Mc to rebuild cache
            $this->api->get("/campaigns/{$id}/content");

            Yii::$app->session->setFlash('success', 'Done.');

            return $this->redirect(['campaign/index']);
        } else {
            Yii::$app->session->setFlash('error', 'Something went wrong.');
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!