如何在产品页面上显示产品SKU

时间:2016-03-01 作者:CreationP

我正在制作一个网站,似乎产品sku被隐藏在产品页面中。我已经找到了如何将其添加到目录(商店)页面,但我需要它显示在产品页面内。

到目前为止,通过改变单一产品。php,我设法将其添加到页面的末尾(我们不想要的东西)或页面左上角的标题之前(我们也不想要的东西)。

我没有办法把它加在价格之前和产品名称下面。

主题的单一产品代码。php:

    <?php
/**
 * Single Product title
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/title.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
 * will need to copy the new files to your theme to maintain compatibility. We try to do this.
 * as little as possible, but it does happen. When this occurs the version of the template file will.
 * be bumped and the readme will list any important changes.
 *
 * @see         http://docs.woothemes.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 1.6.4
 */

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

?>
<?php echo \'<div class="sku">\' . $product->sku . \'</div>\'; ?>
我添加了最后一行。

然而,关于主题/woocommerce/单一产品/meta。php我可以看到应该显示sku,而不是:

<?php
/**
 * Single Product Meta
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly

global $post, $product;
?>
<div class="product_meta">

    <?php if ( $product->is_type( array( \'simple\', \'variable\' ) ) && get_option(\'woocommerce_enable_sku\') == \'yes\' && $product->get_sku() ) : ?>
        <span itemprop="productID" class="sku"><?php _e(\'SKU:\',\'qns\' ); ?> <?php echo $product->get_sku(); ?>.</span>
    <?php endif; ?>

    <?php echo $product->get_categories( \', \', \' <span class="posted_in">\'.__(\'Category:\',\'qns\' ).\' \', \'.</span>\'); ?>

    <?php echo $product->get_tags( \', \', \' <span class="tagged_as">\'.__(\'Tags:\',\'qns\' ).\' \', \'.</span>\'); ?>

</div>
我可以在产品页面中显示产品SKU编号吗?

提前谢谢。

3 个回复
最合适的回答,由SO网友:Joe Dooley 整理而成

将此添加到您的函数中。php

add_action( \'woocommerce_single_product_summary\', \'dev_designs_show_sku\', 5 );
function dev_designs_show_sku(){
    global $product;
    echo \'SKU: \' . $product->get_sku();
}
这将在产品标题下方输出产品SKU。请参见下图。产品SKU为VERTEX-SLVR。

enter image description here

SO网友:Zoe

在(子)主题中添加以下代码functions.php:

function visupporti_get_product_quantity( $atts ) {

global $product;

$atts = shortcode_atts( array(
\'id\' => ”,
), $atts );

// If no id, we’re probably on a product page already
if ( empty( $atts[\'id\'] ) ) {

$sku = $product->get_stock_quantity( );

} else {

//get which product from ID we should display a SKU for
$product = wc_get_product( $atts[\'id\'] );
$sku = $product->get_stock_quantity( );

}

ob_start();

// Only echo if there is a SKU
if ( !empty( $sku ) ) {
echo $sku;
}

return ob_get_clean();

}
add_shortcode( \'wc_sku\', \'visupporti_get_product_quantity\' );
查看更多详细信息how to display SKU on the site.

SO网友:John Fotios

添加到函数。php,仅显示产品是否有SKU

    add_action( \'woocommerce_single_product_summary\', \'dev_designs_show_sku\', 5 );
    function dev_designs_show_sku(){
        global $product;
        $sku = $product->get_sku();
        if ($sku != null) {
            echo \'SKU: \' . $product->get_sku();
        }
    }

相关推荐

如何在Functions.php中链接style.css

我是WordPress的新手;我刚开始学习WordPress。我想把风格联系起来。函数中的css。php,但我无法解决这里可能存在的问题。谁能给我指出正确的方向吗?指数php<?php get_header(); ?> <?php if ( have_posts() ) { while ( have_posts() ) { the_post();