1

Appreciate I may be asking for a lot here - I have an existing module that I use to fire events when a stripe order is completed.

I would like to iterate through some User matrix blocks and update a dropdown field value (I have the IDs of the blocks from the stripe order)

This is far as I have got I'm afraid.. happy to look at some docs or be pointed in the right direction. I have other code in the module that does similar to entries, i just cannot work out how to find the matrix blocks by user + ID, and then update the dropdown field.

thanks

Event::on(Orders::class, Orders::EVENT_AFTER_ORDER_COMPLETE, function(OrderCompleteEvent $e) {
            
            // get the order
            $order = $e->order;
            
            //get the fields from the order
            $fields= $order->getFormFields();
            

            // TROPHY ORDERS
            if (!empty($fields['Trophies'])) {
                
                //create array of ids from Trophies field in the order
                $blockIds = explode(',', $fields['Trophies']);

             // Get the user from the order
            $user = Craft::$app->getUsers()->getUserByUsernameOrEmail($order->email);
                
                ///do something here to find matrix blocks with the above ID's and then update a dropdown in each one to "paid"
                
                // save
                return Craft::$app->getElements()->saveElement($user, false);
                
            }           
             
         });

1 Answer 1

1

If you only need to update a field value in an existing matrix block, not add new ones or remove existing ones, you don't need the user at all. Matrix blocks are elements themselves, so you can save them independently from their owner element.

Since you already have the ID of the matrix block, you can use a matrix block query to find that block:

use craft\elements\MatrixBlock;

$blockId = 123;
$matrixBlock = MatrixBlock::find()
    ->id($blockId)
    ->one();

Then you can modify the field values on that matrix block and save it using the Elements service:

use Craft;

$matrixBlock->setFieldValues([
    'body' => 'Foo',
    'my_field' => 'Bar', 
]);
$success = Craft::$app->getElements()->saveElement($matrixBlock);
if (!$success) {
    // Catch validation errors
}

If you also need to add or remove blocks, you'll need the User element as well, you can always find that through $matrixBlock->owner. The documentation on saving matrix fields has examples for adding new blocks and updating existing ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.