Skip to content

Instantly share code, notes, and snippets.

@aristath
Created March 19, 2013 09:03
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 aristath/5194644 to your computer and use it in GitHub Desktop.
Save aristath/5194644 to your computer and use it in GitHub Desktop.
function custom_mp_buy_button_class( $post_id = NULL ) {
global $id, $mp;
$post_id = ( NULL === $post_id ) ? $id : $post_id;
$meta = get_post_custom($post_id);
//unserialize
foreach ($meta as $key => $val) {
$meta[$key] = maybe_unserialize($val[0]);
if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file")
$meta[$key] = array($meta[$key]);
}
//check stock
$no_inventory = array();
$all_out = false;
if ($meta['mp_track_inventory']) {
$cart = $mp->get_cart_contents();
if (isset($cart[$post_id]) && is_array($cart[$post_id])) {
foreach ($cart[$post_id] as $variation => $data) {
if ($meta['mp_inventory'][$variation] <= $data['quantity'])
$no_inventory[] = $variation;
}
foreach ($meta['mp_inventory'] as $key => $stock) {
if (!in_array($key, $no_inventory) && $stock <= 0)
$no_inventory[] = $key;
}
}
//find out of stock items that aren't in the cart
foreach ($meta['mp_inventory'] as $key => $stock) {
if (!in_array($key, $no_inventory) && $stock <= 0)
$no_inventory[] = $key;
}
if (count($no_inventory) >= count($meta["mp_price"]))
$all_out = true;
}
if ($all_out) {
$all_out_class = 'mp_no_stock';
}
echo $all_out_class;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment