0
votes

I'm trying to customize a child theme to display the sku of variable products. The main issue is that it works in the main theme but it's called through a function and I needed to delete it because of formatting.

My client asked for a more compact and clear interface so my solution was to put everything in the price.php, however with this method when i try to call the product's Sku it only shows the main one and not the variations'.

To be more specific:
My main theme config.php has this function:

function mad_woocommerce_template_single_meta () {
        ?>

        <?php global $product; ?>

        <section class="product-section">

            <div class="product_meta">

                <?php do_action('woocommerce_product_meta_start'); ?>

                <?php if ('yes' == get_option('woocommerce_manage_stock')): ?>

                    <?php
                    $availability      = $product->get_availability();
                    $availability_html = empty( $availability['availability'] ) ? '' : '<span class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</span>';
                    ?>

                    <span class="stock_wrapper">
                        <span class="meta-title"><?php _e('Availability:', 'flatastic'); ?></span>
                        <?php echo apply_filters( 'woocommerce_stock_html', $availability_html, $availability['availability'], $product ); ?>
                    </span>

                <?php endif; ?>

                <?php if (wc_product_sku_enabled() && ($product->get_sku() || $product->is_type('variable'))) : ?>
                    <span class="sku_wrapper">
                        <span class="meta-title"><?php _e('SKU:', 'flatastic'); ?></span>
                        <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'flatastic' ); ?></span>
                    </span>
                <?php endif; ?>

                <?php do_action('woocommerce_product_meta_end'); ?>

            </div><!--/ .product_meta-->

        </section><!--/ .product-section-->

        <?php
            $post_content = !empty($product->post_excerpt) ? $product->post_excerpt : '';
            $post_content = apply_filters('the_excerpt', $post_content);
            $post_content = str_replace(']]>', ']]&gt;', $post_content);
        ?>

        <?php if (!empty($post_content)): ?>
            <section class="product-section">
                <?php echo $post_content; ?>
            </section><!--/ .product-section-->
        <?php endif; ?>

        <?php
    }

Called in

public function woocommerce_add_hooks(){ 
 add_action('woocommerce_single_product_summary', array(&$this, 'mad_woocommerce_template_single_meta'), 11); }

My code in the child theme price.php looks like this:

 <?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>
    <span class="product-code"><?php _e('SKU:', 'flatastic'); ?> <span class="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : _e( 'N/A', 'flatastic' ); ?></span></span>
   <?php endif; ?>

Also calling variations stocks from the price returns a similar issue with quantities as well.

What's the best way to solve this? I tried different solutions but none worked so far

1

1 Answers

1
votes

I'm not getting what you're at with your codes.

But with your problem, what I understand is that you'll have to loop all the children to get the sku of the variations. Something like this:

    $products = array_filter( array_map( 'wc_get_product', $product->get_children() ) );        
    foreach ( $products as $_product ) {
        echo $_product->get_sku();
    }