繁体   English   中英

Woocommerce:显示除购物车以外的所有产品

[英]Woocommerce: Show all products excluding what contains cart

我正在尝试显示所有可用产品,但当前购物车中没有。 我尝试使用Internet上的某些方法,但是大多数方法使用过滤器,因此我无法排除包含购物车的内容。

我的示例代码显示所有库存产品,如下所示:

    <?php
    $params = array(
        'posts_per_page' => 999,
        'post_type' => 'product',
        'meta_query' => array(
            array(
                        'key' => '_stock_status',
                        'value' => 'instock'
                    )
        )
    );

    $wc_query = new WP_Query($params);

?>

<ul>
<?php
  if ($wc_query->have_posts()) :
    while ($wc_query->have_posts()) :
      $wc_query->the_post();
      $product = new WC_Product(get_the_ID());
?>
  <li>
  <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php
    the_post_thumbnail();
    the_excerpt();
    echo $product->get_price_html();
    echo $product->get_sku();
?>
  </li>

<?php
    endwhile; 

  wp_reset_postdata(); ?>
  else:
?>
  <li>
  <?php _e( 'No Products' ); ?>
  </li>

  <?php endif; ?>
</ul>

任何提示如何使其正常工作? 我只想到一件事-检查购物车中的SKU。

得到解决:

global $woocommerce; 
$my_cart = array();

$items = $woocommerce->cart->get_cart();

foreach($items as $item => $values) { 
//print_r($values); 
$_product = $values['data']->post;

$my_cart[] = $_product->ID;
}

//print_r($my_cart);

$params = array(
  'posts_per_page' => 999, 
  'post_type' => 'product',
  'meta_query' => array(
    array(
      'key' => '_stock_status',
      'value' => 'instock'
      )
  ) 
);

$wc_query = new WP_Query($params);

?>

<ul>

<?php

  if ($wc_query->have_posts()) :
    while ($wc_query->have_posts()) :
      $wc_query->the_post(); 
      $product = new WC_Product(get_the_ID()); 


if (!in_array($product->id, $my_cart)){
?>

<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php 

    the_post_thumbnail();
    the_excerpt();
    echo $product->get_price_html(); 
    echo $product->get_sku();
    echo $product->id;

?>

<form class="cart" method="post" enctype="multipart/form-data">
     <div class="quantity">
          <input type="number" step="1" min="1" max="9" name="quantity" value="1" title="<?php _e('Szt.', 'my-theme'); ?>" class="input-text qty text" size="4">
     </div>  
     <input type="hidden" name="add-to-cart" value="<?php echo esc_attr($product->id); ?>">
     <button type="submit"> <?php echo $product->single_add_to_cart_text(); ?> </button>
</form>


</li>



<?php 
}

    endwhile; 

    wp_reset_postdata(); 

    else:  
?>
<li><?php _e( 'No Products' ); ?></li>
<?php endif; ?>

</ul>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM