WooCommerce - add column to Product Cat admin table - php

I would like to add a new column in the back-end table for Product Categories. This column will contain a link "view category" and will link all to the www.domain.com/category/category-name page.
I looked into the Wordpress docs and this is the code I came up with... but it doesn't work!
function product_cat_cpt_columns($columns) {
$new_columns = array(
'Link' => "Link to page"
);
return array_merge($columns, $new_columns);
}
add_filter('manage_product_cat_posts_custom_columns' , 'product_cat_cpt_columns');
Any idea how I would do this? I really appreciate your help!

I found it suprisingly hard to find a solution for such a trivial task and I'm very thankful to Helgatheviking for her answer that pointed me to the right direction. Her answer didn't quite work for me, because it would allow only the same value for all the column values, so I decided to post an improved version here.
The problem was with the second function, because it didn't provide a way to add field's value that is corresponding to the current category. I've dug through Woocommerce's source (there you could search for "product_cat_column" to go through relevant parts and see how it's made) and found out that this filter accepts 3 paramaters, not 1. That allows for a specific value per row, not the same value for all rows, as it is in Helgatheviking's answer.
Another drawback was that it would put the value to the thumbnail's column, because that is what Woocommerce use this filter for, actually.
So here's my code:
function add_custom_column($columns) {
$columns['foo'] = 'FOO';
$columns['link'] = 'Link to page';
return $columns;
}
add_filter('manage_edit-product_cat_columns', 'add_custom_column');
function category_custom_column_value( $columns, $column, $term_id ) {
if ($column == 'FOO') {
$foo = get_term_meta( $term_id, 'foo', true );
return $foo;
}elseif ($column == 'link') {
$category = get_term_by( 'id', $term_id, 'product_cat' );
$category_link = get_term_link( $category->slug, 'product_cat' );
return '' . $category_link . '';
}
}
add_filter('manage_product_cat_custom_column', 'category_custom_column_value', 10, 3);
As you can see, the first function stays the same, but the second now checks for the column name and returns the content depending on this name. You can get any category meta this way and do it for as many columns as you'd like.

Pulling from this answer you can add columns to the Edit Tags screen with the following code:
function add_post_tag_columns($columns){
$columns['foo'] = 'Foo';
return $columns;
}
add_filter('manage_edit-product_cat_columns', 'add_post_tag_columns');
function add_post_tag_column_content($content){
$content .= 'Bar';
return $content;
}
add_filter('manage_product_cat_custom_column', 'add_post_tag_column_content');

Related

ACF filter to exclude all categories from select

I am using "Frontend Submit Pro" and "ACF(not pro) plugins for WordPress.
I am using these plugins to make frontend post creator for my users.
I have more than 200+ categories so I want to make it more easy for my users to select categories. I will create multiple forms and each form will have a few categories to be selected from users.
For now i use the below filter to exclude some categories from forms.
add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2);
function exclude_categories( $args, $field ) {
global $uncategorized_id;
$args['exclude'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
return $args;
}
Because I have a lot of categories I can't exclude 200 categories in the above code is too hard.
So i want a filter that will exclude all categories and include only the 5-10 categories that i want to be show in each form.
I have not the knowledge to do that so i am asking if anyone can help.
I also want each filter apply to only one form. I need some way to link filters to the correct form.(maybe by link or form name)
Solved it with a filter.
add_action( 'init', 'get_term_ids' );
function get_term_ids() {
global $uncategorized_id;
$u = get_term_by( 'slug', 'uncategorized', 'product_cat' );
$uncategorized_id = $u->term_id;
}
add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2);
function exclude_categories( $args, $field ) {
global $uncategorized_id;
$args['exclude'] = array($uncategorized_id); //the IDs of the excluded terms
return $args;
}
Answering my question.
I changed the "exclude" to "include" in the filter above and it seems to do exactly what I wanted.
It only shows the categories that I give it in the form.
For those who display categories within "select" use the following code.
add_filter('acf/fields/taxonomy/query/name=kathgories', 'include_categories', 10, 2);
function include_categories( $args, $field ) {
global $uncategorized_id;
$args['include'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
return $args;
}
For those who display categories in a "checkbox" use the following code.
add_filter('acf/fields/taxonomy/wp_list_categories/name=kathgories', 'my_taxonomy_args', 10, 2);
function my_taxonomy_args( $args, $field ){
$args['include'] = array(197,247,245,250,246,248,249,251);//the IDs of the excluded terms
return $args;
}
Also change /name=kathgories'
with your own ACF Taxonomy field name.
So with that change , I am answering my second question.

Turn Woocommerce category ID into slug function

I am looking for a function to convert the woocommerce category id into a category slug for a breadcrumb link.
The following loop pulls the upper most product category ID for product pages. This is necessary because some of my products have 4 or 5 levels of hierarchy.
This is working fine.
$prod_terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($prod_terms as $prod_term) {
// gets product cat id
$product_cat_id = $prod_term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
foreach($last_parent_cat as $last_parent_cat_value){
}
}
At this point echo $last_parent_cat_value; would give you the ID of the highest category.
In order to turn this into a breadcrumb link I need to pull the category name. so...
// This function turns Category Ids produced by the foreach loop above into Names
function woocommerceCategoryName($id){
$term = get_term( $id, 'product_cat' );
return $term->name;
}
Now all I need is the slug to complete the link. so...
//this function gets the category slug from the category id
function get_cat_slug($cat_id) {
$cat_id = (int) $cat_id;
$category = &get_category($cat_id);
return $category->slug;
}
but when I echo
echo '' . woocommerceCategoryName($last_parent_cat_value) . '';
My Name function is working but my slug function is not.
Can anyone shine any light into my error?
when I inspect element on the front end here is my generated html...
Sports
Could I try and use the Name to generate the slug?
Thanks for reading.
okay, I am not sure why the above wasn't working, or why the following worked.
//I copied the function to generate the name, and generated a slug instead...
function woocommerceCategorySlug($id){
$term = get_term( $id, 'product_cat' );
return $term->slug;
}
echo '' . woocommerceCategoryName($last_parent_cat_value) . '';
Thanks again for reading, I hope this can help someone else.

Modify WooCommerce product columns

I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!
I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}

PHP: Add translated tags to category list

I have a website made from a template in Wordpress, I've installed a translation plugin (qtranslate).
http://madebysylvie.be/collection
In my "photo album" page when I sort the items in the default language (english) everything works fine, however if I change to another translation (ex.french), the name of the category changes and the tagged items don't appear anymore.
Here is the PHP code that executes that function,
<ul class="filter_portfolio">
<?php
// Get the taxonomy
$terms = get_terms('filter', $args);
// set a count to the amount of categories in our taxonomy
$count = count($terms);
// set a count value to 0
$i=0;
// test if the count has any categories
if ($count > 0) {
// break each of the categories into individual elements
foreach ($terms as $term) {
// increase the count by 1
$i++;
// rewrite the output for each category
$term_list .= '<li class="segment-'.$i.'">' . $term->name . '</li>';
// if count is equal to i then output blank
if ($count != $i)
{
$term_list .= '';
}
else
{
$term_list .= '';
}
}
// print out each of the categories in our new format
echo $term_list;
}
?>
</ul>
I would like to change this block of code in order to identify the tags in the translated version. I know that the trigger is the ("data-value") parameter.
The following code let's me translate the taxonomies from the default language,
function qtranslate_edit_taxonomies(){
$args=array(
'public' => true ,
'_builtin' => false
);
$output = 'object'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies($args,$output,$operator);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
add_action( $taxonomy->name.'_add_form', 'qtrans_modifyTermFormFor');
add_action( $taxonomy->name.'_edit_form', 'qtrans_modifyTermFormFor');
}
}
}
add_action('admin_init', 'qtranslate_edit_taxonomies');
?>
Thank you very much for helping!
i suspect you use the category name to filter your database records. So in your db there is only the "englsh" name and when you "translate" it your script is unable to work properly since it uses the new "french" name.
Can you try to convert the category name to engish before executing your query?
EDIT
What i actually think is for you to find a way to pass two variables to your php script. Whatever suits your needs.
For example, some possible scenarios are:
- category in english + language (fr) = based on your selected language you query your db and return proper results(based on english work thought)
- category id + language (fr) = same as above but more generic since you use an id to get data from table.
I am guessing you must change somewhat your html and you php script (one to pass correct data and the other to return correct format).

wp_list_pages doesn't list included page

Ok, this is really bugging me! I wrote a function (well, modified a very similar one from the wordpress codex) which lists all child pages and the parent page whenever the page has children or is a child. That bit works fine, but what I want to do now is have it look in a custom field (add-to-nav) which has the path to another page to add to the end of the list. It checks the page and parent for that custom field.
It seems to be echoing all the right details but for some reason won't add the page to the list. Would really appreciate any help!
function add_sec_nav_w_extras(){
global $post;
global $wpdb;
$key = 'add-to-nav';
$addition = get_post_meta($post->ID, $key, TRUE);
if($addition == '') {$addition = get_post_meta($post->post_parent, $key, TRUE);}
if($addition != '') {
$addition_page = get_page_by_path($addition);
}
echo("Addition page: ".$addition_page->post_title."<br/>Addition ID: ".$addition_page->ID."<br/>Current: ".$post->ID."<br/>Parent: ".$post->post_parent);
?><section id="secondnav_w_parent" class="fix"><ul><?php
if($post->post_parent != "0") { // If post has parent, list that parent and all its children
wp_list_pages( array('title_li'=>'','include'=>$post->post_parent) );
wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>$post->post_parent) );
if($addition_page != ''){
wp_list_pages( array('title_li'=>'','include'=>$addition_page->ID) );
}
}
else if(get_pages('child_of='.$post->ID)) { // If post is a parent, list itself and all children
wp_list_pages( array('title_li'=>'','include'=>$post->ID) );
wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>$post->ID) );
if($addition_page != ''){
wp_list_pages( array('title_li'=>'','include'=>$addition_page->ID) );
}
}
?></ul></section><?php
}
EDIT:
Ok, so now I know why it's not showing up... I told it not to! I'm using the exclude pages plugin so that the page doesn't show up in the primary nav, and that apparently affects anything that uses wp_list_pages. So now I need to find a way to force it in there... Any thoughts?
EDIT2:
Have now disables the exclude pages plugin and using a custom menu for the primary nav. Bit of a pain but I guess not too bad. If anyone's got a better suggestion let me know!

Categories