2

Im trying to update an entry field programatically. I can load an entry by any field with a function i created

    /**
 * Returns an element by any attribute.
 *
 * @param int $elementId
 * @param string|null $type
 * @param string|null $localeId
 * @param string $localeId
 * @return BaseElementModel|null
 */
public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
{
    if (!$elementId)
    {
        return null;
    }

    if (!$elementType)
    {
        $elementType = craft()->elements->getElementTypeById($elementId);

        if (!$elementType)
        {
            return null;
        }
    }

    $criteria = craft()->elements->getCriteria($elementType);
    $criteria->$field = $elementId;
    $criteria->locale = $localeId;
    $criteria->status = null;
    $criteria->localeEnabled = null;
    return $criteria->first();
}

but i cant save the new values of the fields i want. This is my save code:

$entry = $this->getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
    $entry->variantSku = 1;
    craft()->entries->saveEntry($entry);

And the error is Property "Craft\EntryModel.variantSku" is not defined. I know the variantSku exists since im loading a product by that attribute and i can see it when dumping $entry->variantSku.

Complete error here: https://www.diigo.com/item/image/4u33k/972k

Did anyone achieved this?

4

After some research, i found a way to achieve this. Here is my full controller:

<?php
  namespace Craft;

  class Module_ControlController extends BaseController {
   protected $allowAnonymous = true;
public function actionChangeStock()
{
    $entry = $this->getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
    $entry->enabled = false;
    $entry->getContent()->setAttributes(array(
        'variantSku' => "Hello my friend!",
        'variantStock'  => "3"
    ));
    $success = craft()->entries->saveEntry($entry);
    die(var_dump($success));
}

/**
 * Returns an element by any attribute.
 *
 * @param int $elementId
 * @param string|null $type
 * @param string|null $localeId
 * @return BaseElementModel|null
 */
public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
{
    if (!$elementId)
    {
        return null;
    }

    if (!$elementType)
    {
        $elementType = craft()->elements->getElementTypeById($elementId);

        if (!$elementType)
        {
            return null;
        }
    }

    $criteria = craft()->elements->getCriteria($elementType);
    $criteria->$field = $elementId;
    $criteria->locale = $localeId;
    $criteria->status = null;
    $criteria->localeEnabled = null;
    return $criteria->first();
}}
| improve this answer | |
  • This will not work on Craft 3. Any idea on how to implement this login in craft 3? The documentation is not helping me out... – Radmation Jan 17 '19 at 21:41

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.