Jump to content

jarvis

Members
  • Posts

    543
  • Joined

  • Last visited

Posts posted by jarvis

  1. HI All,

    I'm hoping someone can help!

    I need to either remove or change a DIV tag directly above another. Here's the code:

    <div style="clear:both;"></div>
    <div class="berocket_lgv_widget" style="float:left;padding: 5px 0px 5px 0px;">

    Either the entire first DIV needs removing or change the style to clear:none

    I thought something like:

    $(".berocket_lgv_widget").prev().attr("style", " clear:none");
    $(".berocket_lgv_widget").prev().remove();

    No joy with either. I also tried before()

    Any help is much appreciated

    Thanks

  2. Apologies, why is it by posting I managed to work these things out

    Each of my posts when created gets the company ID assigned. So rather than query post by author ID, I queried for all posts linked by company ID

    This worked! More so because the tests I were doing were based on posts written by the primary user ID, not any other user IDs with the same company ID

    Problem solved!

  3. Hi All, 

    I really hope someone can help me on this.

    Basically, I'm trying to disable a bunch of accounts linked by a custom field value of company_id. It toggles a custom field of enable_account from 1 to 0 - this part works fine.

    What I also need to do, is once the user is disabled, locate any posts by the user (author ID) and set it from publish to draft. Now, this is oddly the part not working! If I dump the code out in any file its fine, however, once I put the exact code back to my functions file, it fails.

    So I have the disable link on a template like so:

    <a href="#" data-id="<?php echo $user_info->ID; ?>" data-nonce="<?php echo wp_create_nonce('my_disable_account_nonce') ?>" class="disable-account">Disable</a>

    It s handled by this:

    	$('.disable-account').click(function(){    
    	if (confirm('Are you sure you want to disable this account?')) {								 
            var action = 'disable_account';				
            var id = $(this).data('id');
    		var nonce = $(this).data('nonce');
    		//alert('ID: '+id+' Nonce: '+nonce);
    		$.ajax({
    			type        : "POST",
    			data		: { action:action, id:id, nonce:nonce},
    			dataType	: "html",
    			url			: '<?php echo admin_url('admin-ajax.php');?>', 
    			success     : function(data) {
    				//alert(this.data);
    				jQuery("#table_data").html(data);
    				console.log("action:" + action + " id: " + id + " nonce: " + nonce); //debug
    				//window.location.reload();
    			},
    			error       : function(xhr, status, error) {
    				var err = eval("(" + xhr.responseText + ")");
    				alert(err.Message);
    			}
    		});		
    	}
    	});

    Finally, here's the function:

    function disable_account() {
    
    	$permission = check_ajax_referer( 'my_disable_account_nonce', 'nonce', false );
    	if( $permission == false ) {
    		echo '<td colspan="7">Error!</td>';
    	} else {
    		
    		$response = '';
    		
    		// Get the company ID
    		$company_id = get_field( 'company_id', 'user_'.$_REQUEST['id'] );
    		
    		$response .= '<p>Company ID: '.$company_id.'</p>';
    		
    		// Update the main user
    		update_user_meta( $_REQUEST['id'], 'enable_account', '0' );			
    		
    		$response .= '<p>Primary User ID: '.$_REQUEST['id'].'</p>';
    
    		$user_args = array(
    			'role__in'	=> array('subscriber', 'staff_member'),
    			'orderby' 	=> 'meta_value',
    			'meta_key'	=> 'company_name',
    			'order' 	=> 'ASC',
    			'meta_query'=>
    			array(
    				array(
    					'relation'	=> 'AND', # OR						
    					array(
    						'key' 		=> 'enable_account',
    						'value' 	=> '1',
    						'compare'	=> "=",
    					),
    					array(
    						'key' 		=> 'company_id',
    						'value' 	=> $company_id,
    						'compare'	=> "=",
    					)						
    				)
    			)	
    		);
    		$users  = get_users( $user_args );
    		if ($users):
    		
    			foreach ( $users as $user ) :
    				$user_info = get_userdata( $user->ID );
    				
    				// Update the main user
    				update_user_meta( $user->ID, 'enable_account', '0' );	
    				
    				#echo '<p>User ID: '.$user->ID.'</p>';
    				$response .= '<p>User ID: '.$user->ID.'</p>';
    				
    				global $post;
    				
    				// Get all posts by author ID
    				$myposts = get_posts( array(
    					'post_status'	=> 'publish',
    					'posts_per_page'=> -1,
    					'author'		=> $user->ID
    				));
    				 
    				if ( $myposts ) {
    					foreach ( $myposts as $post ) : 
    					setup_postdata( $post ); 
    						$response .= '<p>Post ID: '.$post->ID.'</p>';
    					endforeach;
    					wp_reset_postdata();
    				}				
    				
    				
    			endforeach;
    			
    		else:	
    
    			
    		endif;	
    		
    		echo $response;
    
    		
    	}			
    	
    	die();
    
    }
    // Fire AJAX action for logged in users only
    add_action('wp_ajax_disable_account', 'disable_account');

    As I say, using this in an ordinary template works.

    For now, I've removed the code to change the post from publish to draft as at this stage, its not even displaying the Post ID, so I know its not even getting this far!

    Have I missed something obvious?

    Thanks

  4. Hi All,

    Am having a "can't see the wood for the trees" moment

    I have a php redirect file which contains my URLs, like so:

    return [	
    	'/shop/' => '/products/',
    ];

    I then have a functions file which contains:

    function 301_redirects(){
        $redirects = include __DIR__ . '/redirects.php';
    
        if (isset($redirects[$_SERVER['REQUEST_URI']])) {
            header("HTTP/1.1 301 Moved Permanently");
            header('location: ' . $redirects[$_SERVER['REQUEST_URI']]);
            exit;
        }
    
        if (isset($redirects[rtrim($_SERVER['REQUEST_URI'], '/')])) {
            header("HTTP/1.1 301 Moved Permanently");
            header('location: ' . $redirects[rtrim($_SERVER['REQUEST_URI'], '/')]);
            exit;
        }
    										
    }

    If the URL is https://www.mydomain.com/shop then the redirect won't work

    If the URL is https://www.mydomain.com/shop/ then the redirect works

    What am I missing? 

    Thanks

  5. I "think" I may have sussed this:

    add_filter( 'gform_pre_render_2', 'applicant_2_previous_address_history' );
    add_filter( 'gform_pre_validation_2', 'applicant_2_previous_address_history' );
    function applicant_2_previous_address_history( $form ) {
    
    	#get the value from the form date input d/m/Y so need to change
    	$get_moved_in_date = rgpost( 'input_1' );
    	if ( $get_moved_in_date ) {
    		#convert the date format
    		$moved_in_date = date_format(date_create_from_format('d/m/Y', $get_moved_in_date), 'd-m-Y'); 	
    		#echo "New date format is: ".$moved_in_date. "<br/>"; 
    	}
    	
    	#get todays date
    	$today = date("d-m-Y");
    	#echo "<h3>Today: ".$today."</h3>";
    	
    	#calculate the difference in days
    	$dateDiff = dateDiffInDays($today, $moved_in_date); 	
    	#printf("<p>Difference between two dates: " . $dateDiff . " Days</p>"); 	
    	
    	
    	if ( $dateDiff < 1095 ) {
    		
    		#$flag = 'true';
    		
    		# get the input from the list
    		$list_values = rgpost( 'input_2' );
    		$i = 0; #use this to get even rows only due to the way data is stored :(
    		$sum = 0; #use this to increment the no. of rows in the list
    		$previous = null; #use this to calc date difference between rows
    		$days_sum = $dateDiff; #set the total number of days. Don't start at 0, start from current date - time at current residence		
    		
    			if ( $list_values ) {
    				foreach ( $list_values as $key => $value ){
    					#get even rows only
    					if($i%2 == 0){
    						
    						$sum = $sum + 1; #increment our rows
    						
    						if ( $value ) {
    							#convert the date format
    							$additional_date = date_format(date_create_from_format('d/m/Y', $value), 'd-m-Y');
    							echo "<h3>Date: ".$additional_date."</h3>";
    						}			
    								
    						if ( $previous !== null ){ # if it's null, we're in the first loop
    						
    							#convert the date format
    							$previous_date = date_format(date_create_from_format('d/m/Y', $previous), 'd-m-Y');
    							
    							$dateDiff2 = dateDiffInDays($additional_date, $previous_date); 	
    							printf("<p>Next Iteration Difference between " . $additional_date . " and " . $previous_date . " is: " . $dateDiff2 . " Days</p>"); 	
    							
    							$days_sum = $days_sum + $dateDiff2;
    							
    						} else {
    							
    							#convert the date format
    							$dateDiff1 = dateDiffInDays($additional_date, $moved_in_date); 	
    							printf("<p>1st Iteration Difference between " . $additional_date . " and " . $moved_in_date . " is: " . $dateDiff1 . " Days</p>"); 	
    							
    							$days_sum = $days_sum + $dateDiff1;
    							
    						}
    						
    						$previous = $value; # set the current value, so it will be saved for the next iteration
    			
    					}
    					$i++;
    				} #end foreach $list_values
    			} #endif $list_values
    			#$sum = $sum + 1; #increment our rows
    			echo "<p>Total no of days = " .$days_sum.'</p>';		
    			
    			if ( $days_sum < 1095 ) {
    				echo '<p>Not enough days so loop through list</p>';
    				$sum = $sum + 1; #increment our rows
    				$flag = 'true';
    			}
    				
    	} else {
    		echo '<p>Were ok!</p>';
    		$flag = 'false';	
    	}		
    
    	#we moved in less than 3 years ago, so get history
    	if ( $flag == 'false' ) {		
            return $form;
        } 
    		
    	echo "<p>The sum of array element is = " .$sum.'</p>';
    	
        foreach ( $form['fields'] as &$field ) {
            if ( $field->id == 2 ) {
                $field->isRequired = true;
            }
    
    		
    		if ( $field->id == 3 ) {
    			$_POST['input_3'] = $sum;
    		}
    		
        }
    
        return $form;
    	
    }

    From initial testing, this now seems to work!

  6. Hi All,

    I'm hoping someone can help as I've got myself in a muddle on some code.

    Basically, the form has a date field (date moved in).

    If the date entered is over 3 years old, all's ok

    However, if the date entered is less than 3 years old, the user needs to complete a list field. The list field is essentially a repeater field with a date and address field.

    If the user adds a date and address and this (plus the original date) is more than 3 years combined history - happy days!

    If the user adds a date and address and they've still not hit 3 years history, then I need to increase the number of rows (this is handled by another function which works by incrementing a field on the form.

    As it stands, it works until I need another field to appear i.e. they've entered the first date, then entered a date within the list/repeater field but still not got 3 years worth.

    I think it's more or less there but seem to have muddled some of the logic (I think/hope!). A fresh pair of eyes would be great:

    Here's my code:

    function dateDiffInDays($date1, $date2) { 
        # Calulating the difference in timestamps 
        $diff = strtotime($date2) - strtotime($date1); 
          
        # 1 day = 24 hours 
        # 24 * 60 * 60 = 86400 seconds 
        return abs(round($diff / 86400)); 
    } 
    
    add_filter( 'gform_pre_render_2', 'applicant_2_previous_address_history' );
    add_filter( 'gform_pre_validation_2', 'applicant_2_previous_address_history' );
    function applicant_2_previous_address_history( $form ) {
    
    	#get the value from the form date input d/m/Y so need to change
    	$get_moved_in_date = rgpost( 'input_1' );
    	if ( $get_moved_in_date ) {
    		#convert the date format
    		$moved_in_date = date_format(date_create_from_format('d/m/Y', $get_moved_in_date), 'd-m-Y'); 	
    		#echo "New date format is: ".$moved_in_date. "<br/>"; 
    	}
    	
    	#get todays date
    	$today = date("d-m-Y");
    	#echo "<h3>Today: ".$today."</h3>";
    	
    	#calculate the difference in days
    	$dateDiff = dateDiffInDays($today, $moved_in_date); 	
    	#printf("<p>Difference between two dates: " . $dateDiff . " Days</p>"); 	
    
    	#check if we moved in less than 3 years ago
    	if ( $dateDiff < 1095 ) {
    		
    		#less than 3 years, so need to loop through the list fields, compare dates and tally totals days
    		if ( $days_sum < 1095 ) {
    			
    			# get the input from the list
    			$list_values = rgpost( 'input_2' );
    			$i = 0; #use this to get even rows only due to the way data is stored :(
    			$sum = 0; #use this to increment the no. of rows in the list
    			$previous = null; #use this to calc date difference between rows
    			$days_sum = $dateDiff; #set the total number of days. Don't start at 0, start from current date - time at current residence
    			if ( $list_values ) {
    				foreach ( $list_values as $key => $value ){
    					#get even rows only
    					if($i%2 == 0){
    						
    						#$sum = $sum + 1; #increment our rows
    						
    						if ( $value ) {
    							#convert the date format
    							$additional_date = date_format(date_create_from_format('d/m/Y', $value), 'd-m-Y');
    							#echo "<h3>Date: ".$additional_date."</h3>";
    						}			
    								
    						if ( $previous !== null ){ # if it's null, we're in the first loop
    						
    							#convert the date format
    							$previous_date = date_format(date_create_from_format('d/m/Y', $previous), 'd-m-Y');
    							
    							$dateDiff2 = dateDiffInDays($additional_date, $previous_date); 	
    							printf("<p>Next Iteration Difference between " . $additional_date . " and " . $previous_date . " is: " . $dateDiff2 . " Days</p>"); 	
    							
    							$days_sum = $days_sum + $dateDiff2;
    							
    						} else {
    							
    							#convert the date format
    							$dateDiff1 = dateDiffInDays($additional_date, $moved_in_date); 	
    							printf("<p>1st Iteration Difference between " . $additional_date . " and " . $moved_in_date . " is: " . $dateDiff1 . " Days</p>"); 	
    							
    							$days_sum = $days_sum + $dateDiff1;
    							
    						}
    						
    						$previous = $value; # set the current value, so it will be saved for the next iteration
    			
    					}
    					$i++;
    				} #end foreach $list_values
    			} #endif $list_values
    			$sum = $sum + 1; #increment our rows
    			echo "<p>Total no of days = " .$days_sum.'</p>';
    			
    		} #endif $days_sum < 1095	
    	
    		echo '<p>Not enough days so loop through list</p>';
    		$flag = 'true';
    		
    	} else {
    		
    		echo '<p>Were ok!</p>';
    		$flag = 'false';	
    		
    	} #endif $dateDiff < 1095
    
    
    
    	/*
    	if ( $dateDiff < 1095 ) {
    		$value = 'yes';
    	} else {
    		$value = 'no';	
    	}	
    	*/
    
    	#we moved in less than 3 years ago, so get history
    	if ( $flag == 'false' ) {		
            return $form;
        } 
    
        foreach ( $form['fields'] as &$field ) {
            if ( $field->id == 2 ) {
                $field->isRequired = true;
            }
    		echo "<p>The sum of array element is = " .$sum.'</p>';
    		if ( $field->id == 3 ) {
    			$_POST['input_3'] = $sum;
    		}
    		
        }
    
        return $form;
    	
    }

    Thanks

  7. Hi All,

    I have a Bootstrap (v4) carousel. It has numerous slides but one slide has a video. For example:

    <div class="carousel-item" data-interval="10000">
    	<video id="player" loop>
    		<source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4"><br>
    		Your browser does not support the video tag.
    	</video>
    	<div class="carousel-caption d-md-block">
    		<h3 data-animation="animated flipInX">
    		This is the caption for slide 4
    		</h3>
    	</div>
    </div>

    What I'm trying to do, is set the video to play only when you're on that slide (active) and pause when you either scroll away (via a click on the carousel arrows/indicators or the carousel interval).

    Therefore, I will use the event functionality (https://getbootstrap.com/docs/4.0/components/carousel/#events). As I'm using HTML 5 video tags, this is my solution:

    $('#carousel').carousel().on('slide.bs.carousel',function(){
    	$('.carousel-item').find('video').each(function(){
    		this.pause();
    	});
    });	
    $('#carousel').carousel().on('slide.bs.carousel',function(){
    	$('.carousel-item active').find('video').each(function(){
    		this.play();
    	});
    });		

    Unfortunately, it doesn't seem to work but I can't see why? Am I missing something very obvious??

    Many thanks in advanced

  8. Hi @cyberRobot

     

    It's Wordpress/WooCommerce so it's simply a function/hook:

    function prg_woocommerce_checkout_fields( $checkout_fields = array() ) {
    
    	$checkout_fields['order']['categories'] = array(
    		'type'			=> 'select',
    		'class'			=> array('my-field-class form-row-wide'),	
    		'label'			=> __('Categories', 'woocommerce'),
    		'placeholder'	=> _x('', 'placeholder', 'woocommerce'),
    		'required'		=> false,
    		'clear'			=> false,
    		'options'		=> array_values($category_str)
    
    	);			
    
    	return $checkout_fields;
    
    }
    

    If I manually add the option:

    		'options'     => array(
    			'eat-meat' => __('I eat meat', 'woocommerce' ),
    			'not-meat' => __('Meat is gross', 'woocommerce' )
    		)
    

    Then the drop down creates correctly:

    <option value="eat-meat">I eat meat</option>
    <option value="not-meat">Meat is gross</option>
    						
    

    But once I try to make it dynamic by passing values from elsewhere, it just doesn't quite display the right output

     

    Does that help?

  9. Ok, I've managed to get somewhere but struggling with one part now. So I now have:

    $categories = get_field( 'categories', 'options' );
    $choices = array();
    if ( is_array( $categories ) ) {
    
    	$category_str = array();
    	foreach ( $categories as $category ) {
    		
    		$category['value'];
    		$category['label'];
    		$category_str[$category['value']] = $category['label'];
    	
    	}
    	
    }
    
    	$checkout_fields['order']['categories'] = array(
    		'type'        => 'select',
    		'class'         => array('my-field-class form-row-wide'),	
    		'label'       => __('Food options', 'woocommerce'),
    		'placeholder' => _x('', 'placeholder', 'woocommerce'),
    		'required'    => false,
    		'clear'       => false,
    		'options' 	  => array_values($category_str)	
    	);	
    

    If I use print_r($category_str); I see the following:

    Array
    (
        [a] => A
        [b] => B
        [c] => C
    )
    

    Yet if I view the drop down it shows:

    <option value="0">A</option>
    <option value="1">B</option>
    <option value="2">C</option>
    

    So how do I get the value to be the value from the first array? So it's like this:

    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
    

    Thanks

  10. Hi,

     

    Am hoping someone can help/point me in the right direction!

     

    I have the following code:

    	$checkout_fields['order']['categories'] = array(
    		'type'        => 'select',
    		'class'         => array('my-field-class form-row-wide'),	
    		'label'       => __('Food options', 'woocommerce'),
    		'placeholder' => _x('', 'placeholder', 'woocommerce'),
    		'required'    => false,
    		'clear'       => false,
    		'options'     => array(
    			'eat-meat' => __('I eat maet', 'woocommerce' ),
    			'not-meat' => __('Meat is gross', 'woocommerce' )
    		)
    	);	
    

    However, I'd like to make the options part dynamic and therefore grab the values from elsewhere. So I then have this code:

    $categories = get_field( 'categories', 'options' );
    $choices = array();
    if ( is_array( $categories ) ) {
    
    	$category_str = array();
    	foreach ( $categories as $category ) {
    		
    		#echo $category['value'];
    		#echo $category['label'];
    		
    		$category['value'];
    		$category['label'];
    		$category_str[] = $category['value'].$category['label'];
    	
    	}
    	
    	$result = implode(",",$category_str);
    	echo $result;
    }
    

    My issue is I'm not sure on the best way to amalgamate the two?

     

    Can someone help?

  11. Hi,

     

    Apologies if this is a basic question but either I've misunderstood or I'm being daft 

     

    In one function I have:

    function create_coupon() {
    	$unique_coupon_code = $coupon_name.'-'.$date; #concatenate the two
    	return $unique_coupon_code;
    }
    

    Then in my second function I try to get that variable ($unique_coupon_code) to use again:

    function use_coupon() {
    	echo $unique_coupon_code;
    }
    

    But it returns blank?

     

    However, if I echo $unique_coupon_code; in the first function, it does show a value

     

    What am I doing wrong?

    Thanks

  12. Hi,

     

    Apologies If I've got the title wrong but I'm trying to obtain a value from an array, however, it's an object (?)

    Array
    (
        [test] => WC_Coupon Object
            (
                [code] => test
                [id] => 1529
                [exists] => 1
                [discount_type] => percent_product
                [coupon_amount] => 10
                [individual_use] => yes
                [product_ids] => Array
    
    

    I need to get coupon_amount

     

    I thought I could use $my_coupon[0]->coupon_amount; but believe I've confused myself or misunderstood!

     

    Any advice is gratefully received!

    Thanks

  13. Thanks requinix

     

    Yes, sadly it's Wordpress and stores data that way - made for an interesting day so far!

     

    Ok, I will look to amend the query but last time i tried if broke it lol. It looked like this:

    WHERE 		(
    			a1.meta_key = '$siteID'
    			AND a1.meta_value NOT LIKE 'a:1:{s:12:"shop_manager";b:1;}'
    		}
    		AND 
    		{
    			a2.meta_key = '$siteID'
    			AND a2.meta_value = 'a:1:{s:10:"subscriber";b:1;}'
    		)
    		AND (
    			a3.meta_key = 'q1'
    			AND a3.meta_value REGEXP '$q1_regex'
    
    		OR 
    			a4.meta_key = 'q2'
    			AND a4.meta_value REGEXP '$q2_regex'
    		
    		OR 
    			a5.meta_key = 'q3'
    			AND a5.meta_value REGEXP '$q3_regex'
    		)
    

    But think that may be wrong also!?

  14. Hi,

    I think I'm missing the obvious, so turned to here for some assistance! I have the following query:

    		SELECT DISTINCT a1.user_id
    		FROM $wpdb->usermeta a1
    		INNER JOIN $wpdb->usermeta a2 ON a1.user_id = a2.user_id
    		INNER JOIN $wpdb->usermeta a3 ON a1.user_id = a3.user_id
    		INNER JOIN $wpdb->usermeta a4 ON a1.user_id = a4.user_id
    		INNER JOIN $wpdb->usermeta a5 ON a1.user_id = a5.user_id
    		WHERE (
    			a1.meta_key = 'q1'
    			AND a1.meta_value REGEXP '$q1_regex'
    		)
    		OR (
    			a2.meta_key = 'q2'
    			AND a2.meta_value REGEXP '$q2_regex'
    		)
    		OR (
    			a3.meta_key = 'q3'
    			AND a3.meta_value REGEXP '$q3_regex'
    		)
    		AND (
    			a4.meta_key = '$siteID'
    			AND a4.meta_value NOT LIKE 'a:1:{s:12:\"shop_manager\";b:1;}'
    		)
    		AND (
    			a5.meta_key = '$siteID'
    			AND a5.meta_value = 'a:1:{s:10:\"subscriber\";b:1;}'
    		)
    

    It returns results (which is a good start). However, it also returns some results that shouldn't be there.

     

    In this example. $siteID =   'table_99_capabilities';

     

    For example. It returns the following user IDs

    61

    64

    65

     

    Yet I know the following:

    61 = is a customer, so fails on the last part of the query (AND a5.meta_value = 'a:1:{s:10:\"subscriber\";b:1;}')

    64 & 65 fail as they have a siteID of table_66_capabilities

     

    I'm hoping this is enough info and clearly explained 

     

    Any help is much appreciated

     

    Thanks for your time

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.