Get and display the selected variation SKU in WooCommerce

前端 未结 3 1984
旧时难觅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: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( '
    ', $variants_array[$i]['sku'] ); } }

    Reference:

    • get_available_variations() ~ https://docs.woocommerce.com/wc-apidocs/class-WC_Product_Variable.html

提交回复
热议问题