Get a list of special offers for a dealer based on whether the dealer has special offers turned off, whether they are in a certain region or whether they have their own special offers.
<?php | |
###################################################### | |
//get a list of specials assigned to a dealer so that we can display a list | |
//of post id's in the array_of_dealer | |
//added on 19/10/2018 by craig@jucra.com | |
###################################################### | |
function white_label_get_array_of_special_offer_posts_for_dealer($dealer_id) { | |
################################################### | |
//STEP 1 | |
//Check if this dealer has disabled special offers on their site. | |
//Some dealer want NO special offers to be shown soif the dealer has disabled special offers then no point | |
//wasting resources to get any special offers. | |
################################################### | |
$special_offer_status = get_post_meta( $dealer_id, "dealer_disable_special_offers_page", $single = true ); | |
if(is_array($special_offer_status)) { | |
$dealer_special_offer_status = $special_offer_status[0]; | |
if($dealer_special_offer_status == "yes") { | |
$array_of_special_offers_posts_ids = ""; | |
return $array_of_special_offers_posts_ids; | |
} | |
} | |
################################################### | |
//STEP 2 | |
//Get the region id the dealer is in. | |
//This is needed to determine if we should include egional special offers on a dealer special offer page | |
################################################### | |
//note added on 19/10/2018 by craig@jucra.com | |
//we have to use a sql query becasue you cannot use wp_get_post_terms in functions.php | |
//wp_get_post_terms only works on the theme pages, something about init, fucking complicated shite. | |
$sql = " | |
SELECT t.*, tt.* FROM wp_core_terms AS t | |
INNER JOIN wp_core_term_taxonomy AS tt ON (tt.term_id = t.term_id) | |
INNER JOIN wp_core_term_relationships AS tr ON (tr.term_taxonomy_id = tt.term_taxonomy_id) | |
WHERE tt.taxonomy IN ('dealers_regions') AND tr.object_id IN ($dealer_id) | |
ORDER BY t.name ASC; | |
"; | |
global $wpdb; | |
$wpdb->show_errors( true ); | |
$results = $wpdb->get_results($sql); | |
$dealers_region_id = $results[0]->term_id; | |
//************************************************// | |
//TROUBLESHOOTING | |
//************************************************// | |
$display_troublehooting = false; | |
if($display_troublehooting == true && WHITE_LABEL_DEBUGGING_STATUS == "on") { | |
echo "DEALER REGION ID: " . $dealers_region_id; | |
echo "<pre>"; | |
echo "This is Line number: " . __LINE__ . " in " . basename(__FILE__) . PHP_EOL; | |
print_r($array_of_dealers_region); | |
echo "</pre>"; | |
} | |
//************************************************// | |
################################################### | |
//STEP 3 | |
//get an array of ALL the special offers | |
################################################### | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'special-offers', | |
'post_status' => 'publish' | |
); | |
$posts_array = get_posts( $args ); | |
################################################### | |
//Create an array of post id's for the special offers. | |
//Also we have to include certain other variables for the special offer | |
//such as if the special offer is a regional one or the list of dealer sites it should appear on. | |
################################################### | |
$count = 0; | |
foreach($posts_array as $post) { | |
$post_id = $posts_array[$count]->ID; | |
$display_type = get_post_meta( $post_id, "dealers_special_offers_display_type", $single = true ); | |
$regions = get_post_meta( $post_id, "dealers_special_offers_regions", $single = true ); | |
$dealer_sites = get_post_meta( $post_id, "dealers_special_offers_dealer_sites", $single = true ); | |
$array_of_specials[] = array( | |
'post_name' => get_the_title($post_id), | |
'post_id' => $post_id, | |
'display_type' => $display_type, | |
'regions' => $regions, | |
'dealer_sites' => $dealer_sites, | |
); | |
$count = $count + 1; | |
} | |
//************************************************// | |
//TROUBLESHOOTING | |
//************************************************// | |
$display_troublehooting = false; | |
if($display_troublehooting == true && WHITE_LABEL_DEBUGGING_STATUS == "on") { | |
echo "<pre>"; | |
echo "This is Line number: " . __LINE__ . " in " . basename(__FILE__) . PHP_EOL; | |
print_r($array_of_specials); | |
echo "</pre>"; | |
} | |
//************************************************// | |
################################################### | |
//STEP 4 | |
//Determine if a special offer should be on a single site or region of sites | |
//EG: Dealer A might be located in Canada and have 2 special offers. | |
//There might be 10 regional offers for the Canadian region. | |
//So we will need to show 12 special offers on the dealers feed. | |
################################################### | |
$new_array_of_specials = array(); | |
$count = 0; | |
foreach($array_of_specials as $x){ | |
$post_id = $array_of_specials[$count]["post_id"]; | |
$display_type = $array_of_specials[$count]["display_type"]; | |
$regions = $array_of_specials[$count]["regions"]; | |
$dealer_sites = $array_of_specials[$count]["dealer_sites"]; | |
//determine the display type first | |
if($display_type == "sites") { | |
if(in_array($dealer_id, $dealer_sites)){ | |
$new_array_of_specials[] = array( | |
'post_id' => $post_id, | |
'post_name' => get_the_title($post_id), | |
'permalink' => get_permalink($post_id), | |
); | |
} | |
} | |
if($display_type == "regions") { | |
if(in_array($dealers_region_id, $regions)){ | |
$new_array_of_specials[] = array( | |
'post_id' => $post_id, | |
'post_name' => get_the_title($post_id), | |
'permalink' => get_permalink($post_id), | |
); | |
} | |
} | |
$count = $count + 1; | |
} | |
//************************************************// | |
//TROUBLESHOOTING | |
//************************************************// | |
$display_troublehooting = false; | |
if($display_troublehooting == true && WHITE_LABEL_DEBUGGING_STATUS == "on") { | |
echo "<pre>"; | |
echo "This is Line number: " . __LINE__ . " in " . basename(__FILE__) . PHP_EOL; | |
print_r($new_array_of_specials); | |
echo "</pre>"; | |
} | |
//************************************************// | |
################################################### | |
//STEP 5 | |
//produce a list of special offers for the dealer | |
################################################### | |
if(count($new_array_of_specials) > 0) { | |
$count = 0; | |
foreach($new_array_of_specials as $post_id){ | |
$post_id = $new_array_of_specials[$count]["post_id"]; | |
$final_array_of_special_offers[] = $post_id; | |
$count = $count + 1; | |
} | |
} | |
//************************************************// | |
//TROUBLESHOOTING | |
//************************************************// | |
$display_troublehooting = false; | |
if($display_troublehooting == true && WHITE_LABEL_DEBUGGING_STATUS == "on") { | |
echo "<pre>"; | |
echo "This is Line number: " . __LINE__ . " in " . basename(__FILE__) . PHP_EOL; | |
print_r($new_array_of_specials); | |
echo "</pre>"; | |
} | |
//************************************************// | |
return $final_array_of_special_offers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment