Enable multiple select dropdown in Google Sheets

I was looking for a way to multi select items from a dropdown in google sheets due to a client request and I’ve found several tutorials, one in particular i’ve found it very helpful but incomplete at the same time: https://www.lido.app/tutorials/google-sheets-multi-select-dropdown

The script included in this tutorial wasn’t 100% completed, so i took 15 min of my time to make it work in a way i thought it was convenient at that moment. What I needed was to remove an option if was already selected, same behaviour of a select2 dropdown or kinda.

So I’ve changed the code from the tutorial to the following:

function onEdit(e) {

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var activeCell = ss.getActiveCell();

    if ( 
          (activeCell.getColumn() == 8 || activeCell.getColumn() == 11) 
          && ss.getActiveSheet().getName() == "Sheet1"
        ) 
    {

        var newValue = e.value;

        var oldValue = e.oldValue;

        if (!newValue) {

            activeCell.setValue("");

        } else {

            if (!oldValue) {
                activeCell.setValue(newValue);
            } else {

                let opts = oldValue.split(', ')
                if( opts.includes(newValue) ){
                  console.log('value is in array. Remove then.') 
                  // new validation
                    var list = [];
                    opts.forEach(function(item, index){
                      if( item !== newValue ){
                        list.push(item)
                      }
                    })
                    console.log('list:', list)
                  // end new validation
                  console.log('newValue', newValue)
                  newValue = list.join(', ')
                }else{
                  newValue = oldValue + ', ' + newValue;
                }
                activeCell.setValue(newValue);

            }

        }

    }

}

Now if i select an option that is already selected, would be removed from the selection.

Hope this helps!.

Add “Skip” button to Woocommerce Subscriptions

I had a client with a request that i didn’t thought until I speak with him, he told me that clients on their website should be able to skip one month of subscription but keeping the subsscription as Active. That means we cannot change the status of the subscription.

Well in case you need it for any reason, or a cllient request like me. You can achieve this with the following code snippet in your theme/plugin:

<?php

add_filter( 'wcs_view_subscription_actions', 'add_skip_action', 10, 3 );
    
public function subscription_skip_handler(){

		global $post;

		// If the current user doesn't own the subscription, remove the query arg from the URL
		if ( isset( $_GET['subscription_id'] ) ) {

			$subscription = wcs_get_subscription( absint( $_GET['subscription_id'] ) );

			// Visiting a switch link for someone elses subscription or if the switch link doesn't contain a valid nonce
			if ( ! is_object( $subscription ) || empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), $subscription->get_id() . '_skip' ) ) {

				wp_redirect( remove_query_arg( array( 'skip' ) ) );
				exit();

			} else {

                $date = date('Y-m-d H:i:s', $subscription->get_time( 'next_payment' ));
                $new_date = date('Y-m-d H:i:s', strtotime($date. ' + 1 months') );  

                try {

                    $subscription->update_dates( ['next_payment' => $new_date], 'gmt' );
                    
                    wp_cache_delete( $subscription->get_id(), 'posts' );
                        
                    wc_add_notice( "Your next renewal payment is in: {$new_date}", 'success' );

                    wp_redirect( remove_query_arg( array( 'skip', 'subscription_id', '_wpnonce' ) ) );

                } catch ( \Exception $e ) {
                    wcs_add_admin_notice( $e->getMessage(), 'error' );
                }

            }
        
        }

    }

What it dows is to add 1 month to the next subscription renewal and tada!. Happy Clients.