Last active
December 29, 2015 14:59
-
-
Save rohmann/7687335 to your computer and use it in GitHub Desktop.
When "on sale" is toggled, a "promo" tag will automatically be added/removed from the product.
http://git.io/pOvGzg
This file contains 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
<?php | |
function auto_tag_on_sale( $meta_id, $post_id, $meta_key) { | |
if ('mp_is_sale' != $meta_key){ | |
return; | |
} | |
$meta = get_post_custom($post_id); | |
$on_sale = false; | |
if (is_array($meta['mp_is_sale'])) { | |
foreach($meta['mp_is_sale'] as $key) { | |
$on_sale = ($key || $on_sale); | |
} | |
} | |
else { | |
$on_sale = $meta['mp_is_sale']; | |
} | |
$has_promo_tag = has_term('promo','product_tag',$post_id); | |
global $mp_wpmu; | |
if($on_sale && !$has_promo_tag) { | |
wp_set_post_terms( $post_id, 'promo', 'product_tag', true ); | |
$mp_wpmu->index_product($post_id); | |
} | |
if(!$on_sale && $has_promo_tag) { | |
$without_promo = array_diff(wp_get_post_terms(104,'product_tag',array("fields" => "names")),array('promo')); | |
wp_set_post_terms( $post_id, $without_promo, 'product_tag' ); | |
$mp_wpmu->index_product($post_id); | |
} | |
} | |
add_action( 'updated_postmeta', 'auto_tag_on_sale',12,3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be placed within a network activated plugin, or a plugin in the mu-plugins folder
For quick implementation without coding, use these steps: