I'm using below
add_action('transition_post_status', 'my_product_update', 1000, 3);
function my_product_update($new_status, $old_status, $post) {
if($new_status == 'publish' && $post->post_type == "product") {
$product_id = $post->ID;
$product = new WC_Product($product_id);
echo $product->get_price();
}
}
I figure it out that $product->get_price() is string(0) "".
I think it beacause I retrieve the price before it's saved.
But I can retrieve the name by using $product->post->post_title
Any idea how I can get the price right after the product is published?
Thank you.
Try This :
function wpa104760_default_price( $post_id, $post ){
echo $price = get_post_meta( $post_id, '_regular_price', true);
echo $sale = get_post_meta( $post_id, '_sale_price', true);
// exit;
}
add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price',1000,2 );
Related
I want a function that triggers when a new WordPress post publish.
add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $post_id, $post ) {
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $post_id );
$edit = get_edit_post_link( $post_id, '' );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$message = sprintf( 'Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $message, $headers );
}
I tried this one. I tried also different statuses such as save_post but I didn't get any email when the user add a new post and publish it. so it doesn't work. how can I solve this problem?
You use the publish_post hook. If get_post_meta() returns a value, it will trigger your actions to occur.
function run_on_post_publish( $post_id ) {
$post_data = get_post_meta( $post_id, 'run_once', true );
if ( ! empty( $post_data ) ) {
return;
}
// Run the action you wish to occur when a post is published
update_post_meta( $post_id, 'run_once', true );
}
add_action( 'publish_post', 'run_on_post_publish' );
I prepared following code to overwrite post tags following several meta keys.
Now, it works for only one meta key. How can I apply it to several meta keys?
<?php
global $post;
$post_id = $post->ID;
$tags[] = get_post_meta($post_id, 'aaaaa', $single);
$tags[] = get_post_meta($post_id, 'bbbbb', $single);
$tags[] = get_post_meta($post_id, 'ccccc', $single);
$tags = array_unique( array_filter( $tags ) ); // here, adding array_filter to remove empty get_post_meta results
wp_set_post_tags( $post_id, $tags, false );
?>
By above code, meta value of meta key aaaaa only applied as post tag, and meta value of meta key bbbbb and ccccc is ignored.
After I hear advise below, I tried this code too.
<?php
global $post;
$post_id = $post->ID;
$tags = get_post_meta($post_id, 'aaaaa', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tags = get_post_meta($post_id, 'bbbbb', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tags = get_post_meta($post_id, 'ccccc', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
wp_set_post_tags( $post_id, $tags, false );
?>
Then, all tag is eliminatd.
Your code looks right, but I suggest that you check if the meta key bbbbb and ccccc does have value or not int he first place. So try to display something like that.
Also, give this a go and try to add one tag in time.
https://developer.wordpress.org/reference/functions/wp_add_post_tags/
Like this:
<?php
global $post;
$post_id = $post->ID;
$tag = get_post_meta($post_id, 'aaaaa', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tag = get_post_meta($post_id, 'bbbbb ', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tag = get_post_meta($post_id, 'ccccc', $single);
if (!empty() ) { wp_add_post_tags( $post_id, $$tag ); }
?>
Hope this help you.
global $post;
$post_id = $post->ID;
$array_tags_and_ids = [
$post_id => ["aaaaa", "bbbbb", "ccccc"]
];
foreach ($array_tags_and_ids as $key => $value) {
// re-call the value and get only the text value
$implode_tags_names = implode( ', ', $value );
// wordpress function
wp_set_post_tags( $key, $implode_tags_names, true );
}
I changed third argument to true(default is false), and it started to work!
like this
global $post;
$post_id = $post->ID;
$tags[] = get_post_meta( $post->ID, 'aaaaa', true );
$tags[] = get_post_meta( $post->ID, 'bbbbb', true );
$tags[] = get_post_meta( $post->ID, 'ccccc', true );
$tags = array_unique( array_filter( $tags ) );
wp_set_post_tags($post_id , $tags, false );
Just like the title said:
How can i show only the sales price in Woocommerce if the regular price is blank?
I would go for something like:
$regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
$price_sale = get_post_meta( get_the_ID(), '_sale_price', true);
if ($regular_price == '') {
echo $price_sale;
} else {
echo $regular_price;
}
add_filter( 'woocommerce_get_price_html', 'change_product_price_html', 10, 2 );
function change_product_price_html( $price_html, $product ) {
global $post;
$id = !empty( $product->get_id() ) ? $product->get_id() : $post->ID;
$regular_price = get_post_meta( $id, '_regular_price', true);
$sale_price = get_post_meta( $id, '_sale_price', true);
$price_html = ( $regular_price == '' ) ? $sale_price : $regular_price;
return $price_html;
}
Please use this code in your active theme's functions.php file .
I am creating a plugin where I need to update the input field post meta when the actual post is updated. So here is my code:
function save_meta_function ( ) {
global $post;
$post_id = $post->ID;
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );
but it's showing several errors:
Notice: Trying to get property of non-object // that's $post_id
= $post->ID;
Can you tell me why $post_id = $post->ID; line showing that error?
Replace your code with follows -
function save_meta_function ( $post_id ) {
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php