首页 > 解决方案 > 检查自定义搜索中的帖子类型以获取 Woocommerce 产品属性

问题描述

我正在实施一个 woocommerce 网站。在这里,我提供了一个搜索栏来仅搜索产品。

<form action="<?php echo get_site_url() ?>" method="GET">
    <div class="input-group">
         <input type="search" name="s" class="form-control" placeholder="Search Products" required>
         <input type="hidden" name="post_type" value="products" />
         <div class="input-group-btn">
             <button class="btn btn-default" type="submit">
                <i class="fa fa-search" aria-hidden="true"></i>
             </button>
         </div>
    </div>
</form>

上面的搜索代码结果single-product页面。在单个产品页面的侧边栏中,我有一个显示product attributes和的部分attribute terms

以下代码用于获取当前页面中可用的产品尺寸 ( product-single)

// get available sizes  for woocommerce sidebar
function getSizes(){
  $sizes = array();
  $codes = array();
  if ( have_posts() ) :
     while ( have_posts() ) : the_post();
     echo get_the_title();
     $product = wc_get_product();
     $attributes = $product -> get_attributes('size');
     foreach ($attributes as $key => $attribute) {
        foreach ($attribute['options'] as $key => $meta) {
          $meta_array = get_term_meta($meta);

          if($meta_array['label']){
            $temp = array();
            $term = get_term($meta);
            foreach ($term as $key => $value) {
              if($key == 'term_id'){
                $temp['term_id'] = $value;
              }
              if($key == 'name'){
                $temp['name'] = $value;
              }
              $temp['attribute'] = 'size';
            }
            foreach ($meta_array['label'] as $key => $value) {
              if (!in_array($value, $codes)){
                $temp['size'] =  $value;
                array_push($sizes,$temp);
                array_push($codes,$value);
              }
            }
          }
        }
     }
   endwhile; endif; return $sizes;
 }

这是html我打印它们的地方HTML-DOM

<ul class="w-woo-inner-box">

  <?php $sizes = getaSizes();
      if(!empty($sizes)):
         foreach ($sizes as $key => $size) { ?>
          <li>
            <label class="checkbox inline-block <?php echo checkurlactive($size['attribute'], $size['term_id']); ?>">
                                  <a href="<?php echo generate_filter_url($size['attribute'], $size['term_id']) ?>">
                                    <input type="checkbox" class="">
                                    <label>&nbsp;</label>
            <span><?php echo $size['size']; ?></span>
            </a>
            </label>
          </li>
        <?php }
       else: ?>
            <li class="no-val-found"><span>No sizes found!</span> </li>
    <?php endif; ?>
</ul>

问题: 这是一个巨大的描述。好的,现在让我们看看问题,从上面的代码中,当我搜索它时existing product name它工作正常。但是当我搜索一些值时,页面上other post types有一个php error提到

Fatal error: Call to a member function get_attributes() on boolean in /..../functions.php on line 482

我试图调试它,我发现下面的循环getSizes()返回另一个post type values

if ( have_posts() ) :
 while ( have_posts() ) : the_post();

这里有什么问题?我怎样才能避免搜索另一个post type values

标签: phpwordpressmethodswoocommerceproduct

解决方案


在您的函数代码中,您需要在循环之前检查帖子类型,例如:

if ( have_posts() ) :
    while ( have_posts() ) : the_post();

        // Check for "product" post type or continue
        if( 'product' !== get_post_type() ) {
            continue; // jump to next product

        echo get_the_title();

        // Get an instance of the WC_Product Object
        $product = wc_get_product( get_the_id() );

这将避免您得到的错误,因为$product它将始终是一个WC_Product对象。


奖金补充:

现在该WC_Product方法get_attributes()有一个参数$context可以有 2 个值:

  • 'view'(默认值)
  • 'edit' (用于后端)

不是: $attributes = $product -> get_attributes('size');

因此,如果您想获取特定属性,您将使用inteadWC_Product方法,该方法将给出一个逗号分隔的术语名称字符串,例如:get_attribute()

        // Get an instance of the WC_Product Object
        $product = wc_get_product( get_the_id() );

        // Loop through 'size' product attribute
        $size_values = $product->get_attribute('size');

        // Convert to an array of term names
        $term_names = (array) explode(', ', $size_values);

        // The product attribute taxonomy (always start with "pa_" + the slug)
        $taxonomy = 'pa_size';

        // Loop through term names
        foreach( $term_names as $term_name ) {
            // Get the WP_Term object (if needed)
            $term = get_term_by( 'name', $term_name, $taxonomy );

            $term_id   = $term->term_id;
            $term_slug = $term->slug;
        }

推荐阅读