• Resolved mairh93

    (@mairh93)


    Hello all,

    I had created a snippet code with an php in order o display in the store list the opening and close hours of the store.It worked fine for several days but suddently I am getting the error below :

    Warning: Invalid argument supplied for foreach() in /home/customer/www/m……com/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()’d code on line 1

    I provide you also the php code that I have in the snippet :

    add_action('wcfmmp_store_list_after_store_info','fn_wcfmmp_store_list_after_store_info',11,2); function fn_wcfmmp_store_list_after_store_info($store_id, $store_info) { global $WCFM, $WCFMmp, $wp, $WCFM_Query; $store_user = wcfmmp_get_store( $store_id ); $wcfm_vendor_store_hours = get_user_meta( $store_id, 'wcfm_vendor_store_hours', true ); if( !$wcfm_vendor_store_hours ) { return; } $wcfm_store_hours_enable = isset( $wcfm_vendor_store_hours['enable'] ) ? 'yes' : 'no'; if( $wcfm_store_hours_enable != 'yes' ) return; $wcfm_store_hours_off_days = isset( $wcfm_vendor_store_hours['off_days'] ) ? $wcfm_vendor_store_hours['off_days'] : array(); $wcfm_store_hours_day_times = isset( $wcfm_vendor_store_hours['day_times'] ) ? $wcfm_vendor_store_hours['day_times'] : array(); if( empty( $wcfm_store_hours_day_times ) ) return; $weekdays = array( 0 => __( 'Monday', 'wc-multivendor-marketplace' ), 1 => __( 'Tuesday', 'wc-multivendor-marketplace' ), 2 => __( 'Wednesday', 'wc-multivendor-marketplace' ), 3 => __( 'Thursday', 'wc-multivendor-marketplace' ), 4 => __( 'Friday', 'wc-multivendor-marketplace' ), 5 => __( 'Saturday', 'wc-multivendor-marketplace' ), 6 => __( 'Sunday', 'wc-multivendor-marketplace') ); ?> <div class="wcfmmp_store_hours"> <span class="wcfmmp-store-hours widget-title"><span class="wcfmfa fa-clock"></span>Store Hours</span><div class="wcfm_clearfix"></div> <?php $day_number = date('w', time()); if($day_number == 0) { $day = 6; } else { $day = $day_number-1; } if(in_array($day, $wcfm_store_hours_off_days)){ foreach( $wcfm_store_hours_day_times[$day+1] as $wcfm_store_hours_day => $wcfm_store_hours_day_time_slots ) { if( in_array( $wcfm_store_hours_day, $wcfm_store_hours_day_times[$day+1] ) ) continue; if( !isset( $wcfm_store_hours_day_time_slots['start'] ) ) return; if( empty( $wcfm_store_hours_day_time_slots['start'] ) || empty( $wcfm_store_hours_day_time_slots['end'] ) ) continue; echo '<span class="wcfmmp-store-hours-day">' . $weekdays[$day+1] . ':</span>'; echo "<br />" . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['start'] ) ) . " - " . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['end'] ) ); echo '<br /><br />'; } } else { foreach( $wcfm_store_hours_day_times[$day] as $wcfm_store_hours_day => $wcfm_store_hours_day_time_slots ) { if( in_array( $wcfm_store_hours_day, $wcfm_store_hours_day_times[$day] ) ) continue; if( !isset( $wcfm_store_hours_day_time_slots['start'] ) ) return; if( empty( $wcfm_store_hours_day_time_slots['start'] ) || empty( $wcfm_store_hours_day_time_slots['end'] ) ) continue; echo '<span class="wcfmmp-store-hours-day">Today:</span>'; echo "<br />" . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['start'] ) ) . " - " . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['end'] ) ); echo '<br /><br />'; } } ?> </div> <?php }

    Do you know how I could fix it ?
    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter mairh93

    (@mairh93)

    I noticed that I get this error whn the stores dont have set the open hours of today. So is it possible when they dont have set the hour to show nothing, in order to dont display this error ? or else add a text : store doesnt available .

    Thank you

    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @mairh93,

    Looks like you had a bug in your snippet, which was not properly checking whether an array index existed before attempting to access it. Hopefully this updated version should fix the issue:

    add_action( 'wcfmmp_store_list_after_store_info', function ( $store_id ) {
    	$wcfm_vendor_store_hours = get_user_meta( $store_id, 'wcfm_vendor_store_hours', true );
    	if ( ! $wcfm_vendor_store_hours ) {
    		return;
    	}
    	$wcfm_store_hours_enable = isset( $wcfm_vendor_store_hours['enable'] ) ? 'yes' : 'no';
    	if ( $wcfm_store_hours_enable != 'yes' ) {
    		return;
    	}
    
    	$wcfm_store_hours_day_times = isset( $wcfm_vendor_store_hours['day_times'] ) ? $wcfm_vendor_store_hours['day_times'] : array();
    	if ( empty( $wcfm_store_hours_day_times ) ) {
    		return;
    	}
    	$weekdays = array(
    		0 => __( 'Monday', 'wc-multivendor-marketplace' ),
    		1 => __( 'Tuesday', 'wc-multivendor-marketplace' ),
    		2 => __( 'Wednesday', 'wc-multivendor-marketplace' ),
    		3 => __( 'Thursday', 'wc-multivendor-marketplace' ),
    		4 => __( 'Friday', 'wc-multivendor-marketplace' ),
    		5 => __( 'Saturday', 'wc-multivendor-marketplace' ),
    		6 => __( 'Sunday', 'wc-multivendor-marketplace' ),
    	);
    
    	?>
    	<div class="wcfmmp_store_hours"><span class="wcfmmp-store-hours widget-title"><span class="wcfmfa fa-clock"></span>Store Hours</span>
    		<div class="wcfm_clearfix"></div> <?php $day_number = date( 'w', time() );
    		if ( $day_number == 0 ) {
    			$day = 6;
    		} else {
    			$day = $day_number - 1;
    		}
    		if ( isset( $wcfm_store_hours_day_times[ $day + 1 ] ) && is_array( $wcfm_store_hours_day_times[ $day + 1 ] ) ) {
    			foreach ( $wcfm_store_hours_day_times[ $day + 1 ] as $wcfm_store_hours_day => $wcfm_store_hours_day_time_slots ) {
    				if ( in_array( $wcfm_store_hours_day, $wcfm_store_hours_day_times[ $day + 1 ] ) ) {
    					continue;
    				}
    				if ( ! isset( $wcfm_store_hours_day_time_slots['start'] ) ) {
    					return;
    				}
    				if ( empty( $wcfm_store_hours_day_time_slots['start'] ) || empty( $wcfm_store_hours_day_time_slots['end'] ) ) {
    					continue;
    				}
    				echo '<span class="wcfmmp-store-hours-day">' . $weekdays[ $day + 1 ] . ':</span>';
    				echo "<br />" . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['start'] ) ) . " - " . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['end'] ) );
    				echo '<br /><br />';
    			}
    		} elseif ( isset( $wcfm_store_hours_day_times[ $day ] ) && is_array( $wcfm_store_hours_day_times[ $day ] ) ) {
    			foreach ( $wcfm_store_hours_day_times[ $day ] as $wcfm_store_hours_day => $wcfm_store_hours_day_time_slots ) {
    				if ( in_array( $wcfm_store_hours_day, $wcfm_store_hours_day_times[ $day ] ) ) {
    					continue;
    				}
    				if ( ! isset( $wcfm_store_hours_day_time_slots['start'] ) ) {
    					return;
    				}
    				if ( empty( $wcfm_store_hours_day_time_slots['start'] ) || empty( $wcfm_store_hours_day_time_slots['end'] ) ) {
    					continue;
    				}
    				echo '<span class="wcfmmp-store-hours-day">Today:</span>';
    				echo "<br />" . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['start'] ) ) . " - " . date_i18n( wc_time_format(), strtotime( $wcfm_store_hours_day_time_slots['end'] ) );
    				echo '<br /><br />';
    			}
    		}
    		?>
    	</div>
    	<?php
    }, 11, 2 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Error in snippet code – php’ is closed to new replies.