SHARE
TWEET

Untitled

a guest Jun 19th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. use DrupalCoreFormFormStateInterface;
  5. use Drupalcommerce_orderEntityOrder;
  6. use SymfonyComponentHttpFoundationRedirectResponse;
  7.  
  8. /**
  9.  * Implements hook_form_FORM_ID_alter().
  10.  */
  11. function commerce_custom_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  12.    $form['actions']['submit']['#value']='Sponsor';
  13.    $form['#validate'][] = 'commerce_custom_form_validate';
  14.    $form['actions']['submit']['#submit'][] = 'commerce_custom_cart_alter_redirect';
  15. }
  16.  
  17. function commerce_custom_form_validate(array &$form, FormStateInterface $form_state) {
  18.  
  19.     $user = Drupal::currentUser()->id();
  20.     $connection = Drupal::database();
  21.     $query = $connection->query("select * from commerce_order_item join commerce_order on commerce_order_item.order_id = commerce_order.order_id where uid = :user and state = 'draft'", [':user' => $user]);
  22.     $result = $query->fetchAll();
  23.     //dpm($result);
  24.  
  25.     $cart_count = $result[0]->quantity;
  26.     $order_id = $result[0]->order_id;
  27.  
  28.     if ($cart_count > 0 && $order_id) {
  29.         $order = Drupal::entityTypeManager()->getStorage('commerce_order')->load($order_id);
  30.         $items = $order->getItems();
  31.         foreach($items as $item_key => $item) {
  32.             $order->removeItem($item);
  33.             $item->delete();
  34.         }
  35.     }
  36. }
  37.  
  38. function commerce_custom_cart_alter_redirect(&$form, FormStateInterface $form_state) {
  39.   $responce =  new RedirectResponse('/how-it-works');
  40.   $responce->send();
  41. }
  42.      
  43. <?php
  44.  
  45. namespace Drupalcommerce_customPluginBlock;
  46. use DrupalCoreBlockBlockBase;
  47.  
  48. class ContinueCheckoutBlock extends BlockBase {
  49.  
  50.    public function build() {
  51.  
  52.         $user = Drupal::currentUser()->id();
  53.         $connection = Drupal::database();
  54.         $query = $connection->query("select * from commerce_order_item join commerce_order on commerce_order_item.order_id = commerce_order.order_id where uid = :user and state = 'draft'", [':user' => $user]);
  55.         $result = $query->fetchAll();
  56.         $order_id = $result[0]->order_id;
  57.  
  58.         return array(
  59.             '#type' =>'markup',
  60.             '#markup' => '<a href=checkout/' . $order_id . '/order_information class=button>Continue</a>',
  61.         );
  62.    }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top