按颜色显示 WooCommerce 的产品变体库存数量
Display product variation stock quantity by color for WooCommerce
我写了一个小片段来在我的产品页面中获取所有尺寸的产品变体颜色和具体数量
我得到了这个结果:
color 1 size S = quantity
Color 1 size M = quantity
Color 1 size L = quantity
Color 2 size M = quantity
Color 2 size L = quantity
...
我想要更像这样的结果
Color 1 : quantity of size S, quantity of size M, quantity of size L
Color 2 : quantity of size S, quantity of size M, quantity of size L
...
如何显示此结果?
这是我的代码:
add_action( 'woocommerce_before_variations_form', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
global $product;
if ($product->is_type( 'variable' ) && has_term( 71 , 'product_cat' ))
{
echo 'Taille(s) proposée(s) '.'</br>';
$avail_vari = $product->get_available_variations();
foreach ($avail_vari as $key => $value)
{
$vari_id = $value['variation_id'];
$vari_color = $value['attributes']['attribute_pa_couleur'];
sort($vari_color);
$vari_taille = $value['attributes']['attribute_pa_taille'];
sort($vari_taille);
$vari_obj = new WC_Product_variation($vari_id);
$vari_stock = get_post_meta( $vari_id, '_booking_lockout_field',true );
$sum+= $vari_stock;
if ( $vari_stock > 0 ) {
echo $vari_color . " en " . $vari_taille . " : " . $vari_stock . " ".'</br>';
}
}
echo 'Soit un total de : ' . $sum .' costume(s) disponible'.'</br>';
}
}
以下应该可以解决问题:
add_action( 'woocommerce_before_variations_form', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
global $product;
if ($product->is_type('variable') && has_term(71 , 'product_cat') ) {
$sum = 0;
$data = array();
// Loop through available variation IDs (visible)
foreach ( $product->get_visible_children() as $variation_id ) {
$variation = wc_get_product($variation_id);
$color = $variation->get_attribute('pa_couleur');
$size = $variation->get_attribute('pa_taille');
$stock = $variation->get_meta('_booking_lockout_field');
if ( $stock > 0 ) {
$sum += $stock;
$data[$color][] = sprintf( __('%s taille(s) %s'), $stock, $size );
}
}
echo __("Taille(s) proposée(s)").' </br>';
foreach ( $data as $color => $values ) {
echo __("Color ") . $color . ' : ' . implode(', ', $values) . '<br>';
}
echo sprintf( __("Soit un total de %s costume(s) disponible(s)"), $sum ) . '</br>';
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。
我写了一个小片段来在我的产品页面中获取所有尺寸的产品变体颜色和具体数量 我得到了这个结果:
color 1 size S = quantity
Color 1 size M = quantity
Color 1 size L = quantity
Color 2 size M = quantity
Color 2 size L = quantity
...
我想要更像这样的结果
Color 1 : quantity of size S, quantity of size M, quantity of size L
Color 2 : quantity of size S, quantity of size M, quantity of size L
...
如何显示此结果?
这是我的代码:
add_action( 'woocommerce_before_variations_form', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
global $product;
if ($product->is_type( 'variable' ) && has_term( 71 , 'product_cat' ))
{
echo 'Taille(s) proposée(s) '.'</br>';
$avail_vari = $product->get_available_variations();
foreach ($avail_vari as $key => $value)
{
$vari_id = $value['variation_id'];
$vari_color = $value['attributes']['attribute_pa_couleur'];
sort($vari_color);
$vari_taille = $value['attributes']['attribute_pa_taille'];
sort($vari_taille);
$vari_obj = new WC_Product_variation($vari_id);
$vari_stock = get_post_meta( $vari_id, '_booking_lockout_field',true );
$sum+= $vari_stock;
if ( $vari_stock > 0 ) {
echo $vari_color . " en " . $vari_taille . " : " . $vari_stock . " ".'</br>';
}
}
echo 'Soit un total de : ' . $sum .' costume(s) disponible'.'</br>';
}
}
以下应该可以解决问题:
add_action( 'woocommerce_before_variations_form', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
global $product;
if ($product->is_type('variable') && has_term(71 , 'product_cat') ) {
$sum = 0;
$data = array();
// Loop through available variation IDs (visible)
foreach ( $product->get_visible_children() as $variation_id ) {
$variation = wc_get_product($variation_id);
$color = $variation->get_attribute('pa_couleur');
$size = $variation->get_attribute('pa_taille');
$stock = $variation->get_meta('_booking_lockout_field');
if ( $stock > 0 ) {
$sum += $stock;
$data[$color][] = sprintf( __('%s taille(s) %s'), $stock, $size );
}
}
echo __("Taille(s) proposée(s)").' </br>';
foreach ( $data as $color => $values ) {
echo __("Color ") . $color . ' : ' . implode(', ', $values) . '<br>';
}
echo sprintf( __("Soit un total de %s costume(s) disponible(s)"), $sum ) . '</br>';
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。