【问题标题】:WooCommerce Order overview add column with ordered productsWooCommerce 订单概览添加包含已订购产品的列
【发布时间】:2014-09-07 21:49:50
【问题描述】:

我正在尝试改进 WooCommerce 的订单概览屏幕,我想添加一个包含已订购产品的列。

所以我可以看到例如这些列: 订单号、订单总数、订购的产品、地址、备注、操作

我们已经在互联网上找到了一些添加产品列的代码,但它缺少产品 SKU。我目前能看到的:

  • 1x 产品名称A
  • 3x 产品名称C

我想看什么:

  • 1x ARM-002(产品名称A)
  • 3x ARM-008(产品名称C)

我已使用此代码添加列:

add_filter('manage_edit-shop_order_columns', 'add_ordered_products_column', 11);    
function add_ordered_products_column($columns) {
    $columns['order_products'] = "Ordered products";
    return $columns;
}

还有这个用于添加栏目内容:

add_action( 'manage_shop_order_posts_custom_column' , 'add_ordered_products_column_content', 11, 2 );
function add_ordered_products_column_content( $column ) {
 global $post, $woocommerce, $the_order;

    switch ( $column ) {

        case 'order_products' :

        $terms = $the_order->get_items();

        if ( is_array( $terms ) ) {
             foreach($terms as $term) {
                echo $term['item_meta']['_qty'][0] .' x '. $term['name'] .'<br />';
            }
        }

        else {
                _e( 'Unable to get products', 'woocommerce' );
        }

        break;
    }
}

我想使用类似 $term['sku'] 的东西,但这不起作用,get_sku(); 也不行。 谁知道这个问题的解决方案?

【问题讨论】:

    标签: php wordpress woocommerce orders


    【解决方案1】:

    首先如果你要查找产品SKU,替换这行代码:

    echo $term['item_meta']['_qty'][0] .' x '. $term['name'] .'<br />';
    

    用这个:

    $sku = ( $sku = get_post_meta( $term['product_id'], '_sku', true ) ) ? $sku : '';
    echo $term['quantity'] . ' ' . $sku . ' x ' . $term['name'] .'<br />';
    

    如果在那之后仍然有问题,我建议您检查适合我的this code

    【讨论】:

      【解决方案2】:

      你有没有尝试过类似的东西

      <?php echo ( $sku = $_product->get_sku() ) ? $sku : __( 'n/a', 'woocommerce' ); ?>
      

      在哪里

      <?php 
           foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
               $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
               if( necessary conditionals goes here){ ... }        
           }
      ?>
      

      【讨论】:

        【解决方案3】:

        请注意,从 Woocommerce 3.0 开始,这行代码:

        echo $term['item_meta']['_qty'][0] .' x '. $term['name'] .'<br />';
        

        不再显示订购商品的数量。

        如果您使用的是 Woocommerce 3.0 及更高版本,则必须将其换成这行代码:

        echo $term['quantity'] .' x '. $term['name'] .'<br />';
        

        【讨论】:

          猜你喜欢
          • 2015-08-14
          • 1970-01-01
          • 2011-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多