Home › Forums › ACF PRO › Using repeater for coupon codes › Reply To: Using repeater for coupon codes
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++;
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.