Woocommerce Shortcode - 返回前端单个$ key的多个$值

时间:2017-04-24 18:28:59

标签: php sql woocommerce metadata

好的,这就是我的问题:我有一个自定义字段附加到每个产品,其中有许多复选框...客户最多检查4个框。结果保存在$ key' Selection'

下的元数据中

我只能返回这四个选项中的最后一个......如何列出客户名旁边的表格中的所有选项...请参阅下文。

这是一个接受产品ID的短代码,因此可以在任何地方显示!

非常感谢!

<?php
// Add Shortcode
function pcl_shortcode( $atts ) {
$output = '';

// Attributes
$customer_atts = shortcode_atts( array(
    'product' => get_the_id(),
), $atts );

// Code
global $post, $wpdb;
$post_id = $customer_atts['product'];
$pcl_orders = '';
$columns = array();
$customerquery = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta woim 
    LEFT JOIN {$wpdb->prefix}woocommerce_order_items oi 
    ON woim.order_item_id = oi.order_item_id 
    WHERE meta_key = '_product_id' AND meta_value = %d
    GROUP BY order_id;";
$order_ids = $wpdb->get_col( $wpdb->prepare( $customerquery, $post_id ) );
$order_status = get_option( 'pcl_order_status_select', array('wc-completed') );
if( $order_ids ) {
    $args = array(
        'post_type'       =>'shop_order',
        'post__in'   => $order_ids,
        'posts_per_page' =>  999,
        'order'          => 'ASC',             
        'post_status' => $order_status,
    );
    $pcl_orders = new WP_Query( $args );
}
if($pcl_orders) {
    $output .= '<table>';
    foreach($pcl_orders->posts as $pcl_order) {
        $order = new WC_Order($pcl_order->ID);
        $output .= '<tr>';
        $output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . '</td>';
        foreach($order->get_items() as $item) 

        // get an array of multiple values
        $output .= '<td>' . $item['Selection'] . '</td>';
        $output .= '</tr>';
    }
    $output .= '</table>';
}
return $output;
}

add_shortcode( 'woo_customer_list', 'pcl_shortcode' );

0 个答案:

没有答案