Woocommerce: append atribute value to order-notes - woocommerce

I'm trying to append the atribute value of pa-lager to delieverynotes and invoices. I'm working from this answer.
This is what I have:
function lager_placering( $product ) {
if( isset( $product->id ) ) {
$lager = $product->get_attribute('pa_lager');
//echo $lager;
}
}
add_action( 'wcdn_order_item_after', 'lager_placering' );
It does not work. at all.

This works like a charm
function lager_placering( $product ) {
$lager = $product->get_attribute('pa_lager');
echo $lager;
}
add_action( 'wcdn_order_item_after', 'lager_placering' );

Related

Problem with woocommerce_add_order_item_meta replacement code

I previously had this code to add custom field values to the Order and Order emails. This worked and still works but I'm getting errors in the error log due to the code being depreciated:
add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
if ( empty( $values['_goodlen'] ) ) {
return;
}
woocommerce_add_order_item_meta( $item_id, 'Flavours', $values['_goodlen'] );
}
I've replaced it with:
add_action ('woocommerce_checkout_create_order_line_item', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
if ( empty( $values['_goodlen'] ) ) {
return;
}
woocommerce_checkout_create_order_line_item( $item_id, 'Flavours', $values['_goodlen'] );
}
But that no longer adds the content to the email and orders.
Any ideas or help would be much appreciated.
Ah, I think I may have answered my question. Been looking around for ages and found this code:
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
// Get a product custom field value
$custom_field_value = get_post_meta( $item->get_product_id(), '_goodlen', true );
// Update order item meta
if( isset( $values['_goodlen'] ) ) {
$item->update_meta_data( 'Flavours', $values['_goodlen'] );
}
}
Seems to work and display in both order and order email and doesn't throw up any errors in the log.

How to remove Author Tag from being visible in Discord's previews?

When sharing a post to Discord, the preview Discord generates shows the author name and URL. We removed all information about the author but it didn't stop the author tag from showing.
That’s done via oEmbed. Add below code in your functions.php file
add_filter( 'oembed_response_data', 'disable_embeds_filter_oembed_response_data_' );
function disable_embeds_filter_oembed_response_data_( $data ) {
unset($data['author_url']);
unset($data['author_name']);
return $data;
}
**disorc may have stored the response in cache so create new post or page and test that **
#hrak has the right idea but his answer lacks context for those of us not used to dealing with PHP.
What I ended up doing was check if there was already a filter for 'oembed_response_data' in /wp-includes/default-filters.php. Mine looked like this:
add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 );
Add the previous line to the specified file if, for whatever reason, it isn't there already.
Afterward, I checked in wp-includes/embed.php for the get_oembed_response_data_rich function, which looked like this:
function get_oembed_response_data_rich( $data, $post, $width, $height ) {
$data['width'] = absint( $width );
$data['height'] = absint( $height );
$data['type'] = 'rich';
$data['html'] = get_post_embed_html( $width, $height, $post );
// Add post thumbnail to response if available.
$thumbnail_id = false;
if ( has_post_thumbnail( $post->ID ) ) {
$thumbnail_id = get_post_thumbnail_id( $post->ID );
}
if ( 'attachment' === get_post_type( $post ) ) {
if ( wp_attachment_is_image( $post ) ) {
$thumbnail_id = $post->ID;
} elseif ( wp_attachment_is( 'video', $post ) ) {
$thumbnail_id = get_post_thumbnail_id( $post );
$data['type'] = 'video';
}
}
if ( $thumbnail_id ) {
list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
$data['thumbnail_url'] = $thumbnail_url;
$data['thumbnail_width'] = $thumbnail_width;
$data['thumbnail_height'] = $thumbnail_height;
}
return $data;
}
I just added the two lines of code that #hrak introduced in his answer to remove the author tag (name and URL) from $data before it was returned:
function get_oembed_response_data_rich( $data, $post, $width, $height ) {
(...)
if ( $thumbnail_id ) {
list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
$data['thumbnail_url'] = $thumbnail_url;
$data['thumbnail_width'] = $thumbnail_width;
$data['thumbnail_height'] = $thumbnail_height;
}
unset($data['author_url']);
unset($data['author_name']);
return $data;
}
As before, add the get_oembed_response_data_rich function if it does not already exist. After about 5-10 minutes, Discord link embeds stopped showing the author tag.
Source:
http://hookr.io/filters/oembed_response_data/
https://developer.wordpress.org/reference/functions/get_oembed_response_data_rich/
I've done this by emptying the posted_by function like this article shows, in the "Use Code to Remove the Author Name" section
https://wpdatatables.com/how-to-hide-the-author-in-wordpress/
basically find the posted_by function and empty it
function twentynineteen_posted_by() {
}
endif;

Remove custom body class from pages with a specific category wordpress

I am trying yo Remove custom body class from pages with a specific category in WordPress.
Here is the code below I am trying to make to work. However, it does not.
function remove_body_class($wp_classes) {
if ( is_category ('places') ) :
foreach ( $wp_classes as $key=>$value ) {
if ( $value =='my_class' ) unset( $wp_classes[ $key ] );}
endig;
return $wp_classes;
} add_filter( 'body_class', 'remove_body_class');
It works when I remove class from all pages without using "if ( is_category ('places') ) :"
But I can't make it work only for specific category/posts.
Could you tell me if I do something wrong? I would highly appreciate it.
Thank you.
it will be help for you.
// Removes a class from the body_class array.
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
// 34 is your category id
if (is_category('34'))
{
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
}
// When the archive page for Category 34 is being displayed.
Define $cat_id to the category ID and change "your-class-name" to the name of the class you want removed:
if ( is_category($cat_id) ) {
add_filter( 'body_class', function( $classes ) {
if ( null !== array_search( 'your-class-name', $classes) ) {
unset( $classes[ array_search( 'your-class-name', $classes) ] );
}
return $classes;
} );
}

how update post meta when post type post edit and save

hi all here is my function
function save_urun_meta_price( $post_id ) {
$slug = 'urun';
if ( $slug != $_POST['post_type'] ) {
return;
}
// - Update the post's metadata.
if ( isset( $_REQUEST['urun_indirimli_fiyat'] ) ) {
$product_price = get_post_meta( $post_id, 'urun_fiyat', true );
$product_discount = of_get_option('urun_discount');
$yuzde = ($product_discount / 100)*$product_price;
$discounted = $product_price-$yuzde;
update_post_meta( $post_id, 'urun_indirimli_fiyat', $discounted );
}
}
add_action( 'save_post', 'save_urun_meta_price' );
add_action( 'edit_post', 'save_urun_meta_price' );
When user write some price into the urun_fiyat meta field i want to calculate this price with % discount field from the options framework panel.
Than i want to put new price another meta field urun_indirimli_fiyat..
What is wrong with my function ?
Thanks.
Try using the below code. I think the problem was with the product price variable. You were trying to get the value from post meta ( which does not exist )
Getting the value from $_POST variable will do the trick I guess.
function save_urun_meta_price( $post_id ) {
$slug = 'urun';
if ( $slug != $_POST['post_type'] ) {
return;
}
// - Update the post's metadata.
if ( isset( $_REQUEST['urun_indirimli_fiyat'] ) ) {
$product_price = $_POST['urun_fiyat'];
$product_discount = of_get_option('urun_discount');
$yuzde = ($product_discount / 100)*$product_price;
$discounted = $product_price-$yuzde;
update_post_meta( $post_id, 'urun_indirimli_fiyat', $discounted );
}
}
add_action( 'save_post', 'save_urun_meta_price' );
add_action( 'edit_post', 'save_urun_meta_price' );

Highlighting page in menu on custom post types

I have a page called "Portfolio". I use this page to show the archive for my custom post type called "Works". I do this by displaying the portfolio page on with a custom template called "Work archive".
I would like to highlight the Portfolio page in my menu when I am on a single post of Works.
Can You help me?
This could help you
function change_page_menu_classes($menu){
global $post;
if (get_post_type($post) == 'portfolio')
{
$menu = str_replace( 'current_page_parent', '', $menu ); // remove all current_page_parent classes
$menu = str_replace( 'page-item-366', 'page-item-366 current_page_parent', $menu ); // add the current_page_parent class to the page you want
}
return $menu;
}
add_filter( 'wp_page_menu', 'change_page_menu_classes', 0 );
Source
Hey I dunno if this is still relevant but I came across this and it worked great. I'm using the roots theme with a post type of "projects"
// Remove active class from menu
function remove_active_class($class) {
return ( $class == 'active' ) ? FALSE : TRUE;
}
// Add active class to menu of post type single template
function add_class_to_wp_nav_menu($classes) {
if( is_singular( 'projects' ) ) {
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-projects', $classes) ) {
$classes[] = 'active';
}
} elseif( is_singular( 'resources' ) ) {
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-resources', $classes) ) {
$classes[] = 'active';
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
function namespace_menu_classes( $classes , $item ){
if ( get_post_type() == 'attorneys' ) {
$classes = str_replace( 'current_page_parent', '', $classes );
if ( $item->url == '/attorneys' ) {
// Replace "attorneys" with your code
if(preg_match('/attorneys/', $item->url)) {
$classes = str_replace( 'menu-item', 'menu-item current_page_parent', $classes );
}
}
return $classes;
}
Altered from here: https://wordpress.org/support/topic/custom-post-type-highlighting-current-menu-item

Resources