STOCKI-WYKRYWANIE-KONTA

user_0377216 avatar
user_0377216
plain_text
03/02/2026 9:30 AM
4.9 KB
17
Indexable
add_filter('woocommerce_product_get_stock_status', function ($status, $product) {
  if (!is_a($product, 'WC_Product')) return $status;
  if (!er_is_b2b_user()) return $status;
  if (!$product->is_type('variable')) return $status;

  foreach ($product->get_children() as $vid) {
    $v = wc_get_product($vid);
    if (!$v) continue;

    $separate = get_post_meta($vid, '_separate_stock_quantities_b2b', true);
    if ($separate !== 'yes') $separate = get_post_meta($product->get_id(), '_separate_stock_quantities_b2b', true);

    if ($separate === 'yes') {
      $qty = (int) get_post_meta($vid, '_stock_b2b', true);
      $back = get_post_meta($vid, '_backorders_b2b', true);
      $manage = $v->managing_stock(); // nadal true/false wg WC
      $in = (!$manage && $v->get_stock_status() === 'instock') || ($manage && $qty > 0) || in_array($back, ['yes','notify'], true);
    } else {
      $qty = (int) $v->get_stock_quantity();
      $back = $v->get_backorders();
      $manage = $v->managing_stock();
      $in = (!$manage && $v->get_stock_status() === 'instock') || ($manage && $qty > 0) || in_array($back, ['yes','notify'], true);
    }

    if ($in) return 'instock';
  }

  return 'outofstock';
}, 20, 2);


// 1) B2B detector (rola b2b-* lub meta b2bking_b2buser)
if ( ! function_exists('er_is_b2b_user') ) {
	function er_is_b2b_user() {
		if ( ! is_user_logged_in() ) return false;

		$u = wp_get_current_user();
		if ( empty($u) || empty($u->ID) ) return false;

		foreach ( (array) $u->roles as $r ) {
			if ( strpos($r, 'b2b-') === 0 ) return true;
		}

		return ( get_user_meta($u->ID, 'b2bking_b2buser', true) === 'yes' );
	}
}

// 2) Helper: czy produkt jest "instock" wg B2B (separate stock)
if ( ! function_exists('er_b2b_product_is_in_stock') ) {
	function er_b2b_product_is_in_stock($product_id) {
		$p = wc_get_product($product_id);
		if ( ! $p ) return false;

		// SIMPLE
		if ( $p->is_type('simple') ) {
			$sep = get_post_meta($product_id, '_separate_stock_quantities_b2b', true);
			if ( $sep === 'yes' ) {
				$qty  = (int) get_post_meta($product_id, '_stock_b2b', true);
				$back = get_post_meta($product_id, '_backorders_b2b', true);
				return ( $qty > 0 || $back === 'yes' || $back === 'notify' );
			}
			return $p->is_in_stock();
		}

		// VARIABLE PARENT: jakikolwiek wariant ma stock_b2b > 0
		if ( $p->is_type('variable') ) {
			$parent_sep = get_post_meta($product_id, '_separate_stock_quantities_b2b', true);

			foreach ( $p->get_children() as $vid ) {
				$sep = get_post_meta($vid, '_separate_stock_quantities_b2b', true);
				if ( $sep !== 'yes' ) $sep = $parent_sep;

				if ( $sep === 'yes' ) {
					$qty  = (int) get_post_meta($vid, '_stock_b2b', true);
					$back = get_post_meta($vid, '_backorders_b2b', true);
					if ( $qty > 0 || $back === 'yes' || $back === 'notify' ) return true;
				} else {
					$v = wc_get_product($vid);
					if ( $v && $v->is_in_stock() ) return true;
				}
			}
			return false;
		}

		// Inne typy
		return $p->is_in_stock();
	}
}

// 3) Główna logika: jeśli URL ma stock_status=instock i user jest B2B → filtruj listę produktów
add_filter('the_posts', function($posts, $query) {
	if ( is_admin() || ! $query->is_main_query() ) return $posts;
	if ( ! er_is_b2b_user() ) return $posts;

	// działa dokładnie pod Twój URL
	if ( empty($_GET['stock_status']) || $_GET['stock_status'] !== 'instock' ) return $posts;

	$post_type = $query->get('post_type');
	if ( $post_type && $post_type !== 'product' && $post_type !== array('product') ) return $posts;

	$out = array();
	foreach ( (array) $posts as $p ) {
		if ( er_b2b_product_is_in_stock($p->ID) ) {
			$out[] = $p;
		}
	}
	return $out;
}, 20, 2);
Editor is loading...
Leave a Comment