gpt4 book ai didi

php - 在 Woocommerce 中使用 wc_dropdown_variation_attribute_options 列出产品变体

转载 作者:行者123 更新时间:2023-12-01 21:26:50 26 4
gpt4 key购买 nike

我是 woocommerce 的新手,我正在尝试将产品变体显示为商店页面中的下拉列表。但这些变体并未填充到选择选项列表中。下面是我的代码片段。

<?php foreach ( $product->get_attributes() as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
//$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
//wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
$args=array();
$result = wc_dropdown_variation_attribute_options($args);
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach;?>

最佳答案

您那里的代码来自single-product/add-to-cart/variable.php模板。

But you can't use it with $product->get_attributes() in the foreach loop.

In this template, $attributes is $product->get_variation_attributes()

因此,以下代码将显示属性下拉列表(其中 $product 是变量产品对象的实例):

<?php
$attributes = $product->get_variation_attributes();
$attribute_keys = array_keys( $attributes );
?>

<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
$args = array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected );
wc_dropdown_variation_attribute_options( $args );
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach; ?>

As you can see the wc_dropdown_variation_attribute_options() function is made to display variation attribute dropdowns for the current variable product, BUT NOT a list of variations in a drop down.

<小时/>

在下拉列表中列出可变产品的产品变体:

要在下拉列表中列出所有变体属性,您将使用以下代码(其中 $product 是变量产品对象的实例):

$available_variations = $product->get_available_variations();

if( count($available_variations) > 0 ){

$output = '<div class="product-variations-dropdown">
<select id="available-variations" class="" name="available_variations">';

$output .= '<option value="">'. __('Choose a variation') .'</option>';

foreach( $available_variations as $variation ){
$option_value = array();

foreach( $variation['attributes'] as $attribute => $term_slug ){
$taxonomy = str_replace( 'attribute_', '', $attribute );
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name; // Attribute name
$term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name; // Attribute value term name

$option_value[] = $attribute_name . ': '.$term_name;
}
$option_value = implode( ' | ', $option_value );

$output .= '<option value="'.$variation['variation_id'].'">'.$option_value.'</option>';

}
$output .= '
</select>
</div>';

echo $output;
}

代码已经过测试并且可以工作

To get an instance of the variable product object from a defined $product_id (where $product_id is the ID of a variable product) you will use:

$product = wc_get_product($product_id);

关于php - 在 Woocommerce 中使用 wc_dropdown_variation_attribute_options 列出产品变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48435290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com