Jump to content

Editing a Code


MoeCloudy

Recommended Posts

Hello

The code shown below creates a new endpoint to the "my-account" section on my wordpress website. basically it displays the products a user purchased from our site. I just want to EXCLUDE a certain product category to be shown in this endpoint ! i hope you can help me achieve this !


 

function bbloomer_add_premium_support_endpoint() {
    add_rewrite_endpoint( 'premium-support', EP_ROOT | EP_PAGES );
}
 
add_action( 'init', 'bbloomer_add_premium_support_endpoint' );
 
 
// ------------------
// 2. Add new query var
 
function bbloomer_premium_support_query_vars( $vars ) {
    $vars[] = 'premium-support';
    return $vars;
}
 
add_filter( 'query_vars', 'bbloomer_premium_support_query_vars', 0 );
 
 
// ------------------
// 3. Insert the new endpoint into the My Account menu
 
function bbloomer_add_premium_support_link_my_account( $items ) {
    $items['premium-support'] = 'My Products';
    return $items;
}
 
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_premium_support_link_my_account' );
 
 
// ------------------
// 4. Add content to the new endpoint
 
function bbloomer_premium_support_content() {
echo '<h1>MY PRODUCTS</h1> ';
echo do_shortcode( ' [my_purchased_products] ' );
}
 
add_action( 'woocommerce_account_premium-support_endpoint', 'bbloomer_premium_support_content' );
// Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format

Link to comment
Share on other sites

SORRY !! i forgot the main code

 

  

add_shortcode( 'my_purchased_products', 'bbloomer_products_bought_by_curr_user' );

   

function bbloomer_products_bought_by_curr_user() {

   

    // GET CURR USER

    $current_user = wp_get_current_user();

    if ( 0 == $current_user->ID ) return;

   

    // GET USER ORDERS (COMPLETED + PROCESSING)

    $customer_orders = get_posts( array(

        'numberposts' => -1,

        'meta_key'    => '_customer_user',

        'meta_value'  => $current_user->ID,

        'post_type'   => wc_get_order_types(),

        'post_status' => array_keys( wc_get_is_paid_statuses() ),

    ) );

   

    // LOOP THROUGH ORDERS AND GET PRODUCT IDS

    if ( ! $customer_orders ) return;

    $product_ids = array();

    foreach ( $customer_orders as $customer_order ) {

        $order = wc_get_order( $customer_order->ID );

        $items = $order->get_items();

        foreach ( $items as $item ) {

            $product_id = $item->get_product_id();

            $product_ids[] = $product_id;

        }

    }

    $product_ids = array_unique( $product_ids );

    $product_ids_str = implode( ",", $product_ids );

   

    // PASS PRODUCT IDS TO PRODUCTS SHORTCODE

    return do_shortcode("[products ids='$product_ids_str']");

   

}

 

 

Link to comment
Share on other sites

If you need to post more code, please use the Code <> dialog to do it. Much easier to read that way.

 

Seems you would handle the product category in

foreach ( $items as $item ) {
	$product_id = $item->get_product_id();
	$product_ids[] = $product_id;
}

there. Like

foreach ( $items as $item ) {
	if (/* the product category is good to show */) {
		$product_id = $item->get_product_id();
		$product_ids[] = $product_id;
	}
}

How you go about deciding whether to show, I can't tell. Do you know what method(s) you need to call on $item?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.