Get and display the selected variation SKU in WooCommerce

前端 未结 3 1985
旧时难觅i
旧时难觅i 2021-01-20 17:14

I have this code that works for simple product type but not for variable products in WooCommerce:

add_shortcode( \'product_sku_div\', \'wc_product_sku_div\'         


        
相关标签:
3条回答
  • 2021-01-20 17:24

    You need the Variation ID to get the SKU of Variable Product.

    If you pass the Variation ID in the below Function, then you can get its SKU.

    $product = new WC_Product_Variation($variation_id);
    $product->get_sku();
    

    The below code can be used to get the variations of a product. Then lopp the result to get the variation ID and then use that variation ID in the above code to get the result.

    $variations = $product->get_available_variations();
    
    0 讨论(0)
  • 2021-01-20 17:27

    To make it work also for variable products and their variations, it requires Javascript (jQuery) to get the selected variation SKU for variable products.

    Try the following that works for simple an variable product types, displaying the selected variation SKU for variable products:

    add_shortcode( 'product_sku_div', 'wc_product_sku_div');
    function wc_product_sku_div() {
        global $product;
    
        if( ! is_a('WC_Product', $product) ) {
            $product = wc_get_product( get_the_id() );
        }
    
        ## 1 - For variable products (and their variations)
        if( $product->is_type('variable') ) {
            ob_start(); // Starting buffering
    
            ?>
            <div class="widget" sp-sku=""></div>
            <script type="text/javascript">
            jQuery( function($){
                $('form.variations_form').on('show_variation', function( event, data ){
                    $( 'div.widget' ).attr( 'sp-sku', data.sku );
                    // For testing
                    console.log( 'Variation Id: ' + data.variation_id + ' | Sku: ' + data.sku );
                });
                $('form.variations_form').on('hide_variation', function(){
                    $( 'div.widget' ).attr( 'sp-sku', '' );
                });
            });
            </script><?php
    
            return ob_get_clean(); // return the buffered content
        }
        ## 2 - For other products types
        else {
            return sprintf( '<div class="widget" sp-sku="%s"></div>', $product->get_sku() );
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.

    0 讨论(0)
  • 2021-01-20 17:28

    You need to use get_available_variations();.

    add_shortcode( 'product_sku_div', 'wc_product_sku_div'); 
    function wc_product_sku_div() { 
        global $product;
    
        $variants_array = $product->get_available_variations();
    
        for ($i = 0; $i <  count($variants_array); $i++ ) {
            return sprintf( '<div class="widget" sp-sku="%s"></div>', $variants_array[$i]['sku'] );
        }
    }
    

    Reference:

    • get_available_variations() ~ https://docs.woocommerce.com/wc-apidocs/class-WC_Product_Variable.html
    0 讨论(0)
提交回复
热议问题