Last active
February 1, 2016 14:59
-
-
Save dleone81/7af670df9de65a349e1f to your computer and use it in GitHub Desktop.
Add or remove product to specific category based on product custom attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add / Remove product to 'Angolo delle offerte' on product save action | |
**/ | |
function update_product_category( $post_id ) { | |
global $post; | |
if(function_exists('get_product')) { | |
$product = get_product($post->ID); | |
if($product->is_type('variable')) { | |
$available_variations = $product->get_available_variations(); | |
$catarray = array(); | |
$myarray = array(); | |
foreach($available_variations as $k) { | |
$_in_promo = get_post_meta($k[variation_id], '_in_promo', true); | |
$_special_price_to_date = get_post_meta($k[variation_id], '_special_price_to_date', true); | |
$now = date("Y-m-d"); | |
$exp = date("Y-m-d", strtotime(str_replace('/', '-', $_special_price_to_date))); | |
if(($_in_promo == yes) && ($now<=$exp)) { | |
$myarray[k.$exp] = $_in_promo; | |
} | |
} | |
$newarray = array_merge($catarray, $myarray); | |
$newarray = array_filter($newarray); | |
if(!empty($newarray)) { | |
wp_set_object_terms($post->ID, 'angolo-delle-offerte', 'product_cat', $append = true); | |
} else { | |
wp_remove_object_terms($post->ID, 'angolo-delle-offerte', 'product_cat' ); | |
} | |
} | |
} | |
} | |
add_action( 'save_post', 'update_product_category' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment