0

I create a Checkbox in the Checkout Flow, CheckoutPane. How i can send the Value of the Checkbox to a Entity in the User-Profile or the Order.

Description: I need a Checkbox in the CheckoutPane, when the Customer select the Checkbox and click submit i must save the value in a Field in the Order or Profile of the specific user with the date. When the customer select the checkbox first time, its hidden for future orders.

The checkbox is correctly printed, but cannot submit the value of the checkbox to a entity in order, or better a entity in the user-profile. I create the Field field_customer_check in the Order and try it same as in the Drupal Commerce Checkout Flow Example with comments. https://docs.drupalcommerce.org/commerce2/developer-guide/checkout/create-custom-checkout-pane

public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state->getValue($pane_form['#parents']);
  $this->order->setData('order_comment', $values['comment']);
}

enter image description here

enter image description here Here is my previous Code for the CheckoutPane:

<?php

namespace Drupal\mymodule_checkout\Plugin\Commerce\CheckoutPane;

use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce\InlineFormManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;


/**
 * Provides the completion message pane.
 *
 * @CommerceCheckoutPane(
 *   id = "agree_terms",
 *   label = @Translation("Permanent agree"),
 * )
 */
class AgreeTerms extends CheckoutPaneBase implements CheckoutPaneInterface {
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $order_id = $this->order->id();

        $pane_form['user_check_accept'] = [
          '#type' => 'checkbox', 
          '#default_value' => FALSE,
          '#title' =>$this->t('My Checkbox Title'),
          '#required' => FALSE,
          '#weight' => $this->getWeight(),
        ];

        return $pane_form;
        
  }
public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state->getValue($pane_form['#parents']);

  // How to submit the value from a Checkbox in a Boolean Field in the Order
  // Problem, field_customer_check not get any data 
  $this->order->setData('field_customer_check', $values['user_check_accept']);

}

 }

2 Answers 2

1

If I got the question right, you have 2 different use cases; first, you want to update the user field with values from the pane, and the second is to hide it if the user already filled the information in a previous purchase.

To solve the first problem, you can update the user fields by calling the current user and setting the value that you want as the following:

public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state->getValue($pane_form['#parents']);

  // Get the current user entity.
  $user = User::load(\Drupal::currentUser());

  // Set the value of the field.
  $user->FIELD_NAME->value = $form_state->getValue('THE VALUE');

  // Save the user entity.
  $user->save;

}

For the second part of the question, you need to check for the values while building the form and you can get the current user in the same way using

$user = User::load(\Drupal::currentUser()->id());

and to check for the existing value in the same way:

if (!empty($user->FIELD_NAME->value)) { DO SOMETHING }
2
  • I try it many times, i have the invisible function, it works. I get the Value from the field_revoked_consent. But i cannot save the checkbox value to the boolean-field. Maybe you have a solution ? Apr 14, 2022 at 12:11
  • The User::load() parameter is the user ID. User::load(\Drupal::currentUser()) won't work.
    – apaderno
    Apr 16, 2022 at 21:33
0

I try now many times to save it in the user field_revoke_consent, but i not bring the value from the checkbox into this field.

  public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $values = $form_state->getValue($pane_form['#parents']);

    // Get the current user entity.
    $user = User::load(\Drupal::currentUser()->id());
    $fieldconsent = ('field_revoke_consent');
     // Set the value of the field.
     $user->$fieldconsent->value = $form_state->getValue($pane_form['user_consent_accept']);
  
  
      // Save the user entity.
    $user->save;


    /*
    $pane = $form_state->getValue('agree_terms');

    $values = $form_state->getValue($pane_form['#parents']);
    
    $checked = $form_state->getValue($pane_form['user_consent_accept']['#default_value']);
 
    $user = User::load(\Drupal::currentUser()->id());

   if($checked ==['#default_value' => 1] ) {$user->field_revoke_consent->value = 1; }

   $user->field_revoke_consent->value = $form_state->getValue($pane_form['user_consent_accept']);  
  $user->save;
   */  
  } 
}
1
  • you need to remove the ->id() as it calls the user ID. just use. "$user = User::load(\Drupal::currentUser());" Apr 14, 2022 at 15:06

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.