5

I'm testing with the Foxycart Plugin to update the stock in my code. In the FoxyCartController.php I'm able to filter through the XML feed ok and get the new Stock value but it's doesn't seem to be getting saved.

My current code which picks up the correct product and $newQuantity value ok - but it doesn't seem to save the $newQuantity value back to the stock field in the entry.

//If you have custom code to run for each product, put it here:

$service = craft()->entries;

    $entry = $service->getEntryById($product_code);

    if ($entry != null) {
        $attrs = $entry->getContent();

        $newQuantity = $attrs['stock'] - $product_quantity;


        $entry->getContent()->setAttributes(array(
            'stock' => $newQuantity
        ));

        $service->saveEntry($entry);

        //return $entry;
    } else {
        //return null;
    }
mmc501
  • 1,779
  • 13
  • 34
  • Figured out a solution. Described here: http://craftcms.stackexchange.com/questions/11661/programmatically-update-entry-field/13210#13210 – Brian Mathews Jan 14 '16 at 03:32

1 Answers1

4

You're likely getting a validation error that you're not checking for. Do something like this:

if ($entry->validate())
{
    $service->saveEntry($entry);
}
else
{
    $errors = $entry->getErrors();
}

Update:

Do you get anything if you do:

if (!$service->saveEntry($entry))
{
    $errors = $entry->getErrors();
}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • Cheers - added that it validates ok - placed that code in and some additional to send an email and it gets validated ok. Foxycart responds with a success when the data is sent. – mmc501 Dec 23 '14 at 16:59
  • But I forgot to add - the record still isn't being updated. – mmc501 Dec 23 '14 at 20:38
  • Updated answer. – Brad Bell Dec 23 '14 at 22:21
  • Ok I have added that - I have just checked the craft.log and I see this: [warning] [application] Craft\ContentModel->typeCategory failed validation: Type Category cannot be blank, then: [trace] [system.db.CDbTransaction] Rolling back transaction. typeCategory is a required field for the product - is this the issue? – mmc501 Dec 23 '14 at 22:40
  • You've got a required custom field with a handle typeCategory, then? – Brad Bell Dec 23 '14 at 22:41
  • I just changed it to not required there and tested it - and the stock update worked fine. Should it be able to work with a required field? – mmc501 Dec 23 '14 at 22:45
  • Sure... but the definition of required means you need to give it a value. :) i.e. $entry->getContent()->setAttributes(array( 'stock' => $newQuantity, 'typeCategory' => 'something' )); – Brad Bell Dec 23 '14 at 22:46
  • Ok, the typeCategory already had a value? Can the stock not simply be updated not touching any other fields? Or do I need to pull the existing typeCategory(category field type) and set it again? – mmc501 Dec 23 '14 at 22:49
  • Are you sure about that? If you do $entry = $service->getEntryById($product_code); $value = $entry->typeCategory; does it output anything? And if you load that entry in the CP, does typeCategory have a value? Maybe there were already some existing entries and someone set typeCategory to required after the fact. – Brad Bell Dec 23 '14 at 22:53
  • Yep it def has a value - and checked it in the Entry in the control panel. When I changed it to not required and processed a Foxycart order it updated the stock and kept the typeCategory value it originally had. That last point may have been the case - that specific entry was added before the field was made required. What's the best way around that - delete the entries and make the field required? Then all should be fine? – mmc501 Dec 23 '14 at 22:59
  • Yeah... either that or go through each entry that doesn't have a value and manually add one? – Brad Bell Dec 23 '14 at 23:08
  • 1
    Cheers - they all had values though. I will delete, (only 2 test products) - thanks for the help - time to sign off for the holidays – mmc501 Dec 23 '14 at 23:11
  • @mmc501, did you ever come back from the holiday and figure this one out? Having the exact same problem now. – Matt Stein Jun 11 '15 at 00:36
  • In my case I originally only had 2 entries - when I deleted those and added again everything worked fine - sorry - no ideal solution. – mmc501 Jun 11 '15 at 06:12