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;
    }
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();
}
  • 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
  • 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

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.