Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Woocommerce shortcode to display variation SKU

$
0
0

Replies: 0

Hi,

Hope someone can help me out?

I am building a new shop with Woocommerce and elementor and i want to display the SKU of a variable product on the product page, i need a shortcode for this. The default shortcode shows the SKU of the parent product wich is not right.

The SKU needs to change whenever the user changes te dropdown.

I found the code below but i need a shortcode instead.
The code below adds the right sku on the variable product description.

add_action('woocommerce_single_product_summary', 'display_product_sku_and_gtin', );
function display_product_sku_and_gtin() {
    global $product; 

    echo '<p>' . __("SKU:") . ' ' . $product->get_sku() . '</p>';

    if( get_post_meta( $product->get_id(), 'barcode', true) )
        echo '<p>' . __("GTIN:") . ' ' . get_post_meta( $product->get_id(), 'barcode', true) . '</p>';                  
}
// Display product variations SKU and GTIN info
add_filter( 'woocommerce_available_variation', 'display_variation_sku_and_gtin', 20, 3 );
function display_variation_sku_and_gtin( $variation_data, $product, $variation ) {
    $html = ''; // Initializing

    // Inserting SKU
    if( ! empty( $variation_data['sku'] ) ){
        $html .= '</div><div class="woocommerce-variation-sku">' . __('SKU:') . ' ' . $variation_data['sku'];
    }

    // Inserting GTIN
    if( get_post_meta( $variation->get_id(), 'barcode', true ) ){
        $gtin = get_post_meta( $variation->get_id(), 'barcode', true );
        $html .= '</div><div class="woocommerce-variation-gtin">' . __('GTIN:') . ' ' . $gtin;
    }

    // Using the variation description to add dynamically the SKU and the GTIN
    $variation_data['variation_description'] .= $html;

    return $variation_data;
}

Viewing all articles
Browse latest Browse all 59525

Trending Articles