在WooCommerce中,我想以4列的布局显示最近出售的产品。我要寻找的商品的示例可以在以下位置找到:www.hem.com,最近购买的商品。 因此,清单应具有 - 产品名称 -产品价格 -产品缩略图 -买家名字
由于所有产品都是同类产品之一,因此清单应显示售罄的产品。
我看过多个插件,但是大多数插件都是通过显示弹出窗口或伪造最近购买的内容来操作的。 我想显示实际购买。
答案 0 :(得分:0)
请遵循以下代码:
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;
}