Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在WooCommerce中列出最近售出的产品_Php_Wordpress_Woocommerce - Fatal编程技术网

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

Php 如何在WooCommerce中列出最近售出的产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在WooCommerce中,我想以4列的布局展示最近售出的产品。我想要的一个例子可以在这里找到:www.hem.com,最近购买的。 因此,清单应该具有 -产品名称 -产品价格 -产品缩略图 -买家名字 由于所有产品都是同类产品,因此清单应显示已售出的产品 我看过多个插件,但大多数都是通过显示弹出窗口或直接伪造最近购买的内容来操作的。 我想显示实际购买情况。请按照以下代码操作: function shortcode($atts) { $param = get_optio

在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;
        }
函数短码($atts){
$param=get_选项($this->pref);
$quantity=isset($param['quantity'])?$param['quantity']:'5';
$notification=isset($param['content'])?$param['content']:“{fname}{lname}在{price}{time}之前购买了{product}”;
$img=isset($param['shortcode\u img'])?$param['shortcode\u img']:'none';
$img_size=isset($param['shortcode_img_size'])?$param['shortcode_img_size']:'32';
$args=数组(
“numberposts”=>美元数量,
“开机自检状态”=>“wc已完成”,
“post_type”=>“shop_order”,
“抑制_过滤器”=>true,
);                      
$payments=get_posts($args);
$out=null;
如果(付款){
$out.='
    ; foreach($payments作为$payment){ 设置\u postdata($payment); $order=新WC\U订单($payment->ID); $fname=$order->get_billing_first_name(); $lname=$order->get_billing_last_name(); $date=$order->get_date_completed(); $time=human_time_diff(strotime($date),current_time('timestamp'); $user_id=$order->get_user_id(); $products=$order->get_items(); $product_ids=array(); foreach($products as$product){ $product_id[]=$product['product_id']; } $product\U ID=数组\U unique($product\U ID); $item_id=$product_id[0]; $product=wc\U get\U product($item\U id); $price=$product->get_price_html(); $url=get\u permalink($item\u id); $download=''; $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); 如果($img=='download'){ $image=get_the_post_缩略图($item_id,array($img_size,$img_size),array('class'=>'alignleft'); $image=''; } elseif($img=='avatar'){ $url=get_avatar_url($user_id,array('size'=>$img_size,'default'=>'monsterid',); $image=''; } 否则{ $image=null; } $out.='
  • '.$image.'.$message.'
  • '; } wp_reset_postdata(); $out.='
'; } 退回$out; }
你的问题太广泛了。我认为你需要的第一件事是能够阅读和理解代码。从您包含的代码部分,我看到
$message
包含一个html模板,模板中包含的参数为
{param}
。您需要修改模板并在其中包含参数,然后更改此代码以从数据中填充这些参数
$message=str_replace(“{param},$fname,$message)