Get and display the selected variation SKU in WooCommerce

前端 未结 3 1986
旧时难觅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();
    

提交回复
热议问题