Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce:检索可变产品选项的SKU_Php_Wordpress_Woocommerce - Fatal编程技术网

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

Php Woocommerce:检索可变产品选项的SKU,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在Woocommerce中,variable.php中有一块代码,显示变体选项。我需要在标题旁边的列表中显示每个单独变量的SKU。是否有一种类似于$product->get_sku 这是完整的块: global $product; $attribute_keys = array_keys( $attributes ); <?php foreach ( $attributes as $name => $options ) : ?>

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

谢谢。

将您的foreach替换为:

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