簡體   English   中英

在Woocommerce中獲取作為“自定義我的帳戶”頁面內容分頁的未購買商品

[英]Get unpurchased items paginated as Custom My account page content in Woocommerce

首先,我想說這是我關於堆棧溢出的第一個問題,但是多年來,我發現了許多有用的解決方案,因此我要感謝所有參加此會議的人。

現在,我的第一個問題...

在我的woocommerce設置中,每個產品都是數字下載,客戶只能購買一次。 出於安全原因,所有下載均會在14天后過期。

我們有一些客戶誰已經伴隨我們長達5年,所以,通過產品去當我們決定提供在其賬戶頁面,顯示了他們還沒有尚未購買。 只是讓他們更容易。

現在,我的問題有兩個。 首先,下面的代碼在其帳戶儀表板中顯示了該頁面,我無法讓paginav工作,因此,如果您看到那里可能有什么問題,請告訴我。

但更大的問題是,如何將查詢轉換為排序函數? 意思是,如果客戶選擇,則可以按“尚未購買”進行排序

或者,如果有人知道更簡單的方法來完成此操作,請告訴我。

這是我當前用於顯示一個頁面的代碼,該頁面向他們顯示其“尚未購買”的產品

// *NOT PURCHASED
add_filter ( 'woocommerce_account_menu_items',     'custom_purchased_products_link', 40 );
add_action( 'init', 'custom_add_products_endpoint' );
add_action( 'woocommerce_account_purchased-products_endpoint',     'custom_populate_products_page' );

// here we hook the My Account menu links and add our custom one
function custom_purchased_products_link( $menu_links ){
    // we use array_slice() because we want our link to be on the 3rd position
    return array_slice( $menu_links, 0, 2, true )
    + array( 'purchased-products' => 'Not Purchased Yet' )
    + array_slice( $menu_links, 2, NULL, true );
}

// here we register our rewrite rule
function custom_add_products_endpoint() {
    add_rewrite_endpoint( 'purchased-products', EP_PAGES );
}

// here we populate the new page with the content
function custom_populate_products_page() {
    global $wpdb;

    // this SQL query allows to get all the products purchased by the current     user
    // in this example we sort products by date but you can reorder them another     way
    $purchased_products_ids = $wpdb->get_col( $wpdb->prepare(
        "
        SELECT      itemmeta.meta_value
        FROM        " . $wpdb->prefix . "woocommerce_order_itemmeta itemmeta
        INNER JOIN  " . $wpdb->prefix . "woocommerce_order_items items
                    ON itemmeta.order_item_id = items.order_item_id
        INNER JOIN  $wpdb->posts orders
                    ON orders.ID = items.order_id
        INNER JOIN  $wpdb->postmeta ordermeta
                    ON orders.ID = ordermeta.post_id
        WHERE       itemmeta.meta_key = '_product_id'
                    AND ordermeta.meta_key = '_customer_user'
                    AND ordermeta.meta_value = %s
        ORDER BY    orders.post_date DESC
        ",
        get_current_user_id()
    ) );

    // some orders may contain the same product, but we do not need it twice
    $purchased_products_ids = array_unique( $purchased_products_ids );

    // if the customer purchased something
    if( !empty( $purchased_products_ids ) ) :

        // it is time for a regular WP_Query
        $purchased_products = new WP_Query( array(
            'post_type' => 'product',
            'paginate' => true,
            'posts_per_page' => 50,
            'paginate' => true,
            'post_status' => 'publish',
            'post__not_in' => $purchased_products_ids,
        ) );

        echo '<div class="woocommerce columns-3">';
        echo '<h3>Some Products you have not yet purchased (Max display 39)    
        </h3>';

        woocommerce_product_loop_start();

        while ( $purchased_products->have_posts() ) : $purchased_products- 
        >the_post();

            wc_get_template_part( 'content', 'product' );

        endwhile;
        woocommerce_product_loop_end();

        woocommerce_reset_loop();
        wp_reset_postdata();

    else:
        echo 'Nothing purchased yet.';
    endif;
}

add_action( 'woocommerce_after_shop_loop_item', 
'user_logged_in_product_already_bought', 30 );

您可以簡化和優化代碼以進行唯一的SQL查詢,以獲取當前客戶未購買的產品ID。 然后,您可以使用Woocommerce [products]簡碼來獲得此未購買產品的漂亮分頁頁面。

因此,通過分頁(在Woocommerce 3.2+上) ,代碼將更加輕巧和高效:

// Add a new custom menu item to tabbed "My account" menu
add_filter ( 'woocommerce_account_menu_items', 'add_account_new_menu_item', 30, 1 );
function add_account_new_menu_item( $items ){
    // Define the new item position in the tabbed menu
    $position = 3;

    // Define the new item to include in the tabbed menu
    $item = array( 'unpurchased-products' => 'Not purchased' );

    return array_slice( $items, 0, ($position - 1), true ) + $item + array_slice( $items, ($position - 1), NULL, true );
}

// Enable the new custom menu item tabbed "My account" menu
add_action( 'init', 'add_account_new_menu_item_endpoint' );
function add_account_new_menu_item_endpoint() {
    add_rewrite_endpoint( 'unpurchased-products', EP_PAGES );

    // ==> Always reflush rewrite rules the first time:
    // ==> Go to the Permalinks options page and re-save the permalinks
}

// Add the corresponding content for this new custom menu item
add_action( 'woocommerce_account_unpurchased-products_endpoint', 'custom_populate_products_page' );
function custom_populate_products_page() {
    global $wpdb;

    //Unique SQL query to get tne non purchased product Ids
    $not_purchased_ids = $wpdb->get_col( "SELECT DISTINCT ID
        FROM {$wpdb->prefix}posts
        WHERE post_type = 'product'
        AND post_status = 'publish'
        AND ID NOT IN (SELECT DISTINCT woim.meta_value
        FROM {$wpdb->prefix}woocommerce_order_itemmeta AS woim
        JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON woim.order_item_id = woi.order_item_id
        JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.ID
        JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        WHERE woim.meta_key = '_product_id' AND pm.meta_key = '_customer_user'
        AND pm.meta_value != '{get_current_user_id()}')
    " );

    $included_ids = implode(',', $not_purchased_ids);

    echo do_shortcode("[products limit='9' columns='3' paginate='true' orderby='' ids='$included_ids']");
}

// Rename the default option from sorting select field in My account only
add_filter ( 'woocommerce_catalog_orderby', 'custom_catalog_orderby', 30, 1 );
function custom_catalog_orderby( $options ){
    if( is_account_page() )
        $options['menu_order'] = __( 'Not purchased yet', 'woocommerce' );

    return $options;
}

代碼進入您的活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

啟用新的“我的帳戶”選項卡式菜單項

保存此代碼后,您將需要刷新Wordpress重寫規則:
在“ 永久鏈接”選項頁面中進入WordPress設置,然后重新保存永久鏈接。


按頁面和列數更改產品數:

在簡碼limit中,按頁數limit產品數量,按columnslimit產品數量。

  [products limit='9' columns='3' paginate='true' orderby='' ids='$included_ids'] 

在此處輸入圖片說明


有用的鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM