简体   繁体   English

在Woocommerce中获取作为“自定义我的帐户”页面内容分页的未购买商品

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

first I want to say this is my very first question on Stack overflow but, I have found many helpful solutions over the years so I want to thank everyone who participates here. 首先,我想说这是我关于堆栈溢出的第一个问题,但是多年来,我发现了许多有用的解决方案,因此我要感谢所有参加此会议的人。

Now, onto my first question... 现在,我的第一个问题...

In my woocommerce setup, every product is a digital download and customers are limited to buying each only once. 在我的woocommerce设置中,每个产品都是数字下载,客户只能购买一次。 All downloads expire after 14 days for security reasons. 出于安全原因,所有下载均会在14天后过期。

We have some customers who have been with us for up to 5 years so, when going through products we decided to offer a page in their account that shows what they havent purchased yet. 我们有一些客户谁已经伴随我们长达5年,所以,通过产品去当我们决定提供在其账户页面,显示了他们还没有尚未购买。 Just makes it easier for them. 只是让他们更容易。

Now, my question is two fold. 现在,我的问题有两个。 First for the code below, which shows the page in their account dashboard, I can not get paginav to work so if you can see what could be wrong there, please let me know. 首先,下面的代码在其帐户仪表板中显示了该页面,我无法让paginav工作,因此,如果您看到那里可能有什么问题,请告诉我。

But the bigger question is, how can I turn the query into a sort function? 但更大的问题是,如何将查询转换为排序函数? Meaning, if the customer chooses they can sort by "Not yet purchased" 意思是,如果客户选择,则可以按“尚未购买”进行排序

Or, if anyone knows an easier way to accomplish this, please let me know. 或者,如果有人知道更简单的方法来完成此操作,请告诉我。

Here is the code I am currently using to display a page that shows them products they have "Not yet purchased" 这是我当前用于显示一个页面的代码,该页面向他们显示其“尚未购买”的产品

// *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 );

You can simplify and optimize the code making a unique SQL query to get the product IDs not purchased by the current customer. 您可以简化和优化代码以进行唯一的SQL查询,以获取当前客户未购买的产品ID。 Then you can use Woocommerce [products] shortcode to get a nice paginated page of this not purchased product. 然后,您可以使用Woocommerce [products]简码来获得此未购买产品的漂亮分页页面。

So the code will be much more lighter and efficient, with pagination (on Woocommerce 3.2+) : 因此,通过分页(在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;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

Enable the new My account tabbed menu item 启用新的“我的帐户”选项卡式菜单项

Once this code is saved, you will need to flush Wordpress rewrite rules: 保存此代码后,您将需要刷新Wordpress重写规则:
Go in WordPress settings in Permalinks options page and re-save the permalinks. 在“ 永久链接”选项页面中进入WordPress设置,然后重新保存永久链接。


Changing the number of products by page and the number of columns: 按页面和列数更改产品数:

In the shortcode limit is number of products by page and columns the number of columns. 在简码limit中,按页数limit产品数量,按columnslimit产品数量。

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

在此处输入图片说明


Useful links: 有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 如何获取我的账户物品清单? - WooCommerce how to get the list of my account items? Woocommerce 我的帐户自定义页面 - 找不到页面 404(永久链接问题) - Woocommerce my account custom page - page not found 404 (permalinks problem) 在WooCommerce中定义用户变量我的账户自定义菜单项内容 - Define user variable in WooCommerce My account custom menu item content 仅在 WooCommerce 我的帐户仪表板中添加自定义 html 内容 - Add custom html content in WooCommerce My Account Dashboard only 在 WooCommerce 中设置我的帐户自定义项目端点标题 - Set My Account custom items endpoints titles in WooCommerce WooCommerce 我的帐户页面自定义菜单项与自定义链接 - WooCommerce My Account page custom menu item with custom link WooCommerce:在“我的帐户”的编辑页面上验证自定义字段 - WooCommerce: Validate custom fields on My Account's edit page 如何在woocommerce我的帐户页面中对自定义选项卡重新排序? - How to reorder custom tab in woocommerce my account page? Woocommerce 如何在我的帐户页面上重定向自定义端点 - Woocommerce how to redirect a custom end point on my-account page 将内容添加到 WooCommerce 我的帐户仪表板主页部分 - Add content to WooCommerce My account dashboard main page section
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM