Skip to content

Instantly share code, notes, and snippets.

@rohmann
Last active December 29, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohmann/7687335 to your computer and use it in GitHub Desktop.
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
<?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 );
@rohmann
Copy link
Author

rohmann commented Nov 28, 2013

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:

  1. Download the PHP file by click clicking the raw icon above (see image) and chosing "Save link as"
  2. Upload that file to the wp-content/mu-plugins folder
    alt text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment