修改WooCommerce存档产品页面以显示每个产品的SKU

时间:2019-08-18 00:47:17

标签: php woocommerce

作为一个标准,WooCommerce存档页面显示产品,图像,标题和购买“按钮”并排堆叠。我想要的是也要显示自定义字段,例如SKU,颜色等。

我尝试添加

$block_data['sku'] = $product->get_sku();

然后

$layout['sku'] = array();

代码的结果(这只是.php文件的一部分):

$block_data['content'] = get_the_content();
$block_data['classes'] = $block_classes;
$block_data['tmb_data'] = $tmb_data;
$block_data['id'] = $post->ID;
$block_data['media_id'] = $item_thumb_id;
$block_data['title_classes'] = $title_classes;
$block_data['link'] = get_permalink();
$block_data['text_length'] = 300;
$block_data['product'] = true;
$block_data['sku'] = $product->get_sku();

$categories_id = array();
$categories_link = array();
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
if (isset($woo_categories) && !empty($woo_categories)) {
    foreach ( $woo_categories as $woo_cat ) {
        $woo_cat_id = $woo_cat->term_id; //category ID
        $woo_cat_name = $woo_cat->name; //category name



$block_data['single_categories_id'] = $categories_id;
$block_data['single_categories'] = $categories_link;


if ($item_thumb_id !== '') {
    $layout['media'] = array();

} else {
    $layout['media'] = array('placeholder');

}
$layout['title'] = array();
$layout['price'] = array();
$layout['sku'] = array();

我希望输出结果为此订单图像-title -price-SKU,但不会输出任何SKU

1 个答案:

答案 0 :(得分:0)

将此添加到您当前的活动主题functions.php

产品标题后添加SKU

function woocommerce_item_sku_on_archive()  {
    global $product;
    if ( $product->get_sku() ) {
        echo $product->get_sku();
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_item_sku_on_archive');

产品价格过后增加SKU

function custom_sku_after_price_loop( $price ) { 
    global $product;
    if ( $product->get_sku() ) {
        $sku = $product->get_sku();
        return $price . '<br /><span class="product-sku">' . $sku . '</span>';
    }else { 
        return $price; 
    } 
}
add_filter( 'woocommerce_get_price_html', 'custom_sku_after_price_loop' );