0

I am following the tutorial for creating a custom offer at the official Drupal 8 Commerce documentation. But I'm getting an error that I pasted below when I click on any of the conditions, and none of the forms are appearing for any of the commerce built-in offers. I followed the tutorial exactly, except for extending the OrderPromotionOfferBase class instead of the OrderFixedAmountOff class. I pasted the contents of the OrderItemFixedAmountTarget class I defined for the offer below the error.

Offer fields not appearing

Error

AjaxError: 
An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /promotion/1/edit?destination=/admin/commerce/promotions&ajax_form=1
StatusText: OK
ResponseText: 
( ! ) Fatal error: Cannot declare class Drupal\my_module\Plugin\Commerce\PromotionOffer\OrderItemFixedAmountTarget, because the name is already in use in /home/usertest/workspace/drupalsite/web/modules/my_module/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountTarget.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000438056{main}(  ).../index.php:0
20.0006554920Drupal\Core\DrupalKernel->handle(  ).../index.php:19
30.00441717448Stack\StackedHttpKernel->handle(  ).../DrupalKernel.php:693
40.00441717448Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(  ).../StackedHttpKernel.php:23
50.00441718144Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(  ).../NegotiationMiddleware.php:52
60.00441718144Drupal\page_cache\StackMiddleware\PageCache->handle(  ).../ReverseProxyMiddleware.php:47
70.00441718144Drupal\page_cache\StackMiddleware\PageCache->pass(  ).../PageCache.php:85
80.00441718144Drupal\Core\StackMiddleware\KernelPreHandle->handle(  ).../PageCache.php:106
90.00562139576Drupal\Core\StackMiddleware\Session->handle(  ).../KernelPreHandle.php:47
100.00642260136Symfony\Component\HttpKernel\HttpKernel->handle(  ).../Session.php:57
110.00652260552Symfony\Component\HttpKernel\HttpKernel->handleRaw(  ).../HttpKernel.php:68
120.02877724544Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}(  ).../HttpKernel.php:151
130.02877724544Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(  ).../EarlyRenderingControllerWrapperSubscriber.php:97
140.02877727080Drupal\Core\Render\Renderer->executeInRenderContext(  ).../EarlyRenderingControllerWrapperSubscriber.php:124
150.02877727432Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}(  ).../Renderer.php:582
160.02877727432call_user_func_array:{/home/usertest/workspace/drupalsite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123}
(  ).../EarlyRenderingControllerWrapperSubscriber.php:123
170.02877727840Drupal\Core\Entity\HtmlEntityFormController->getContentResult(  ).../EarlyRenderingControllerWrapperSubscriber.php:123
180.02917849224Drupal\Core\Form\FormBuilder->buildForm(  ).../FormController.php:93
190.043110154816Drupal\Core\Form\FormBuilder->processForm(  ).../FormBuilder.php:319
200.087912157904Drupal\Core\Form\FormBuilder->rebuildForm(  ).../FormBuilder.php:626
210.087912157904Drupal\Core\Form\FormBuilder->retrieveForm(  ).../FormBuilder.php:369
220.087912160160call_user_func_array:{/home/usertest/workspace/drupalsite/web/core/lib/Drupal/Core/Form/FormBuilder.php:519}
(  ).../FormBuilder.php:519
230.087912160568Drupal\commerce_promotion\Form\PromotionForm->buildForm(  ).../FormBuilder.php:519
240.088212170728Drupal\commerce_promotion\Form\PromotionForm->buildForm(  ).../PromotionForm.php:31
250.088312171952Drupal\commerce_promotion\Form\PromotionForm->form(  ).../EntityForm.php:117
260.088312171952Drupal\commerce_promotion\Form\PromotionForm->form(  ).../PromotionForm.php:38
270.088312172728Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(  ).../ContentEntityForm.php:125
280.089312198408Drupal\commerce\Plugin\Field\FieldWidget\PluginSelectWidget->form(  ).../EntityFormDisplay.php:177
290.089312198408Drupal\commerce\Plugin\Field\FieldWidget\PluginSelectWidget->formMultipleElements(  ).../WidgetBase.php:104
300.089312198784Drupal\commerce\Plugin\Field\FieldWidget\PluginSelectWidget->formSingleElement(  ).../WidgetBase.php:202
310.089312199160Drupal\commerce\Plugin\Field\FieldWidget\PluginSelectWidget->formElement(  ).../WidgetBase.php:335
320.089312201184Drupal\commerce\Plugin\Field\FieldWidget\PluginSelectWidget->supportsConfiguration(  ).../PluginSelectWidget.php:116
330.089312201584is_subclass_of
(  ).../PluginSelectWidget.php:149
340.089312201712spl_autoload_call
(  ).../PluginSelectWidget.php:149
350.089312201840Composer\Autoload\ClassLoader->loadClass(  ).../PluginSelectWidget.php:149
360.089412202032Composer\Autoload\includeFile(  ).../ClassLoader.php:322
370.089412202272include( '/home/usertest/workspace/drupalsite/web/modules/my_module/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountTarget.php' ).../ClassLoader.php:444

OrderItemFixedAmountTarget.php

namespace Drupal\my_module\Plugin\Commerce\PromotionOffer;

use Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderPromotionOfferBase;
use Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\FixedAmountOffTrait;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * @CommercePromotionOffer(
 *   id = "my_module_fixed_amount_target",
 *   label = @Translation("Discount each matching product to fixed amount"),
 *   entity_type = "commerce_order_item",
 * )
 */ 
class OrderItemFixedAmountTarget extends OrderPromotionOfferBase {

  use FixedAmountOffTrait;

  /**
   * {@inheritdoc}
   */
  public function apply(EntityInterface $entity, PromotionInterface $promotion) {
    $this->assertEntity($entity);
    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $entity;
    $subtotal_price = $order->getSubTotalPrice();
    $amount = $this->getAmount();
    if ($subtotal_price->getCurrencyCode() != $amount->getCurrencyCode()) {
      return;
    }
    // The promotion amount can't be larger than the subtotal, to avoid
    // potentially having a negative order total.
    if ($amount->greaterThan($subtotal_price)) {
      $amount = $subtotal_price;
    }
    // Split the amount between order items.
    $amounts = $this->splitter->split($order, $amount);

    foreach ($order->getItems() as $order_item) {
      if (isset($amounts[$order_item->id()])) {
        $order_item->addAdjustment(new Adjustment([
          'type' => 'promotion',
          // @todo Change to label from UI when added in #2770731.
          'label' => t('Discount'),
          'amount' => $amounts[$order_item->id()]->multiply('-1'),
          'source_id' => $promotion->id(),
        ]));
      }
    }
  }
}

my_module.info.yml:

name: 'Custom Promotion Conditions'
type: module
description: 'Provides Custom Offers and Conditions'
core: 8.x
package: 'Custom'
dependencies:
  - commerce:commerce
  - commerce:commerce_order
  - drupal:options

my_module.module:

<?php

/**
 * @file
 * Contains my_module_custom_promotion.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function my_module_custom_promotion_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the my_module_custom_promotion module.
    case 'help.page.my_module_custom_promotion':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('My Awesome Module') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function my_module_custom_promotion_theme() {
  return [
    'my_module_custom_promotion' => [
      'render element' => 'children',
    ],
  ];
}

I've tried my best but I cannot figure out what I'm doing wrong and causing this "name already in use" error. I've searched my entire project just in case, but the text "OrderItemFixedAmountTarget" is only used in this one file.

3
  • Perhaps you have used it on a previous unsuccessful attempt and the system still thinks its there. Perhaps a workaround would be to move my_module folder temporarily out of the drupal installation, clear cache and and bring it back its place. I would also try drush entup might help.
    – GiorgosK
    Commented Jul 7, 2019 at 20:47
  • The same error happens if I change the name. If I change it to "MyNewModuleOrderItemFixedAmountTarget", it gives the error: Fatal error: Cannot declare class Drupal\my_module\Plugin\Commerce\PromotionOffer\MyNewModuleOrderItemFixedAmountTarget, because the name is already in use in...
    – user594044
    Commented Jul 7, 2019 at 21:02
  • One thing I did notice, is that if I move OrderItemFixedAmountTarget.php to the promotion/src/Plugin/Commerce/PromotionOffer/ alongside all the default offers, it does give a different error. I'm not sure, it may be how I am setting up my module that is the problem. I generated the module using Drupal CLI ./bin/drupal gm command.
    – user594044
    Commented Jul 7, 2019 at 21:06

1 Answer 1

0

Giorgosk's answer didn't fix the problem but it did lead me to notice that the name of my .yml and .module files didn't match the short name I had given the module, and that was the reason for the bizarre error messages. Thanks!

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.