获取与 WooCommerce 中的默认属性值相关的产品变体
Get the Product Variation related to default attribute values in WooCommerce
我想在我的前端模板文件中显示默认的产品属性表单值及其正常价格。
下面的var_dump
显示了数组中的选项。 我需要获取 [default_attributes]
个值。
<?php
global $product;
echo var_dump( $product );
// Need to get the [default_attributes] values
?>
要获取可变产品的默认属性,您可以使用 WC_Product
方法 get_default_attributes()
这样:
<?php
global $product;
if( $product->is_type('variable') ){
$default_attributes = $product->get_default_attributes();
// Testing raw output
var_dump($default_attributes);
}
?>
现在要找出哪个是此 "defaults" 属性对应的产品变体,有点复杂:
<?php
global $product;
if( $product->is_type('variable') ){
$default_attributes = $product->get_default_attributes();
foreach($product->get_available_variations() as $variation_values ){
foreach($variation_values['attributes'] as $key => $attribute_value ){
$attribute_name = str_replace( 'attribute_', '', $key );
$default_value = $product->get_variation_default_attribute($attribute_name);
if( $default_value == $attribute_value ){
$is_default_variation = true;
} else {
$is_default_variation = false;
break; // Stop this loop to start next main lopp
}
}
if( $is_default_variation ){
$variation_id = $variation_values['variation_id'];
break; // Stop the main loop
}
}
// Now we get the default variation data
if( $is_default_variation ){
// Raw output of available "default" variation details data
echo '<pre>'; print_r($variation_values); echo '</pre>';
// Get the "default" WC_Product_Variation object to use available methods
$default_variation = wc_get_product($variation_id);
// Get The active price
$price = $default_variation->get_price();
}
}
?>
这已经过测试并且有效。
我在寻找比我更简单的方法时发现了这个问题,也许是内置的 WooCommerce 功能,但现在我们仍然需要自己找到它。
我做的有点不同,通过使用 microtime 测试它在我的硬件上的性能,它的运行时间大约是原来的 1/2000。这是我的代码:
function setDefaultVariation(): array {
global $product;
$all_variations = $product->get_available_variations();
$default_attributes = $product->get_default_attributes();
if (empty($default_attributes))
return $all_variations[0];
$default_variation_key = -1;
foreach ($all_variations as $variation) {
$default_variation_key++;
//Check if variation has more or less assigned attributes than the default.
if (getAssignedAttributeCount($variation['attributes']) !== count($default_attributes))
continue;
$is_default = 0; //This will count how many attributes match the default values
//We check each default attribute term and value to see if it matches the term-value pairs of the current variation. Some might have multiple pairs, so we use a counter to know if all are matched
foreach ($default_attributes as $default_term => $default_value) {
if($variation['attributes']['attribute_' . $default_term] !== $default_value) {
break; //A attribute value does not match so this one cant be default, break.
}else{
$is_default++;
if ($is_default === count($default_attributes) ) { //All default attributes matches this variation
return $all_variations[$default_variation_key];
}
}
}
}
return $all_variations[0];//If this statement is reached, no default variation was found, so return key of first variation
}
function getAssignedAttributeCount($attributes): int {
$count = 0;
foreach ($attributes as $attribute) {
if ($attribute !== '')
$count++;
}
return $count;
}
代码找到产品的默认属性,然后遍历每个变体以检查变体的属性是否与默认属性匹配。
它支持具有多个属性的产品,并且还通过比较属性的数量来检查变体是否具有确切的属性,例如(NULL 是未设置属性默认值的地方):
Default
|0|NULL|
Variation 1:
|0|2| -> The 0 matches, but the 2 does not, so this is not default!
Variation 2:
|0|NULL| -> This is the default
为此,我添加了 getAssignedAttributeCount()
函数。
另请注意,如果未设置默认变体,或未找到与默认值匹配的变体,则返回第一个变体。
我想在我的前端模板文件中显示默认的产品属性表单值及其正常价格。
下面的var_dump
显示了数组中的选项。 我需要获取 [default_attributes]
个值。
<?php
global $product;
echo var_dump( $product );
// Need to get the [default_attributes] values
?>
要获取可变产品的默认属性,您可以使用 WC_Product
方法 get_default_attributes()
这样:
<?php
global $product;
if( $product->is_type('variable') ){
$default_attributes = $product->get_default_attributes();
// Testing raw output
var_dump($default_attributes);
}
?>
现在要找出哪个是此 "defaults" 属性对应的产品变体,有点复杂:
<?php
global $product;
if( $product->is_type('variable') ){
$default_attributes = $product->get_default_attributes();
foreach($product->get_available_variations() as $variation_values ){
foreach($variation_values['attributes'] as $key => $attribute_value ){
$attribute_name = str_replace( 'attribute_', '', $key );
$default_value = $product->get_variation_default_attribute($attribute_name);
if( $default_value == $attribute_value ){
$is_default_variation = true;
} else {
$is_default_variation = false;
break; // Stop this loop to start next main lopp
}
}
if( $is_default_variation ){
$variation_id = $variation_values['variation_id'];
break; // Stop the main loop
}
}
// Now we get the default variation data
if( $is_default_variation ){
// Raw output of available "default" variation details data
echo '<pre>'; print_r($variation_values); echo '</pre>';
// Get the "default" WC_Product_Variation object to use available methods
$default_variation = wc_get_product($variation_id);
// Get The active price
$price = $default_variation->get_price();
}
}
?>
这已经过测试并且有效。
我在寻找比我更简单的方法时发现了这个问题,也许是内置的 WooCommerce 功能,但现在我们仍然需要自己找到它。
我做的有点不同,通过使用 microtime 测试它在我的硬件上的性能,它的运行时间大约是原来的 1/2000。这是我的代码:
function setDefaultVariation(): array {
global $product;
$all_variations = $product->get_available_variations();
$default_attributes = $product->get_default_attributes();
if (empty($default_attributes))
return $all_variations[0];
$default_variation_key = -1;
foreach ($all_variations as $variation) {
$default_variation_key++;
//Check if variation has more or less assigned attributes than the default.
if (getAssignedAttributeCount($variation['attributes']) !== count($default_attributes))
continue;
$is_default = 0; //This will count how many attributes match the default values
//We check each default attribute term and value to see if it matches the term-value pairs of the current variation. Some might have multiple pairs, so we use a counter to know if all are matched
foreach ($default_attributes as $default_term => $default_value) {
if($variation['attributes']['attribute_' . $default_term] !== $default_value) {
break; //A attribute value does not match so this one cant be default, break.
}else{
$is_default++;
if ($is_default === count($default_attributes) ) { //All default attributes matches this variation
return $all_variations[$default_variation_key];
}
}
}
}
return $all_variations[0];//If this statement is reached, no default variation was found, so return key of first variation
}
function getAssignedAttributeCount($attributes): int {
$count = 0;
foreach ($attributes as $attribute) {
if ($attribute !== '')
$count++;
}
return $count;
}
代码找到产品的默认属性,然后遍历每个变体以检查变体的属性是否与默认属性匹配。
它支持具有多个属性的产品,并且还通过比较属性的数量来检查变体是否具有确切的属性,例如(NULL 是未设置属性默认值的地方):
Default
|0|NULL|
Variation 1:
|0|2| -> The 0 matches, but the 2 does not, so this is not default!
Variation 2:
|0|NULL| -> This is the default
为此,我添加了 getAssignedAttributeCount()
函数。
另请注意,如果未设置默认变体,或未找到与默认值匹配的变体,则返回第一个变体。