Where would I add this add_action() code influencing the product admin view? - woocommerce

I have a piece of code from another answer that customizes the way products are displayed in the admin view. It appears to be adding a WordPress hook using add_action().
add_action( 'manage_product_posts_custom_column', 'wpso23858236_product_column_offercode', 10, 2 );
function wpso23858236_product_column_offercode( $column, $postid ) {
if ( $column == 'offercode' ) {
echo get_post_meta( $postid, 'offercode', true );
}
}
However, I am unclear where I have to place this code for it to have the desired effect.

Related

How do i make functions code as shortcode?

I’d like to move Woocommerce`s Additional information field into the Accordion tap, which is below “add to cart” button.
I put the Accordion tap to shortcode using plugin already.
And created Shortcode using the code below, but it doesn’t work normally.
in functions.php file
include('custom-shortcode.php');
//remove additional_information and reviews tabs
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset($tabs['additional_information']);
unset( $tabs['reviews'] ); // Removes the reviews tab
return $tabs;
}
in custom-shortcode.php
function additional_info_under_add_to_cart( ) {
global $product;
if ( $product && ( $product->has_attributes() || apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) {
wc_display_product_attributes( $product );
}
}
add_shortcode('custom-shortcode', 'additional_info_under_add_to_cart');
I can insert Additional information field using [custom-shortcode] into accordion, but it’s not put in the accordion tap but on another location.
Please let me know how do i solve this problem.
Thank you.

hide wordpress posts and categories for not logged in users

Basically I handled this issue by overriding parent theme files in child theme.
i.e., by using
if( is_user_logged_in() ){
...................
wordpress loop
}
I had to do this for every template ( wherever there is wordpress loop )
Though I didn't have to do this for displaying sidebars as if there was category in widget and whenever user clicks on it to view it, it would automatically say you don't have proper privileges to view this content.
So, my question is, is there any better way to hide wordpress content ( this may be anything, like normal posts, custom post types,.. ) from just visitors.
function bt_hide_from_guestes( $content ) {
global $post;
if ( $post->post_type == 'post' ) {
if ( !is_user_logged_in() ) {
$content = 'Please login to view this post';
}
}
return $content;
}
add_filter( 'the_content', 'bt_hide_from_guestes' );
Above code didn't help
It looks like your filter function should work for you. Try setting a priority and the "accepted arg" number:
add_filter( 'the_content', 'bt_hide_from_guestes', 10, 1 );

How to save custom meta only for review data to see only in "Preview Changes", not the actual post in the front end

I am trying to add custom meta and see at "Preview Changes". I can see the changes but also changes apply to the actual post at Front end. I want the changes will update to the actual post when it Publish or Update not at "Preview Changes" click. Please help.
I have followed this plugin.
function my_plugin_save_post( $post_id, $post ) {
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$parent = get_post( $parent_id );
$my_meta = get_post_meta( $parent->ID, 'my_meta', true );
if ( false !== $my_meta )
add_metadata( 'post', $post_id, 'my_meta', $my_meta );
}
} add_action( 'save_post', 'my_plugin_save_post' );
The following code will prevent your meta data from saving on preview but You won't be able to preview published posts with your metadata. Honestly I'm trying to figure out this situation myself :/
<?php // In your save metabox data function, near the top...
if (isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] ) {
if(get_post_status($post_id) == 'publish'){
return; // This way we can still preview draft / scheduled posts
}
}
Honestly, I'd use this code and set your post briefly to draft or private while editing / previewing and publish them as normal when you're done.

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;

Resources