Created
June 18, 2018 12:50
-
-
Save iBobik/b64f1c11ef177384bf1d3241d5b21358 to your computer and use it in GitHub Desktop.
Drupal 8 quick&dirty webform handler what creates Commerce 2 orders and sends user to checkout. Hard coded for event registration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\registrations_processor\Plugin\WebformHandler; | |
use Drupal; | |
use Drupal\commerce\PurchasableEntityInterface; | |
use Drupal\commerce_order\Adjustment; | |
use Drupal\commerce_order\Entity\OrderInterface; | |
use Drupal\commerce_price\Price; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Url; | |
use Drupal\views\Views; | |
use Drupal\webform\Plugin\WebformHandlerBase; | |
use Drupal\webform\Plugin\WebformHandlerInterface; | |
use Drupal\webform\WebformSubmissionInterface; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
/** | |
* Creates orders on webform submissions. | |
* | |
* @WebformHandler( | |
* id = "registrations_processor", | |
* label = @Translation("Registrations processor"), | |
* category = @Translation("Commerce"), | |
* description = @Translation("Creates orders on webform submissions."), | |
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED, | |
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED, | |
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED, | |
* ) | |
*/ | |
class RegistrationProcessorWebformHandler extends WebformHandlerBase { | |
/** | |
* Check if there are enough tickets. | |
*/ | |
public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) { | |
// TODO | |
} | |
/** | |
* Populate commerce cart and go to checkout. | |
*/ | |
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) { | |
if ($update) // Process only new submissions | |
return; | |
$data = $webform_submission->getData(); | |
// Volunteer | |
if ($data['volunteer']) { | |
return; | |
} | |
// Create order | |
$order = Drupal::entityManager() | |
->getStorage('commerce_order') | |
->create(['type' => 'default', 'cart' => TRUE]) | |
->setStore(Drupal::entityManager()->getStorage('commerce_store')->loadDefault()); | |
$order->setEmail($data['email']); | |
$this->addProduct($order, $this->resolveTicket()); | |
if ($data['tshirt']) { | |
$tshirt = Drupal::entityManager()->getStorage('commerce_product_variation')->loadBySku("T-shirt"); | |
$this->addProduct($order, $tshirt); | |
} | |
if ($data['bed']) { | |
$bed = Drupal::entityManager()->getStorage('commerce_product_variation')->loadBySku("Bed"); | |
$this->addProduct($order, $bed); | |
} | |
if ($data['contrib']) { | |
$order->addAdjustment(new Adjustment([ | |
'type' => 'custom', | |
'label' => 'Contribution', | |
'amount' => new Price($data['contrib'], 'CZK'), | |
])); | |
} | |
if ($data['student']) { | |
$order->addAdjustment(new Adjustment([ | |
'type' => 'custom', | |
'label' => 'Student', | |
'amount' => new Price('-222', 'CZK'), | |
])); | |
} | |
$order->save(); | |
// Store the new cart order id in the anonymous user's session so that it | |
// can be retrieved on the next page load. | |
if (Drupal::currentUser()->isAnonymous()) { | |
Drupal::service('commerce_cart.cart_session')->addCartId($order->id()); | |
} | |
$this->redirectToCheckout($order->id()); | |
} | |
protected function resolveTicket() { | |
// List sold out tickets | |
$view = Views::getView('tickets_sales'); | |
$view->execute(); | |
$sold_out = []; | |
foreach ($view->result as $row) { | |
$id = $row->_relationship_entities['commerce_product_variation']->id(); | |
$sold = (int)$row->order_item_id; | |
$limit = (int)$row->commerce_product_variation_field_data_commerce_order_item__c; | |
if ($sold >= $limit) | |
$sold_out[$id] = TRUE; | |
} | |
$variants = Drupal::entityManager()->getStorage('commerce_product')->load(3)->getVariations(); | |
foreach ($variants as $v) { | |
// If variant is not in sold_out | |
if (!isset($sold_out[$v->id()])) | |
return $v; | |
} | |
return FALSE; | |
} | |
protected function addProduct(OrderInterface $order, PurchasableEntityInterface $product_variation) { | |
$item = Drupal::entityManager() | |
->getStorage('commerce_order_item') | |
->createFromPurchasableEntity($product_variation); | |
$item->setQuantity(1); | |
$item->save(); | |
$order->addItem($item); | |
} | |
protected function redirectToCheckout($order_id) { | |
$url = Url::fromRoute('commerce_checkout.form', ['commerce_order' => $order_id]); | |
$response = new RedirectResponse($url->toString()); | |
$request = \Drupal::request(); | |
// Save the session so things like messages get saved. | |
$request->getSession()->save(); | |
$response->prepare($request); | |
// Make sure to trigger kernel events. | |
\Drupal::service('kernel')->terminate($request, $response); | |
$response->send(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment