WordPress WooCommerce Membership plan add column to list - wordpress

I found this 2 filters for WooCommerce to extend the membership plan list:
add_filter( 'manage_edit-wc_user_membership_columns', array( $this, 'customize_columns' ) );
add_filter( 'manage_edit-wc_user_membership_sortable_columns', array( $this, 'customize_sortable_columns' ) );
I want to add a new column with the memberships plan id to show.
any suggestion on how to use that in the functions.php

You found the correct filter manage_edit-wc_user_membership_columns – it allows to add a column in membership plans, example:
add_filter( 'manage_edit-wc_user_membership_columns', 'my_add' );
function my_add( $columns ) {
$columns['id_of_the_plan'] = 'Memberships plan id';
return $columns;
}
Once you insert this code in your current theme functions.php file or in a custom plugin, the column will appear. Now it is time to add the data to it. manage_posts_custom_column will help with it.
add_action( 'manage_posts_custom_column', 'my_id' );
function my_id( $column ) {
if( $column == 'id_of_the_plan' ) {
$m = wc_memberships_get_user_membership( get_the_ID() );
echo $m->plan_id;
}
}
The original code is taken from this example.

Related

I want to show a new table on checkout page to show how much user save on a product ACF Wordpress WooCommerce

I working on a WordPress website where users can purchase a membership and I want to Show that if a user is a member how much he can save. I'm using ACF.
My Question is how can I show ACF field on a checkout page next to product???
WordPress in general and WooCommerce have the thing called "Hook" to help us add our custom data.
This article is an introduction and example about WooCommerce Hooks
This is a virtual version of hooks on the WooCommerce checkout page.
So as your needs, you may use woocommerce_review_order_before_cart_contents hook, add this code to functions.php file in your theme:
Add to the checkout page in general
add_action( 'woocommerce_review_order_before_cart_contents', 'add_member_info' );
function add_member_info() {
echo '<div class=”member-message”>Become a member will save $100</div>';
}
Get data from a custom field in the product
I. To show in the single product
function cw_change_product_price_display( $price, $product ) {
$my_field = get_field('member_save', $product->ID);
$price .= " | Member save $$my_field";
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display', 10, 2);
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display', 10, 2);
II. To show in the checkout page
Add custom data with the product to Card
add_filter( 'woocommerce_add_cart_item_data', function ( $cartItemData, $productId, $variationId ) {
$member_saved = get_field('member_save', $productId);
$cartItemData['myCustomData'] = $member_saved;
return $cartItemData;
}, 10, 3 );
add_filter( 'woocommerce_get_cart_item_from_session', function ( $cartItemData, $cartItemSessionData, $cartItemKey ) {
if ( isset( $cartItemSessionData['myCustomData'] ) ) {
$cartItemData['myCustomData'] = $cartItemSessionData['myCustomData'];
}
return $cartItemData;
}, 10, 3 );
Show the data in the cart and checkout page
add_filter( 'woocommerce_get_item_data', function ( $data, $cartItem ) {
if ( isset( $cartItem['myCustomData'] ) ) {
$data[] = array(
'name' => 'Member saved',
'value' => $cartItem['myCustomData']
);
}
return $data;
}, 10, 2 );

Sort Woocommerce products by most viewed using Post View Counter

Any ideas would be much appreciated.
I am trying to reorder my woocommerce products by most viewed using the post-views-counter plugin.
Followed these examples which did not seem to work Sort products by most viewed
function my_view_filter($query){
if ($query->is_main_query() && ( $query->is_home() || $query- >is_archive()
)
) {
$query->set('post_type', 'product');
$query->set('suppress_filters', false);
$query->set('orderby', 'post_views');
$query->set('order', 'asc');
$query->set('fields', '');
}
}
add_action( 'pre_get_posts', 'my_view_filter' );
First of all, the plugin you're trying to use is outdated. Secondly, it seems to create a table to hold the views and this would require changing the actual MySQL query to retrieve posts ordered by date which is a lot of work looking what you need to do.
You could simply just save views on visit to post meta and use that to order products on the catalog like this:
/**
* Setting post count on each visit
*
*/
add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
function prefix_save_product_views( ) {
$product_id = get_the_ID();
$increment = 1;
$current_visit_count = get_post_meta( $product_id, 'product_visit_count', true );
$total_visit_count = (int)$current_visit_count + $increment;
update_post_meta( $product_id, 'product_visit_count', $total_visit_count );
}
/**
* Change the display order based on visit count only in Catalog
*
*/
add_filter('woocommerce_get_catalog_ordering_args', 'prefix_woocommerce_catalog_orderby');
function prefix_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'product_visit_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
return $args;
}
You'd run into multiple issues using pre_get_posts
// Remove product category/tag meta from its original position
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
// Add product meta in new position
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 5 );

Woocommerce remove admin order item item meta

Im adding a custom item meta to every item with woocommerce_add_order_item_meta action.
I dont need to show this custom meta in the Order Detail, because it's an arry stringy that im using to print a pdf.
How can i remove this meta custom item? Is there some action to do it?
Thanks
I understand its a bit old question but I am answering for some other users who will have same issue in future.
If you want your order item meta to not display in admin order details page than you should append underscore (_) at the start of your meta name.
Example:
_custom_order_meta
The underscore trick no longer works. In Woo 3.x there is a hidden meta array:
add_filter('woocommerce_hidden_order_itemmeta',
array($this, 'hidden_order_itemmeta'), 50);
function hidden_order_itemmeta($args) {
$args[] = 'my_hidden_meta';
return $args;
}
It sounds like you need to keep it in order to print the PDF. If you override the order-details.php template you can possibly change:
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
to
$array = $item['item_meta'];
if( isset( $array['your_pdf_array_key'] ) ){ unset( $array['your_pdf_array_key'] ); }
$item_meta = new WC_Order_Item_Meta( $array, $_product );
EDIT
The wc_add_order_item_meta() function has 4 parameters as seen in the code:
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'order_item', $item_id, $meta_key, $meta_value, $unique );
}
If you choose a $meta_key with a preceding underscore, the meta will be automatically hidden from view on the checkout/order-received page, the My Order's list of the My account area, as well as in the admin's order overview page.
Therefore, I would suggest making your woocommerce_add_order_item_meta callback function look something like the following:
add_action( 'woocommerce_add_order_item_meta', '25979024_add_order_item_meta', 10, 3 );
function 25979024_add_order_item_meta( $order_item_id, $cart_item, $cart_item_key ) {
wc_add_order_item_meta( $order_item_id, '_pdf_something', 'hide this stuff' );
}

How to Display Custom meta field value insted of title on custon post list table?

I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.
function remove_box(){
remove_post_type_support('cinfo', 'title');
remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");
It looks something like this.
Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.
I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.
add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
$defaults['title'] = 'Email';
return $defaults;
}
add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
if ($column_name == 'title') {
echo "its working";
}
}
The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.
I tried this with a fast class to test all together inside my setup:
class SO23467344
{
private $cpt = 'portfolio';
private $custom_field = 'video';
public function __construct()
{
add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
add_action( "admin_init", array( $this, "remove_box" ) );
}
function column_register( $columns )
{
$columns['my-col'] = 'My column';
# Remove default columns
unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );
return $columns;
}
function column_display( $column_name, $post_id )
{
if ( 'my-col' != $column_name )
return;
if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
}
function remove_box(){
remove_post_type_support( $this->cpt, 'title' );
remove_post_type_support( $this->cpt, 'editor' );
}
}
new SO23467344;

remove columns from woocommerce product page

Hi I am trying to limit the options certain users have when viewing the products list within woocommerce for wordpress admin pages.
if( current_user_can('vendor') ) {
function my_columns_filter( $columns ) {
unset($columns['tags']);
unset($columns['featured']);
unset($columns['type']);
return $columns;
}
}
add_filter( 'manage_edit-product_columns', 'my_columns_filter', 10, 1 );
Any help or guidance would be much appreciated.
You're doing it wrong.
Place if/else inside the function instead of wrapping the function.
function my_columns_filter( $columns ) {
if( current_user_can('vendor') ) {
unset($columns['tags']);
unset($columns['featured']);
unset($columns['type']);
return $columns;
}
}
add_filter( 'manage_edit-product_columns', 'my_columns_filter', 10, 1 );
You are not using the correct column names, or perhaps WC has changed them since you posted your question. It also better to check if a column still exists before removing it in case WC does change their column names.
Here is a future proof solution you can past into your theme's functions.php:
function my_product_columns_filter( $columns ) {
if ( current_user_can( 'vendor' ) ) {
foreach ( array( 'product_tag', 'featured', 'product_type' ) as $name ) {
if ( isset( $columns[ $name ] ) ) {
unset( $columns[ $name ] );
}
}
}
return $columns;
}
add_filter( 'manage_edit-product_columns', 'my_product_columns_filter' );
no coding solution
If you're just looking for a quick solution without the need for coding, you could use the free admin columns plugin from wordpress.org, which allows you to add and remove columns with a few clicks.

Resources