Ric*_*cky 2 php wordpress product variations woocommerce
我是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;?>
Run Code Online (Sandbox Code Playgroud)
您那里的代码来自single-product/add-to-cart/variable.php模板。
但是您不能
$product->get_attributes()在foreach循环中使用它。在此模板中,
$attributes是$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; ?>
Run Code Online (Sandbox Code Playgroud)
如您所见,该
wc_dropdown_variation_attribute_options()函数可以显示当前变量产品的变体属性下拉列表,但不能显示下拉列表中的变体列表。
在下拉列表中列出可变产品的产品变体:
要在下拉列表中列出所有变体属性,您将使用以下代码(其中$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;
}
Run Code Online (Sandbox Code Playgroud)
代码经过测试的广告作品
要从已定义的$ product_id(其中$ product_id是变量产品的ID)获取变量产品对象的实例,您将使用:
Run Code Online (Sandbox Code Playgroud)$product = wc_get_product($product_id);
| 归档时间: |
|
| 查看次数: |
3425 次 |
| 最近记录: |