Skip to content

Commit cf1f307

Browse files
committed
Initial commit
0 parents  commit cf1f307

10 files changed

+256
-0
lines changed

README.txt

Whitespace-only changes.

composer.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "drupal/minimum_maximum_order",
3+
"type": "drupal-module",
4+
"description": "Validate Order Based On Minimum and Maximum Total",
5+
"keywords": ["Drupal"],
6+
"license": "GPL-2.0+",
7+
"homepage": "https://www.drupal.org/project/minimum_maximum_order",
8+
"minimum-stability": "dev",
9+
"support": {
10+
"issues": "https://www.drupal.org/project/issues/minimum_maximum_order",
11+
"source": "http://cgit.drupalcode.org/minimum_maximum_order"
12+
},
13+
"require": { }
14+
}

minimum_maximum_order.info.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Minimum Maximum Order'
2+
type: module
3+
description: 'Validate Order Based On Minimum and Maximum Total'
4+
core: 8.x
5+
package: 'Custom'
6+
dependencies:
7+
- drupal:commerce

minimum_maximum_order.links.menu.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
minimum_maximum_order.admin_settings_form:
2+
title: 'Minimum Maximum Order Configuration'
3+
route_name: minimum_maximum_order.admin_settings_form
4+
description: 'Minimum maximum order admin'
5+
parent: system.admin_config_system
6+
weight: 99

minimum_maximum_order.module

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains minimum_maximum_order.module.
6+
*/
7+
8+
use Drupal\Core\Routing\RouteMatchInterface;
9+
use Drupal\Core\Form\FormStateInterface;
10+
11+
/**
12+
* Implements hook_help().
13+
*/
14+
function minimum_maximum_order_help($route_name, RouteMatchInterface $route_match) {
15+
switch ($route_name) {
16+
// Main module help for the minimum_maximum_order module.
17+
case 'help.page.minimum_maximum_order':
18+
$output = '';
19+
$output .= '<h3>' . t('About') . '</h3>';
20+
$output .= '<p>' . t('Validate Order Based On Minimum and Maximum Total') . '</p>';
21+
return $output;
22+
23+
default:
24+
}
25+
}
26+
27+
function minimum_maximum_order_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
28+
if (strpos($form_id,"views_form_commerce_cart_form_default_") !== false) {
29+
$minimum = $config = \Drupal::config('minimum_maximum_order.adminsettings')->get('minimum_order');
30+
$order_id = $form['output'][0]['#view']->footer['commerce_order_total']->view->args[0];
31+
$orders = \Drupal::entityTypeManager()
32+
->getStorage('commerce_order')
33+
->loadByProperties(['order_id' => $order_id]);
34+
$order = reset($orders);
35+
$total_price = $order->getTotalprice()->getNumber();
36+
if ($total_price < $minimum) {
37+
// make checkout button as disabled
38+
$form['actions']['checkout']['#attributes']['disabled'] = true;
39+
$form['#prefix'] = '<div class="alert alert-danger" role="alert">$35 Minimum not met</div>';
40+
41+
}
42+
}
43+
}
44+

minimum_maximum_order.routing.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
minimum_maximum_order.admin_settings_form:
2+
path: '/admin/config/minimum_maximum_order/adminsettings'
3+
defaults:
4+
_form: '\Drupal\minimum_maximum_order\Form\MinimumMaximumOrderForm'
5+
_title: 'MinimumMaximumOrderForm'
6+
requirements:
7+
_permission: 'access administration pages'
8+
options:
9+
_admin_route: TRUE

minimum_maximum_order.services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
minimum_maximum_order_validate_min_max_subscriber:
3+
class: '\Drupal\minimum_maximum_order\EventSubscriber\ValidateMinMaxSubscriber'
4+
tags:
5+
- { name: 'event_subscriber' }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Drupal\minimum_maximum_order\EventSubscriber;
4+
5+
use Drupal\commerce_order\Event\OrderItemEvent;
6+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
use Drupal\state_machine\Event\WorkflowTransitionEvent;
8+
9+
/**
10+
* Class EntityTypeSubscriber.
11+
*
12+
* @package Drupal\custom_events\EventSubscriber
13+
*/
14+
class ValidateMinMaxSubscriber implements EventSubscriberInterface {
15+
16+
17+
/**
18+
* {@inheritdoc}
19+
*
20+
* @return array
21+
* The event names to listen for, and the methods that should be executed.
22+
*/
23+
public static function getSubscribedEvents() {
24+
return [
25+
'commerce_order.place.pre_transition' => ['validateMinMax', -10],
26+
];
27+
}
28+
29+
public function validateMinMax(WorkflowTransitionEvent $event) {
30+
$order = $event->getEntity();
31+
$total = (int) $order->getTotalPrice()->getNumber();
32+
$min = 30;
33+
$max = null;
34+
35+
if ( $min !== null && $total < $min) {
36+
return false;
37+
} elseif ( $max !== null && $total > $max) {
38+
39+
}
40+
}
41+
42+
}

src/Form/MinimumMaximumOrderForm.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains Drupal\minimum_maximum_order\Form\MinimumMaximumOrderForm.
5+
*/
6+
namespace Drupal\minimum_maximum_order\Form;
7+
use Drupal\Core\Form\ConfigFormBase;
8+
use Drupal\Core\Form\FormStateInterface;
9+
10+
class MinimumMaximumOrderForm extends ConfigFormBase {
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
protected function getEditableConfigNames() {
16+
return [
17+
'minimum_maximum_order.adminsettings',
18+
];
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function getFormId() {
25+
return 'minimum_maximum_order_form';
26+
}
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function buildForm(array $form, FormStateInterface $form_state) {
31+
$config = $this->config('minimum_maximum_order.adminsettings');
32+
33+
$form['minimum_order'] = [
34+
'#type' => 'number',
35+
'#title' => $this->t('Minimum Order'),
36+
'#description' => $this->t('Minimum order amount needed to go to checkout page'),
37+
'#default_value' => $config->get('minimum_order')
38+
];
39+
40+
$form['maximum_order'] = [
41+
'#type' => 'number',
42+
'#title' => $this->t('Maximum Order'),
43+
'#description' => $this->t('Maximum order amount needed to go to checkout page'),
44+
'#default_value' => $config->get('maximum_order'),
45+
];
46+
47+
return parent::buildForm($form, $form_state);
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function validateForm(array &$form, FormStateInterface $form_state) {
54+
$min = $form_state->getValue('minimum_order');
55+
$max = $form_state->getValue('maximum_order');
56+
if (!empty($min)) {
57+
if ($min < 0) {
58+
$form_state->setError($form['minimum_order'],'Value for minimum order must be greater than 0');
59+
}
60+
}
61+
if (!empty($max)) {
62+
if ($form_state->getValue('maximum_order') < 0) {
63+
$form_state->setError($form['maximum_order'],'Value for maximum order must be greater than 0');
64+
}
65+
}
66+
if (!empty($min) && !empty($max) && $min >= $max) {
67+
$form_state->setError($form['minimum_order'],'Value for minimum order must be less than the maximum order');
68+
}
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function submitForm(array &$form, FormStateInterface $form_state) {
75+
76+
parent::submitForm($form, $form_state);
77+
78+
$this->config('minimum_maximum_order.adminsettings')
79+
->set('minimum_order', $form_state->getValue('minimum_order'))
80+
->set('maximum_order', $form_state->getValue('maximum_order'))
81+
->save();
82+
}
83+
}

tests/src/Functional/LoadTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Drupal\Tests\minimum_maximum_order\Functional;
4+
5+
use Drupal\Core\Url;
6+
use Drupal\Tests\BrowserTestBase;
7+
8+
/**
9+
* Simple test to ensure that main page loads with module enabled.
10+
*
11+
* @group minimum_maximum_order
12+
*/
13+
class LoadTest extends BrowserTestBase {
14+
15+
/**
16+
* Modules to enable.
17+
*
18+
* @var array
19+
*/
20+
public static $modules = ['minimum_maximum_order'];
21+
22+
/**
23+
* A user with permission to administer site configuration.
24+
*
25+
* @var \Drupal\user\UserInterface
26+
*/
27+
protected $user;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function setUp() {
33+
parent::setUp();
34+
$this->user = $this->drupalCreateUser(['administer site configuration']);
35+
$this->drupalLogin($this->user);
36+
}
37+
38+
/**
39+
* Tests that the home page loads with a 200 response.
40+
*/
41+
public function testLoad() {
42+
$this->drupalGet(Url::fromRoute('<front>'));
43+
$this->assertSession()->statusCodeEquals(200);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)