如何在WooCommerce中列出最近销售的产品



在WooCommerce中,我想以4列布局显示最近销售的产品。我可以在这里找到我要做的一个例子:最近购买的 www.hem.com。所以列表应该有- 产品名称- 产品价格- 产品缩略图- 买家名字

列表应显示售罄的产品,因为所有产品都是独一无二的。

我看过多个插件,但大多数插件都是通过显示弹出窗口或直接伪造最近的购买来操作的。我想显示实际购买情况。

请按照以下代码操作:

function shortcode($atts) { 
            $param = get_option($this->pref);           
            $quantity = isset( $param['quntity'] ) ? $param['quntity'] : '5';
            $notification = isset( $param['content'] ) ? $param['content'] : "{fname} {lname} purchased {product} for {price} {time} ago";
            $img = isset( $param['shortcode_img'] ) ? $param['shortcode_img'] : 'none'; 
            $img_size = isset( $param['shortcode_img_size'] ) ? $param['shortcode_img_size'] : '32';
            $args = array(
            'numberposts'      => $quantity,
            'post_status'      => 'wc-completed',           
            'post_type'        => 'shop_order',
            'suppress_filters' => true, 
            );                      
            $payments = get_posts( $args );

            $out = null;
            if($payments){
                $out .= '<ul class="wow_woo_recent_purchases">';            
                foreach ( $payments as $payment ) { 
                    setup_postdata($payment);
                    $order = new WC_Order($payment->ID);
                    $fname = $order->get_billing_first_name();
                    $lname = $order->get_billing_last_name();                           
                    $date = $order->get_date_completed();
                    $time = human_time_diff( strtotime($date), current_time('timestamp') );
                    $user_id = $order->get_user_id();
                    $products = $order->get_items();
                    $product_ids = array();                 
                    foreach($products as $product){
                        $product_ids[] = $product['product_id'];
                    }                   
                    $product_ids = array_unique($product_ids);
                    $item_id = $product_ids[0];
                    $product = wc_get_product( $item_id );                   
                    $price = $product->get_price_html();
                    $url = get_permalink($item_id);             
                    $download = '<a href="'.$url.'">'.$product->get_title().'</a>';                 
                    $message = $notification;
                    $message = str_replace( '{fname}', $fname, $message );
                    $message = str_replace( '{lname}', $lname, $message );
                    $message = str_replace( '{product}', $download, $message );
                    $message = str_replace( '{price}', $price, $message );
                    $message = str_replace( '{time}', $time, $message );                                        
                    if($img == 'download'){
                        $image = get_the_post_thumbnail( $item_id, array($img_size,$img_size), array('class' => 'alignleft') );
                        $image = '<a href="'.$url.'">'.$image.'</a>';
                    }
                    elseif($img == 'avatar'){
                        $url = get_avatar_url( $user_id, array('size' => $img_size,'default'=>'monsterid',) );
                        $image = '<img src="'. $url .'" class="alignleft" width="'.$img_size.'">';                                                      
                    }
                    else {
                        $image = null;
                    }
                    $out .=  '<li>'.$image.' '.$message.'</li>';
                }
                wp_reset_postdata();
                $out .= '</ul>';

            }   
            return $out;
        }

最新更新