Getting unknown and irrelevant meta on the order page - woocommerce - php

on any new order m getting unknown and irrelevant meta on the order page, I donk know the issue, can anyone help me out, please? m sharing an image of an order, and the same meta is also coming in emails as well.
Sample Imgae

Actually I'm looking for the same but I found something, you can try this
stackoverflow post.
//remove order item meta key
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'mobilefolk_order_item_get_formatted_meta_data', 10, 1 );
function mobilefolk_order_item_get_formatted_meta_data($formatted_meta){
$temp_metas = [];
foreach($formatted_meta as $key => $meta) {
if ( isset( $meta->key ) && ! in_array( $meta->key, [
'_fgf_gift_product',
'_fgf_gift_rule_id',
'_fgf_gift_rule_mode'
] ) ) {
$temp_metas[ $key ] = $meta;
}
}
return $temp_metas;
}

Related

WP post_updated $post_after, $post_before showing same product categories

I'm trying to automatically update an xml feed after I update a product of a certain product category...
I found out I can do that with the post_updated hook.
The feed only contains products of category "Willhaben". So whenever I remove the category "Willhaben" from a product, I need the feed to be updated in order to keep the feed up to date...
My problem is that after I remove the category from my feed the post_updated hook doesn't trigger anymore, as I added an if in order to not update the feed when a product without the category "Willhaben" gets updated, to avoid overload.
I tried using $post_after, $post_before to check if the product once had the category "Willhaben" and then rebuild the feed, but $post_after, $post_before always give me the exact same list of categories for that specific product...
Here is my code:
function wpdocs_run_on_transition_only( $post_ID, $post_after, $post_before ) {
if(has_term( 1467, 'product_cat', $post_before ) || has_term( 1467, 'product_cat', $post_after)) {
create_gebraucht_feed(true);
return;
}
}
add_action( 'post_updated', 'wpdocs_run_on_transition_only', 10, 3 );
So because the category list is always the same, I can't determine if the product had the category "Willhaben" and therefore the feed doesn't get created...
I hope it's clear what I mean... does anyone here have an idea what I'm doing wrong? I'm facing this issue for hours now and don't know what to do anymore...
Thx a lot for your time, I'd appreciate some help, thank you!
Yes #RomkaLTU ! Thx a lot that worked for me, here is what I ended up with:
if ( get_post_meta($post_ID, 'is_feed', true) == 1 || has_term( 1467, 'product_cat', $post_after )) {
create_gebraucht_feed(true);
if(!has_term( 1467, 'product_cat', $post_after )) {
update_post_meta($post_ID, 'is_feed', 0);
}
return;
}
Maybe if you can't determine the difference, use this apreach:
update_post_meta($post_ID, 'is_feed', 1)
So if you update a post and it doesn't contain the required category update is_feed to 0.
update_post_meta should be at the end of the function. Check It at the beginning.
I had the same problem: the variables $post_before and $post_after had the same category although I updated the categories while saving.
I have read somewhere else that the post_updated-hook runs too late so that both variables hold the same category. I ended up using the pre_post_update-hook like this:
function myFunctionBeforeUpdate($post_id, $data) {
//$post_id corresponds to the post BEFORE the update
//$_POST holds all the information for the post AFTER the update
$old_cats = get_the_category($post_ID);
$new_cat_ids = ($_POST["post_category"]);
$old_cat_names = array();
$new_cat_names = array();
foreach ( $old_cats as $category ) {
$old_cat_names[] = $category->name;
}
foreach ( $new_cat_ids as $cat_id ) {
$cat = get_category( $cat_id );
$new_cat_names[] = $cat->name;
}
// you can now use the arrays $old_cat_names and $new_cat_names
// which contain the names (not IDs) of the old and new categories
}
add_action('pre_post_update', 'myFunctionBeforeUpdate', 10, 2 );

Is there a way to fix this "Trying to get property 'taxonomy' of non-object" error on my Wordpress/Woocommerce site?

I'm setting up a new Wordpress/Woocommerce website using the Avada theme. The site is not live yet.
The store has custom product taxonomies, and as some of the taxonomy names contain a comma I've had to find a code snippet to do a workaround in PHP so that the comma is displayed without splitting the taxonomy name into two separate names (eg. Bouncy Balls, Putty and Slime). The PHP I found has replaced a double dash (--) with a comma in the front end, which appears to work as it should.
My problem is, that since I have added this code I'm getting an error at the top of the page on individual product pages:
Notice: Trying to get property 'taxonomy' of non-object in /var/sites/a/mywebsite.com/public_html/wpnew/wp-content/themes/Avada-Child-Theme/functions.php on line 32
Line 32 code:
if( $tag_arr->taxonomy == 'post_tag' && strpos( $tag_arr->name, '--' ) ) {
All of the code I inserted into functions.php is below.
I'm not sure what to try to fix this as this is all new to me and I'm learning as I go along. I have very little knowledge of PHP, besides the snippet I have found online and placed here.
if( !is_admin() ){
function comma_tag_filter( $tag_arr ){
$tag_arr_new = $tag_arr;
if( $tag_arr->taxonomy == 'post_tag' && strpos( $tag_arr->name, '--' ) ) {
$tag_arr_new->name = str_replace( '--', ', ', $tag_arr->name );
}
return $tag_arr_new;
}
add_filter( 'get_post_tag', 'comma_tag_filter' );
function comma_tags_filter( $tags_arr ) {
$tags_arr_new = array();
foreach( $tags_arr as $tag_arr ) {
$tags_arr_new[] = comma_tag_filter( $tag_arr );
}
return $tags_arr_new;
}
add_filter( 'get_terms', 'comma_tags_filter' );
add_filter( 'get_the_terms', 'comma_tags_filter' );
}
I expected this code to work without errors, but I have the error message at the top of the product pages.
Thanks for any help

Wordpress / WooCommerce Back-Order Custom Text

I have found two bits of code that are really useful for setting "backorder" text on our website, however they need to be combined to work properly:
The first simply changes the text for all backorders
And the second works when a meta_value is present by displaying the custom meta_value text, however displays nothing if no meta_value is present.
Ideally the way i would want it to work, is for it to display the custom text if there is something present, or some pre-defined text if the meta_value is unavailable for that product.
Any help would be GREATLY appreciated!!
FIRST SAMPLE CODE I FOUND:
function backorder_text($available) {
foreach($available as $i) {
$available = str_replace('Available on backorder', 'Will ship in March', $available);
}
return $available;
}
add_filter('woocommerce_get_availability', 'backorder_text');
SECOND SAMPLE CODE I FOUND:
add_filter( 'woocommerce_get_availability' , 'revised_woocommerce_get_availability' , 10, 2 );
function revised_woocommerce_get_availability( $available_array , $product) {
if ( $product->managing_stock() ) {
if ( !($product->is_in_stock() && $product->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' )) && ($product->backorders_allowed() && $product->backorders_require_notification()) ) {
$custom_meta_value = get_post_meta( $product->id, 'Out_of_stock_message', true );
$available_array["availability"] = $custom_meta_value;
}
}
return $available_array;
}

pull out data from database and display in Gravity form

I am using gravity forms plugin, and I'm trying to display the categories as a drop down list in the form I have already created.
If required, please here's a link to my website
I've been on this for too long, and no way out. Kindly help me out.
add_filter( 'gform_pre_render_1', 'populate_categories' );
add_filter( 'gform_pre_validation_1', 'populate_categories' );
add_filter( 'gform_pre_submission_filter_1', 'populate_categories' );
add_filter( 'gform_admin_pre_render_1', 'populate_categories' );
function populate_categories( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->id != 1 ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$categories = get_categories ;
$choices = array();
foreach ( $categories as $categories ) {
$choices[] = array( 'text' => $categories->name, 'value' => $categories->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Category';
$field->choices = $choices;
}
return $form;
}
You can dynamically generate drop downs for gravity forms using the syntax provided below in this link. You have to take control over the functions.php file of the theme to retrieve your output as per your requirement.
Here is the clear documentation provided and you can create in a simple manner using this methods. There are two methods available refer to it.
https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
If you face any problem with creation let me know we shall solve it.

Wordpress updating screen_layout_post user meta for 1 column layout

I am trying to get Wordpress to show a 1 column layout when adding a new post using the following code in my functions file.
function wpsnippy_one_columns_posts_layout( $columns ) {
$columns['vehicle'] = 1;
return $columns; } add_filter( 'screen_layout_columns', 'wpsnippy_one_columns_posts_layout' ); function wpsnippy_screen_layout_posts() {
return 1; } add_filter( 'get_user_option_screen_layout_post', 'wpsnippy_screen_layout_posts' );
This is working fine if logged in as admin, however I have a user type 'seller' and it is not working for seller, I actually need it to be the other way around, 1 column if seller 2 if anything else.
Your input will be greatly appreciated, many thanks.
We can filter the function get_user_option() and force 1 column:
add_filter( 'get_user_option_screen_layout_post', function( $result, $option, $user )
{
if( in_array( 'seller', $user->roles ) )
$result = '1';
return $result;
}, 10, 3 );
Note that the option is defined like: "screen_layout_$page".

Categories