You are here

function uc_wishlist_form_alter in UC Wish List 8

Same name and namespace in other branches
  1. 6 uc_wishlist.module \uc_wishlist_form_alter()
  2. 7 uc_wishlist.module \uc_wishlist_form_alter()

Implements hook_form_alter().

Alter uc_product_add_to_cart_form instances to include 'add to wish list' button. Form submission is routed through uc_wishlist_add_to_wishlist to differentiate between 'add to cart' and 'add to wish list'. If this module is added to Ubercart core, then this can be simplified.

Alter uc_cart_checkout_form to include by default the delivery address of a wish list's owner, if items come from a single wish list.

File

./uc_wishlist.module, line 55
Allows users to create public shopping/wish lists.

Code

function uc_wishlist_form_alter(&$form, FormState $form_state, $form_id) {
  if (strpos($form_id, 'uc_product_add_to_cart_form_') === 0 || strpos($form_id, 'uc_product_kit_add_to_cart_form_') === 0) {
    $user = \Drupal::currentUser();

    // Allow users which has 'create wish lists' permissions.
    if (!$user
      ->hasPermission('create wish lists')) {
      return;
    }
    $moduleHandler = \Drupal::service('module_handler');

    // ['args'][0];.
    $product = $form_state
      ->getBuildInfo();

    // Check only stock active products, and if not, bail out.
    $config = \Drupal::config('uc_wishlist.settings');
    if (!$config
      ->get('uc_wishlist_out_of_stock')) {
      if ($moduleHandler
        ->moduleExists('uc_stock')) {
        $stock_level = uc_stock_level($product->model);
        if ($stock_level !== FALSE && $stock_level <= 0) {
          return;
        }
      }
    }
    $pid = $form['nid']['#value'];
    $wishlist_manager = \Drupal::service('uc_wishlist.manager');
    $wid = uc_wishlist_get_wid();

    // Check to see if the users wishlist has been created.
    if ($wid == NULL || empty($wid)) {

      // Add the wish list button to the add to cart form because this users wishlist hasnt been made yet.
      $form['actions']['wishlist'] = [
        '#type' => 'submit',
        '#attributes' => [
          'class' => [
            'node-add-to-wishlist',
          ],
        ],
        '#value' => t('Add to wish list'),
        '#submit' => [
          'uc_wishlist_add_to_wishlist_submit',
        ],
        '#weight' => 1,
      ];
    }
    else {

      // Make sure this product isnt already in the users wish list.
      $wishlistProduct = $wishlist_manager
        ->isProductInWishlist($wid, $pid);
      if (is_object($wishlistProduct[0])) {

        // dpm($wishlistProduct[0]);
        // the product was found in the user's wishlist so display the remove from wishlist button on the add to cart form.
        $form['actions']['wishlist'] = [
          '#type' => 'submit',
          '#attributes' => [
            'class' => [
              'node-add-to-wishlist',
            ],
          ],
          '#value' => t('Remove from wish list'),
          '#submit' => [
            'uc_wishlist_remove_from_wishlist_submit',
          ],
          '#weight' => 1,
        ];
      }
      else {

        // Product is not in the users wishlist.
        $form['actions']['wishlist'] = [
          '#type' => 'submit',
          '#attributes' => [
            'class' => [
              'node-add-to-wishlist',
            ],
          ],
          '#value' => t('Add to wish list'),
          '#submit' => [
            'uc_wishlist_add_to_wishlist_submit',
          ],
          '#weight' => 1,
        ];
      }
    }
  }

  // Checking if the product is added from wishlist in checkout page.
  if ($form_id == 'uc_cart_checkout_form') {
    if (isset($form['panes']['cart']['cart_review_table']['#items']) && !empty($form['panes']['cart']['cart_review_table']['#items'])) {
      $items = $form['panes']['cart']['cart_review_table']['#items'];
      $wids = [];
      foreach ($items as $item) {
        if (!empty($item->data['wid'])) {
          $wids[] = $item->data['wid'];
        }
      }
      $wids = array_unique($wids);
      if (count($wids) > 1) {
        drupal_set_message(t('This order contains items from multiple wish lists. It is not possible to automatically address this order for its recipient.'));
      }
      elseif (count($wids) == 1) {
        $wishlist = uc_wishlist_load($wids[0]);
        if (variable_get('uc_wishlist_save_address', TRUE) && !empty($wishlist->address->firstname) && !empty($wishlist->address->lastname) && !empty($wishlist->address->addr1) && !empty($wishlist->address->postcode) && is_object($form['panes']['delivery']['address']['#default_value']) && empty($form['panes']['delivery']['address']['#default_value']->delivery_first_name)) {
          $order = Order::load($_SESSION['cart_order']);
          if ($order) {
            $defaults = $order;
            $defaults->delivery_first_name = $wishlist->address->firstname;
            $defaults->delivery_last_name = $wishlist->address->lastname;
            $defaults->delivery_company = $wishlist->address->company;
            $defaults->delivery_street1 = $wishlist->address->addr1;
            $defaults->delivery_street2 = $wishlist->address->addr2;
            $defaults->delivery_city = $wishlist->address->city;
            $defaults->delivery_country = $wishlist->address->country;
            $defaults->delivery_zone = $wishlist->address->zone;
            $defaults->delivery_postal_code = $wishlist->address->postcode;
            $defaults->delivery_phone = $wishlist->address->phone;
            $form['panes']['delivery']['address']['#default_value'] = $defaults;
            drupal_set_message(t('This order contains items from a wish list. The delivery address has been automatically set to the preferred address from the wish list. You may change this address.'));
          }
        }
      }
    }
  }
}