Woocommerce:检索可变产品选项的SKU

时间:2018-08-21 20:18:01

标签: php wordpress woocommerce

在Woocommerce中,variable.php中有一段代码显示变体选项。我需要在标题旁边的列表中显示每个单独版本的SKU。有什么方法可以类似于$product->get_sku;

这是完整的内容:

    global $product;
    $attribute_keys = array_keys( $attributes );

                <?php foreach ( $attributes as $name => $options ) : ?>
                    <tr class="attribute-<?php echo sanitize_title($name); ?>">
                        <?php
                        $sanitized_name = sanitize_title( $name );
                        if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
                            $checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
                        } elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
                            $checked_value = $selected_attributes[ $sanitized_name ];
                        } else {
                            $checked_value = '';
                        }
                        ?>
                        <td class="value">
                            <?php
                            if ( ! empty( $options ) ) {
                                if ( taxonomy_exists( $name ) ) {
                                    // Get terms if this is a taxonomy - ordered. We need the names too.
                                    $terms = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'all' ) );

                                    foreach ( $terms as $term ) {
                                        if ( ! in_array( $term->slug, $options ) ) {
                                            continue;
                                        }
                                        print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
                                    }
                                } else {
                                    foreach ( $options as $option ) {

                                        echo $product->get_sku; // This is where the variation SKU would go

                                        print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
                                    }
                                }
                            }

                            echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                            ?>
                        </td>
                    </tr>
                <?php endforeach; ?>

在此部分中,变体SKU将位于此位置:

foreach ( $options as $option ) {
    echo $product->get_sku; // This is where the variation SKU would go
    print_attribute_radio( $checked_value, $option, $option, 
    $sanitized_name );
}

谢谢。

1 个答案:

答案 0 :(得分:1)

将您的foreach替换为:

$product = new WC_Product_Variable( $post->ID );
$variations = $product->get_available_variations();
foreach ($variations as $key => $variation) {
    $sku = $variation['sku'];
    $options = $variation['attributes'];
 .
 .
 .