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

标签 php wordpress woocommerce

在 Woocommerce 中,variable.php 中有一个代码块,用于显示变体选项。我需要在标题旁边的列表中显示每个单独变体的 SKU。有没有一种类似于 $product->get_sku; 的方法可以做到这一点?

这是完整的 block :

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

谢谢。

最佳答案

将 foreach 替换为:

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

关于php - Woocommerce:检索可变产品选项的 SKU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956110/

相关文章:

php - WooCommerce:更改图库缩略图的大小

php - WooCommerce 礼品卡插件实际上在哪里创建数据库记录?

php - 如何从特定字符中剪切动态字符串并将剩余部分保留在变量中

PHP 服务器 ping 似乎导致 502 错误

php - 使用正则表达式和四行上下文匹配搜索

php - WooCommerce API 不使用 SKU 创建产品

php - 在 Woocommerce 中获取可变产品的正常价格

PHP 下载带有大文件隐藏 URL 链接的简历

php - Laravel 护照非过期 token

php - URL 重写不起作用,a2enmod 处于事件状态,使用“覆盖全部”,.htaccess 正确