在 WooCommerce*(最新版本)* 我有一个变量产品Id:9`。
使用下面的变体属性,我创建了多个产品变体。
然后我想从父产品 ID (Id: 9) 和以下属性值中获取特定的产品变体:
<attribute_for_variation>: <attribute_value_to_filter>
pa_size: size_8x10
pa_material: mat_luster_photo_paper
pa_frame: fra_silver_wood
pa_mat_usage: musa_yes
下面是该变体的屏幕截图:
我已经尝试了以下代码及其相应的结果。
为简单起见,现在只尝试使用 pa_frame 属性。
尝试 1:
static function filterVariations() {
$query = [
'post_parent' => 9,
'post_status' => 'publish',
'post_type' => ['product_variation'],
'posts_per_page' => -1,
];
$result = [];
$wc_query = new \WP_Query($query);
while ($wc_query->have_posts()) {
$wc_query->next_post();
$result[] = $wc_query->post;
}
return $result;
}
// ---> RESULT: all the variations, that's OK
尝试 2:
static function filterVariations() {
$query = [
'post_parent' => 9,
'post_status' => 'publish',
'post_type' => ['product_variation'],
'posts_per_page' => -1,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'pa_frame',
'field' => 'slug',
'terms' => [ 'fra_silver_wood' ],
],
],
];
$result = [];
$wc_query = new \WP_Query($query);
while ($wc_query->have_posts()) {
$wc_query->next_post();
$result[] = $wc_query->post;
}
return $result;
}
// ---> RESULT: empty list
如何返回具有特定属性值的所有变体?
最佳答案
可变产品中的产品属性在 wp_postmeta 数据库表中设置为元数据。然后您将需要使用元查询而不是税务查询。试试这个:
static function filterVariations() {
$query = new \WP_Query( array(
'post_parent' => 9,
'post_status' => 'publish',
'post_type' => 'product_variation',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => 'attribute_pa_frame',
'value' => 'fra_silver_wood',
) ),
) );
$result = array();
if($query->have_posts()){
while ($query->have_posts()) {
$query->next_post();
$result[] = $query->post;
}
wp_reset_postdata();
}
wp_reset_query();
return $result;
}
现在应该可以正常工作了……
对于您问题中列出的多个产品属性对(键/值),您只需在 WP_Query 中以这种方式使用它们:
public function filterVariations() {
$query = new \WP_Query( array(
'post_parent' => 40,
'post_status' => 'publish',
'post_type' => 'product_variation',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'attribute_pa_size',
'value' => 'size_8x10',
),
array(
'key' => 'attribute_pa_material',
'value' => 'mat_luster_photo_paper',
),
array(
'key' => 'attribute_pa_frame',
'value' => 'fra_silver_wood',
),
array(
'key' => 'attribute_pa_mat_usage',
'value' => 'musa_yes',
),
),
) );
$result = array();
if($query->have_posts()){
while ($query->have_posts()) {
$query->next_post();
$result[] = $query->post;
}
wp_reset_postdata();
}
wp_reset_query();
return $result;
}
然后你会正常得到相应的产品变体(只有一个)......
Note: product attributes meta keys start all with
attribute_pa_instead of justpa_
关于php - 获取 Woocommerce 中特定产品属性值的所有产品变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122976/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c