使用Woocommerce中的wc_dropdown_variation_attribute_options列出产品变体

使用Woocommerce中的wc_dropdown_variation_attribute_options列出产品变体

问题描述:

I am new to woocommerce and i am trying to display the product variations as a drop-down list in shop page. But the variations are not getting populated in the select option list. below is my code snippet.

<?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;?>

我是woocommerce的新手,我正在尝试将商品变体显示为商店页面中的下拉列表。 但是变量不会在选择选项列表中填充。 下面是我的代码片段。 p>

 &lt;?php foreach($ product-&gt; get_attributes()as $ attribute_name =&gt; $ options):?&gt; 
&lt  ; tr&gt; 
&lt; td class =“label”&gt;&lt; label for =“&lt;?php echo sanitize_title($ attribute_name);?&gt;”&gt;&lt;?php echo wc_attribute_label($ attribute_name);  ?&gt;&lt; / label&gt;&lt; / td&gt; 
&lt; td class =“value”&gt; 
&lt;?php 
 // $ selected = isset($ _REQUEST ['attribute_'.sanitize_title($  attribute_name)])?  wc_clean(urldecode($ _REQUEST ['attribute_'.sanitize_title($ attribute_name)])):$ product-&gt; get_variation_default_attribute($ attribute_name); 
 // wc_dropdown_variation_attribute_options(array('options'=&gt; $ options,'attribute)  '=&gt; $ attribute_name,'product'=&gt; $ product,'selected'=&gt; $ selected)); 
 $ args = array(); 
 $ result = wc_dropdown_variation_attribute_options($ args); 
 echo 结束($ attribute_keys)=== $ attribute_name?  apply_filters('woocommerce_reset_variations_link','&lt; a class =“reset_variations”href =“#”&gt;'。__('Clear','woocommerce')。'&lt; / a&gt;'):''; 
?  &gt; 
&lt; / td&gt; 
&lt; / tr&gt; 
&lt;?php endforeach;?&gt; 
  code>  pre> 
  div>

The code that you have there comes from single-product/add-to-cart/variable.php template.

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

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

So the following code will work to display the attributes dropdowns (where $product is an instance of the variable product object):

<?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 maid to display variation attribute dropdowns for the current variable product, BUT NOT a list of variations in a drop down.


List in a dropdown the product variations of a variable product:

To list all variation attributes in a dropdown you will use the following code (where $product is an instance of the variable product object):

$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;
}

Code is tested ad works

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);