【问题标题】:Show SKU on cart and checkout pages in Woocommerce 3在 Woocommerce 3 的购物车和结帐页面上显示 SKU
【发布时间】:2018-02-01 14:52:10
【问题描述】:

我想在购物车(在产品列下)和结帐页面上显示 SKU。

我搜索了 SO,但所有答案都是针对旧版本的 WooCommerce,而不是针对 3.x。

如何在 Woocommerce 3 的购物车和结帐页面上显示 SKU?

【问题讨论】:

    标签: php wordpress woocommerce checkout cart


    【解决方案1】:

    2021 年更新

    您可以使用挂在 woocommerce_cart_item_name 动作钩子中的自定义函数来实现,这样:

    // Display the sku below cart item name
    add_filter( 'woocommerce_cart_item_name', 'display_sku_after_item_name', 5, 3 );
    function display_sku_after_item_name( $item_name, $cart_item, $cart_item_key ) {
        $product = $cart_item['data']; // The WC_Product Object
    
        if( is_cart() && $product->get_sku() ) {
            $item_name .= '<br><span class="item-sku">'. $product->get_sku() . '</span>';
        }
        return $item_name;
    }
    
    // Display the sku below under cart item name in checkout
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'display_sku_after_item_qty', 5, 3 );  
    function display_sku_after_item_qty( $item_quantity, $cart_item, $cart_item_key ) {
        $product = $cart_item['data']; // The WC_Product Object
    
        if( $product->get_sku() ) {
            $item_quantity .= '<br><span class="item-sku">'. $product->get_sku() . '</span>';
        }
        return $item_quantity;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码经过测试,可在 WooCommerce 3+ 上运行。你会得到:

    相关类似:How to Show SKU with product title in Order received page and Email order

    【讨论】:

    • 感谢您的回答。我将代码添加到我的活动主题的 functions.php 中,但 SKU 仍未显示。我的产品是变量产品,所有变量都有不同的 SKU,但我想这不重要。还有其他提示吗?
    • 如此简单。谢谢!
    【解决方案2】:

    你可以这样做:

    /**
     * @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>';
    }
    

    【讨论】:

      【解决方案3】:

      您需要做一些模板覆盖。

      购物车

      plugins/woocommerce/templates/cart/cart.php 复制到您的主题文件my_theme/woocommerce/cart/cart.php(如果它不存在)。然后在大约 #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 }
      

      结帐

      plugins/woocommerce/templates/checkout/review-order.php 复制到您的主题文件my_theme/woocommerce/checkout/review-order.php(如果它不存在)。然后在大约 #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 } ?>
      

      【讨论】:

      • 我已经尝试过了,但没有显示 SKU。谢谢或您的回答!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2013-12-13
      • 2021-09-23
      • 2020-11-03
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多