• Good day,

    Nice plugin!

    I just want to ask if possible i can export the ID/SLUG of my PARENT PRODUCT CATEGORY.

    It’s look like this:

    PRODUCT(My Parent Product Category)
    MARKETING COLLATERAL( MY Proudct Category)
    FLYERS(My Product)

    Thank you for quick response.
    MJ

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author algol.plus

    (@algolplus)

    hi MJ

    please, follow to https://algolplus.freshdesk.com/support/solutions/articles/25000018287-add-calculated-field-for-product-

    use metakey “main_cat_id” and this code.
    thanks, Alex

    add_filter('woe_get_order_product_value_main_cat_id', function ($value, $order, $item, $product,$item_meta) {
    	// get all product cats 
    	$categories = get_the_terms( $product->get_id(), 'product_cat' ); 
    	// wrapper to hide any errors from top level categories or products without category
    	if ( $categories && ! is_wp_error( $category ) ) : 
    		// loop through each cat
    		foreach($categories as $category) :
    		// get the children (if any) of the current cat
    		$children = get_categories( array ('taxonomy' => 'product_cat', 'parent' => $category->term_id ));
    		if ( count($children) == 0 ) {
    			// if no children, then return the category ID
    			return $category->term_id;
    		}
    		endforeach;
    	endif;
    	return $value;
    }, 10, 5);
    • This reply was modified 5 years, 7 months ago by algol.plus.
    Thread Starter hpiiph102

    (@hpiiph102)

    Hi Alex,

    Thank you for your quick response. I’ve tried the process above, but nothing is showing.
    I checked Enable debug output and i encounter this message SELECT ID AS order_id FROM wpir_posts AS orders WHERE orders.post_type in ( ‘shop_order’) AND 1 AND orders.post_status NOT in (‘auto-draft’,’trash’)

    Thanks,
    Mj

    Plugin Author algol.plus

    (@algolplus)

    hi MJ

    Did you add and see new empty column ?
    Have you put this code to “Misc Settings”?
    thanks, Alex

    Thread Starter hpiiph102

    (@hpiiph102)

    Hi Alex,

    Yes, i see an empty column, and also in add the code in MISC Settings. And encounter this error message “SELECT ID AS order_id FROM wpir_posts AS orders WHERE orders.post_type in ( ‘shop_order’) AND 1 AND orders.post_status NOT in (‘auto-draft’,’trash’) “

    Thread Starter hpiiph102

    (@hpiiph102)

    Hi Alex,

    Also, I follow the instruction above and still can’t see my parent category ID.

    Hope you help me on this.

    Thanks,
    MJ

    Plugin Author algol.plus

    (@algolplus)

    hi MJ

    I just create new profile and repeat steps

    1. added field and drag it to export https://imgur.com/BwfYU2H
    2. pasted code and press Preview https://imgur.com/XtC4O0j

    I don’t see a reason why you see no results.

    Could you edit Product and share the screenshot of section “Product categories”?
    Do you see same picture as https://imgur.com/51EtpTu ?
    thanks, Alex

    Thread Starter hpiiph102

    (@hpiiph102)

    Hi Alex,

    Thanks, now i see number on main_cat_id column, however it’s the ID of my sub product category.

    For instance:

    Parent Product Category ID = 1
    Sub Product Category = 2

    On the report i see the ID = 2, it should be 1 since that is the ID of my parent.

    Also, i’ve encounter this error “wp-content/plugins/woo-order-export-lite/classes/core/class-wc-order-export-engine.php(401) : eval()’d code on line 5”.

    Thanks,
    MJ

    Plugin Author algol.plus

    (@algolplus)

    please, replace code with this version

    add_filter('woe_get_order_product_value_main_cat_id', function ($value, $order, $item, $product,$item_meta) {
    	// get all product cats 
    	$categories = get_the_terms( $product->get_id(), 'product_cat' ); 
    	// wrapper to hide any errors from top level categories or products without category
    	if ( $categories && ! is_wp_error( $categories ) ) : 
    		// loop through each cat
    		foreach($categories as $category) :
    			// get the children (if any) of the current cat
    			$parents =  get_ancestors( $category->term_id , 'product_cat', 'taxonomy' );
    			if ( count($parents) == 0 ) {
    				// if no parents, then return the category ID
    				return $category->term_id;
    			}
    		endforeach;
    	endif;
    	return $value;
    }, 10, 5);
    Thread Starter hpiiph102

    (@hpiiph102)

    Hi Alex,

    Thanks, now its works on simple product only.However, I’m using a VARIABLE PRODUCTS and nothing happen.

    Please help me here,

    MJ

    Plugin Author algol.plus

    (@algolplus)

    Here is updated version

    add_filter('woe_get_order_product_value_main_cat_id', function ($value, $order, $item, $product,$item_meta) {
    	// get all product cats 
    	$categories = get_the_terms( $item['product_id'], 'product_cat' ); 
    	// wrapper to hide any errors from top level categories or products without category
    	if ( $categories && ! is_wp_error( $categories ) ) : 
    		// loop through each cat
    		foreach($categories as $category) :
    			// get the children (if any) of the current cat
    			$parents =  get_ancestors( $category->term_id , 'product_cat', 'taxonomy' );
    			if ( count($parents) == 0 ) {
    				// if no parents, then return the category ID
    				return $category->term_id;
    			}
    		endforeach;
    	endif;
    	return $value;
    }, 10, 5);
    Thread Starter hpiiph102

    (@hpiiph102)

    Hi Alex,

    Thanks it’s work on variable product but not on simple product.

    It is possible to work, regardless the product type(simple, grouped, variable, and external/affiliate product)?

    Thanks,
    MJ

    Plugin Author algol.plus

    (@algolplus)

    hi MJ

    Last code should work regardless to product type.
    Please, check if you selected top categories for simple product in section “Product categories”. For example, “Music” at this screenshot https://imgur.com/51EtpTu

    thanks, Alex

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Get Parent Product Category ID’ is closed to new replies.