0

My Goal: If an order item with the same value exists in a completed order,a message notifies the costumer and he cannot pay and complete the purchase until he changes the value of that field.

I have created a new field for each order item at /admin/commerce/config/order-item-types/default/edit/fields. Then I added that field to the cart form /admin/commerce/config/order-item-types/default/edit/form-display/add_to_cart and also to the view that shows the order items of each customer /admin/structure/views/view/commerce_cart_form.The name of the field is field_unique (database:commerce_order_item__field_unique).

Before I create a constraint for the order item, I created a constraint for another post type to do my tests as that would be less complicated. So I created the post type "test" and the field field_unique (database: node__field_unique ). Everything worked perfectly for my test post type. I created the first post and I gave to the field_unique the value:unique. Then I created a second post and tried to give to the field_unique the same value but I couldn't submit the post. A message appeared notifying me that the value:unique for the field_unique already exists in another post. Everything worked as expected!

Here is the code that works for the post type "test" and the field_unique(database: node__field_unique).

mymodule/src/Plugin/Validation/Constraint/OnlyOne.php

   <?php namespace Drupal\mymodule\Plugin\Validation\Constraint;

   use Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint;
use Symfony\Component\Validator\Constraint;

/**
* Prevent duplicated values on machine_name field.
*
* @Constraint(
*   id = "only_one",
*   label = @Translation("Unique Machine Name", context = "Validation"),
* )
*/
//Here I tried to extend to Constraint and to UniqueFieldConstraint with same error.
class OnlyOne extends UniqueFieldConstraint  {
public $message = 'The field value %value already exists in another post';
}

mymodule/src/Plugin/Validation/Constraint/OnlyOneValidator.php

<?php namespace Drupal\mymodule\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class OnlyOneValidator extends ConstraintValidator {

/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
return $this->context->addViolation($constraint->message, array('%label' => 'test', '%value' => 'test'));

 }
 }

mymodule.module

 <?php 
 use Drupal\Core\Entity\EntityTypeInterface;

function mymodule_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($bundle === 'test') {
  if (isset($fields['field_unique'])) {
  //  Use the ID as defined in the annotation of the constraint definition
  $fields['field_unique']->addConstraint('only_one');
  }}}

My question is how can I make it work for the order items too? Although the machine name for both fields is the same: field_unique in the database are stored in different tables with different names. For the node field_unique (database: node__field_unique ) and for the order field_unique (database:commerce_order_item__field_unique) I tried to change the post type name with the orders name like this

 <?php 
 use Drupal\Core\Entity\EntityTypeInterface;

function mymodule_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($bundle === 'order') {
  if (isset($fields['field_unique'])) {
  //  Use the ID as defined in the annotation of the constraint definition
  $fields['field_unique']->addConstraint('only_one');
  }}} 

but it didn't work... Whatelse could I try to make the validation work for the order items?

  • It does not work for products too... Are not products considered as bundle? ` if ($bundle === 'product')` cannot catch the post type product... – Ioannis Feb 8 '18 at 15:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.