php - 检查自定义搜索中的帖子类型以获取 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> </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
?
解决方案
在您的函数代码中,您需要在循环之前检查帖子类型,例如:
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;
}
推荐阅读
- video-streaming - 预加载视频段大小
- node.js - 如何在 Node 中使用 saxon-js 处理 XPath 表达式
- r - 如何从R中的文本文件制作数据框
- python-3.x - 我的登录视图中的 Django 的 Authenticate 方法无法正常工作。它甚至不验证管理员创建的用户
- spring-boot - 将 HTTPS 从 Elastic Load Balancer 重定向到后端服务器
- angular - 在 FormArray 中将焦点设置为动态创建的 FormControl
- c# - ConfirmEmailAsync() 无论如何都会得到无效的令牌
- html - 当我尝试在网页上嵌入处理草图时,为什么会出现空白屏幕?
- twitter-bootstrap - 将 div 与 Bootstrap 列中的相邻 div 对齐
- vue.js - 我可以在 vuejs 上使用 highchart 插件创建日历热图吗