Show SKU on cart and checkout pages in Woocommerce 3

混江龙づ霸主 提交于 2020-08-22 06:40:07

问题


I would like to display SKU on cart (Under product column ) and checkout page.

I searched SO, but all answers are for old versions of WooCommerce and non of them is for 3.x.

How can I show SKU on cart and checkout pages in Woocommerce 3?


回答1:


2020 Update: How display product SKU directly under item name in WooCommerce?

You can do it with a custom unction hooked in woocommerce_cart_item_name action hook, this way:

add_filter( 'woocommerce_cart_item_name', 'showing_sku_in_cart_items', 99, 3 );
function showing_sku_in_cart_items( $item_name, $cart_item, $cart_item_key  ) {
    // The WC_Product object
    $product = $cart_item['data'];
    // Get the  SKU
    $sku = $product->get_sku();

    // When sku doesn't exist
    if(empty($sku)) return $item_name;

    // Add the sku
    $item_name .= '<br><small class="product-sku">' . __( "SKU: ", "woocommerce") . $sku . '</small>';

    return $item_name;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works on WooCommerce 3+. You will get:

And

Related similar: How to Show SKU with product title in Order received page and Email order




回答2:


You'll need to do some template overrides.

Cart

Copy plugins/woocommerce/templates/cart/cart.php into your theme file at my_theme/woocommerce/cart/cart.php if it isn't already there. Then add the following at approx line #85

// Meta data
//  (this is already in cart.php; look for it for where to place the next snippet)
echo WC()->cart->get_item_data( $cart_item );

// Add SKU below product name
if ( !empty($_product->get_sku()) ) { ?>
<div class="sku">
    <small><?php echo "SKU: ". $_product->get_sku(); ?></small>
</div>
<?php }

Checkout

Copy plugins/woocommerce/templates/checkout/review-order.php into your theme file at my_theme/woocommerce/checkout/review-order.php if it isn't already there. Then add the following at approx line #43

<?php
//  (this is already in review-order.php; look for it for where to place the next snippet)
echo WC()->cart->get_item_data( $cart_item ); ?>

<?php
// Add SKU below product name
if ( !empty($_product->get_sku()) ) { ?>
<div class="sku">
    <small><?php echo "SKU: ". $_product->get_sku(); ?></small>
</div>
<?php } ?>



回答3:


You can do it with this way:

/**
 * @snippet       Show SKU @ WooCommerce Cart
 * @author        Khalid Almallahi
 */

add_action( 'woocommerce_after_cart_item_name', 'abukotsh_sku_below_cart_item_name', 11, 2 );

function abukotsh_sku_below_cart_item_name( $cart_item, $cart_item_key ) {
    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    $sku = $_product->get_sku();
    if ( ! $sku ) return;
    echo '<p><small>SKU: ' . $sku . '</small></p>';
}


来源:https://stackoverflow.com/questions/45849475/show-sku-on-cart-and-checkout-pages-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!