woocommerce显示上次购买并交叉销售当前用户

时间:2016-06-17 13:47:11

标签: woocommerce fetch

我根据产品使用特定的感谢页面 我想在此页面上显示当前用户的上次购买(产品名称),并获取特定产品的交叉销售。

任何帮助都是apreciate

当然,我做了一些研究,发现了这样的教程 https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/#comment-703884

但我并不擅长php根据我的需要修改它

谢谢

1 个答案:

答案 0 :(得分:0)

如果您想显示最近购买的50个产品,请转到外观>编辑> function.php中的 function.php ,然后点击以下代码:

// Recently Sold Products shortcode for WooCommerce

  add_shortcode('sold_products', 'sold_products');
  function sold_products($attributes) {
    global $wpdb, $woocommerce;
    $defaults = array('max_products' => 50, 'title' => "Last purchased");
    $parameters = shortcode_atts( $defaults, $attributes);
    $max = absint($parameters['max_products']);  // number of products to show
    $title = sanitize_text_field($parameters['title']);
    $html = '<div class="sold_products">'.PHP_EOL;  
    if ($title) {
      $html .= '<h3>Last purchased:</h3>'.PHP_EOL;
    }
    $table = $wpdb->prefix.'woocommerce_order_items';
    $my_query = $wpdb->prepare("SELECT * FROM $table WHERE `order_item_type`='line_item' ORDER BY `order_id` DESC LIMIT %d", $max);  
    $nr_rows = $wpdb->query($my_query);
    if (!$nr_rows) {
      $html .= '<p>Aucun produit n\'est acheté encore!</p>';
    } else {
      $html .= '<ul>'.PHP_EOL;
      for ($offset = 0; $offset < $nr_rows; $offset++) {
    if ( is_user_logged_in() ) {
            $user_info = wp_get_current_user();
    }
        $row = $wpdb->get_row($my_query, OBJECT, $offset);
        $product_name = $row->order_item_name;
        $product = get_page_by_title($product_name, OBJECT, 'product');
        $url = get_permalink($product->ID);
        $order_id = $row->order_id;
        $order = new WC_Order($order_id);
        $user = $order->get_user();
        if ($user) {
          $user_id = $user->ID;
        } else {
          $user_id = 'Guest';
        }
    if($user_info->ID == $user_id){
            $unix_date = strtotime($order->order_date);
            $date = date('d/m/y', $unix_date);
            $html .= '<li><a href="'.$url.'">'.$product_name.'</a> acheté le '.$date.'</li>'.PHP_EOL;
    }
      }
      $html .= '</ul>'.PHP_EOL;
    }
    $html .= '</div>'.PHP_EOL;
    return $html;
  } // end function

您可以将此结果显示在您希望显示的镜头代码中的页面中的任何位置:

[sold_products max_products="50" title="Last purchased"]