Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

php - Display SKU in Woocommerce single product page with OceanWP theme

I want to display the SKU field of each product in its signle product page. The woocommerce plugin's settings does not have that option in wordpress.

I already followed the instructions here: https://wordpress.stackexchange.com/questions/219410/how-to-show-product-sku-on-product-page/219427#219427

I tried adding to my functions.php file this code:

add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
function dev_designs_show_sku(){
    global $product;
    echo 'SKU: ' . $product->get_sku();
}

And also tried to add this code to the theme's single product content file:

if ( 'sku' === $element ) {
    woocommerce_template_single_sku();
}

file name is owp-single-product.php:

<?php
/**
 * Single product template.
 *
 * @package OceanWP WordPress theme
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

// Get price conditional display state.
$ocean_woo_single_cond = get_theme_mod( 'ocean_woo_single_conditional', false );

// Conditional vars.
$show_woo_single = '';
$show_woo_single = ( is_user_logged_in() && $ocean_woo_single_cond === true );

/**
 * Display Single Product template
 * 
 */

// Get elements.
$elements = oceanwp_woo_summary_elements_positioning();

// Loop through elements.
foreach ( $elements as $element ) {

    do_action( 'ocean_before_single_product_' . $element );

    // Title.
    if ( 'title' === $element ) {

        woocommerce_template_single_title();

    }
    
    // Sku.
    if ( 'sku' === $element ) {

        woocommerce_template_single_sku();

    }

    // Rating.
    if ( 'rating' === $element ) {

        woocommerce_template_single_rating();

    }

    // Price.
    if ( 'price' === $element ) {

        if ( false === $ocean_woo_single_cond || $show_woo_single ) {

            woocommerce_template_single_price();

        }
    }

    // Excerpt.
    if ( 'excerpt' === $element ) {

        woocommerce_template_single_excerpt();

    }

    // Quantity & Add to cart button.
    if ( 'quantity-button' === $element ) {

        if ( false === $ocean_woo_single_cond || $show_woo_single ) {

            woocommerce_template_single_add_to_cart();

        } else {

            // Get Add to Cart button message display state.
            $ocean_woo_single_msg = get_theme_mod( 'ocean_woo_single_cond_msg', 'yes' );

            if ( 'yes' === $ocean_woo_single_msg ) {

                // Get Add to Cart button replacement message.
                $ocean_woo_single_msg_txt = get_theme_mod( 'ocean_woo_single_cond_msg_text' );
                $ocean_woo_single_msg_txt = $ocean_woo_single_msg_txt ? $ocean_woo_single_msg_txt : esc_html__( 'Log in to view price and purchase', 'oceanwp' );

                $woo_single_myaccunt_link = get_theme_mod( 'ocean_single_add_myaccount_link', false );

                echo '<div class="owp-woo-single-cond-notice">';
                if ( false === $woo_single_myaccunt_link ) {
                    echo '<span>'. $ocean_woo_single_msg_txt .'</span>';
                } else {
                    echo '<a href="' . esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) . '">' . $ocean_woo_single_msg_txt . '</a>';
                }   
                echo '</div>';

            }
        }
    }

    // Meta.
    if ( 'meta' === $element ) {

        woocommerce_template_single_meta();
        
    }

    do_action( 'ocean_after_single_product_' . $element );

}

Did not work! What do I do?

question from:https://stackoverflow.com/questions/65868470/display-sku-in-woocommerce-single-product-page-with-oceanwp-theme

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this to show the sku after product title

add_action( 'ocean_after_single_product_title', 'show_product_sku', 5 );
function show_product_sku(){
    global $product;
    echo 'SKU: ' . $product->get_sku();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...