Support

Account

Home Forums ACF PRO Using repeater for coupon codes

Solved

Using repeater for coupon codes

  • Hi,

    So the basic premise is that the client wants to hand out promo codes that link to specific pages.

    The parent promo page will have a box where you enter a code and if the code is correct, it will load the relevant page.

    So currently, I have set up a page with a repeater field (as there will be multiple promo/coupon codes):

    Repeater – promosettings
    1 – Title – title (for example, 50% Off Winter)
    2 – Promo Code – code (for example WINTER2015)
    3 – URL – url – (www.example.com/link)

    I’ve got a form to enter the coupon code that posts to itself.

    I’ve tried following http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/?codekitCB=400588264.302023 but can’t seem to wrap my head around it when using POST data.

    Current Code:

    	$codeErr = "";	
    	$code = ""; 
    	if ($_SERVER["REQUEST_METHOD"] == "POST") {
    		if (empty($_POST["code"])) {
    			$codeErr = "Promo code is required";
    		} else {
    			$code = code_input($_POST["code"]);
    			if (!preg_match("/[A-Za-z0-9]+/",$code)) {
    			   $codeErr = "Incorrect promo code. Please try again."; 
    			} else {				
    				$rows = $wpdb->get_results($wpdb->prepare( 
    					"
    					SELECT * 
    					FROM wp_vrtny1k0kz_postmeta
    					WHERE meta_key LIKE %s
    						AND meta_value = %s
    					",
    					'promosettings_%_code',
    					$code
    				));	
    				if (!$rows){
    					$codeErr = "Incorrect promo code. Please try again."; 
    				}
    			}
    		}	
    	}	
    	function code_input($data) {
    	   $data = trim($data);
    	   $data = stripslashes($data);
    	   $data = htmlspecialchars($data);
    	   return $data;
    	}
    	?>
    	<div class="row">
    		<div class="col-sm-6 col-xs-12">
    			<form action="<?php echo get_permalink(); ?>" method="post" id="promo">
    				<div class="row">
    					<div class="col-sm-9 col-xs-8">
    						<input type="text" name="code">
    					</div>	
    					<div class="col-sm-3 col-xs-4">	
    						<input type="submit" name="submit" value="Submit"> 
    					</div>
    				</div>
    			</form>
    			<span class="error"><?php echo $codeErr; ?></span>
    		</div>	
    	</div>
    	<?php 				
    	if( $rows )	{
    		foreach( $rows as $row ) {
    				preg_match('_([0-9]+)_', $row->meta_key, $matches);
    				$meta_key = 'promosettings_' . $matches[0] . '_url';
    				$url = get_post_meta( $row->post_id, $meta_key, true );
    				echo $url[0]; 
    		}
    	} 
    

    So with the above, if I try a working code, it echos “hh”, however with an incorrect code it will display the incorrect code error. Something must be working somewhere so any ideas why it’s not echo’ing the URL?

  • Ok, so changing

    preg_match('_([0-9]+)_', $row->meta_key, $matches);
    				$meta_key = 'promosettings_' . $matches[0] . '_url';
    				$url = get_post_meta( $row->post_id, $meta_key, true );
    				echo $url[0]; 

    to

    preg_match('_([0-9]+)_', $row->meta_key, $matches);
    				$meta_key = 'promosettings_' . $matches[0] . '_url';
    				$url = get_post_meta( $row->post_id, $meta_key, true );
    				echo $url; 

    Now shows me the URL, but duplicated.

  • Cool, so aside from the duplication, it seems to work fine as the goal is to redirect to the URL if a correct code is entered.

    Code so far:

    	<?php 
    	$codeErr = "";	
    	$code = ""; 
    	if ($_SERVER["REQUEST_METHOD"] == "POST") {
    		if (empty($_POST["code"])) {
    			$codeErr = "<strong>Error:</strong> Promo code is required";
    		} else {
    			$code = code_input($_POST["code"]);
    			if (!preg_match("/[A-Za-z0-9]+/",$code)) {
    			   $codeErr = "<strong>Error:</strong> Incorrect promo code. Please try again."; 
    			} else {				
    				$rows = $wpdb->get_results($wpdb->prepare( 
    					"
    					SELECT * 
    					FROM wp_vrtny1k0kz_postmeta
    					WHERE meta_key LIKE %s
    						AND meta_value = %s
    					",
    					'promosettings_%_code',
    					''.$code.''
    				));	
    				if (!$rows){
    					$codeErr = "<strong>Error:</strong> Incorrect promo code. Please try again."; 
    				}
    			}
    		}	
    	}	
    	function code_input($data) {
    	   $data = trim($data);
    	   $data = stripslashes($data);
    	   $data = htmlspecialchars($data);
    	   return $data;
    	}
    	?>
    	<div class="row">
    		<div class="col-sm-6 col-xs-12">
    			<form action="<?php echo get_permalink(); ?>" method="post" id="promo">
    				<div class="row">
    					<div class="col-sm-9 col-xs-8">
    						<input type="text" name="code">
    					</div>	
    					<div class="col-sm-3 col-xs-4">	
    						<input type="submit" name="submit" value="Submit"> 
    					</div>
    				</div>
    			</form>
    			<span class="error"><?php echo $codeErr; ?></span>
    		</div>	
    	</div>
    	<?php 				
    	if( $rows )	{
    		foreach( $rows as $row ) {
    				preg_match('_([0-9]+)_', $row->meta_key, $matches);
    				$url_key = 'promosettings_' . $matches[0] . '_url';
    				$url = get_post_meta( $row->post_id, $url_key, true );
    				?>
    				<script>
    					window.location.replace("<?php echo $url; ?>");
    				</script>
    				<?php
    		}
    	}

    Edit – added a counter. Not sure if this is the most effective way, but it works

    	if( $rows )	{
    		$i = 0;
    		foreach( $rows as $row ) {
    			if($i == 0) {
    				preg_match('_([0-9]+)_', $row->meta_key, $matches);
    				$url_key = 'promosettings_' . $matches[0] . '_url';
    				$title_key = 'promosettings_' . $matches[0] . '_title';
    				$url = get_post_meta( $row->post_id, $url_key, true );
    				$title = get_post_meta( $row->post_id, $title_key, true );
    				echo $url;
    				echo $title;
    		
    			}
    			$i++;	
    		}
    	} 
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Using repeater for coupon codes’ is closed to new replies.