Unable to get custom product meta with woocommerce_thankyou - wordpress

Using get_post_meta works elsewhere in my plugin, but when I want to use it with woocommerce_thankyou nothing is returned.
function my_function($order_id)
{
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item_id => $item) {
$customer_meta = get_post_meta($item->get_id(), '_my_custom_meta', true);
error_log("Meta value - " . $customer_meta);
}
}
add_action('woocommerce_thankyou', 'my_function');
Checking $item->get_id does confirm the product ID is being read, but get_post_meta isn't working here.

Related

Redirecting customers to custom page after sale in WooCommerce

I'm trying to learn WooCommerce and WordPress plugins so I'm tweaking around. I'm trying to create a plugin that redirects customer to a custom page after checkout. The custom page/url can be defined when I create the product. Here is my code:
<?php
/*
Plugin Name: Custom Redirect After Sale
Description: Redirects customers to a custom page after a successful sale.
*/
// Register a new meta field for products
add_action( 'add_meta_boxes', 'custom_redirect_meta_box' );
function custom_redirect_meta_box() {
add_meta_box( 'custom_redirect_meta_box', 'Custom Redirect URL', 'custom_redirect_meta_box_callback', 'product', 'side' );
}
function custom_redirect_meta_box_callback( $post ) {
$value = get_post_meta( $post->ID, '_custom_redirect_url', true );
echo '<label for="custom_redirect_url">Custom Redirect URL:</label>';
echo '<input type="text" id="custom_redirect_url" name="custom_redirect_url" value="' . esc_attr( $value ) . '" style="width:100%">';
}
// Save the meta field value when the product is saved
add_action( 'save_post_product', 'save_custom_redirect_meta_box_data' );
function save_custom_redirect_meta_box_data( $post_id ) {
if ( isset( $_POST['custom_redirect_url'] ) ) {
update_post_meta( $post_id, '_custom_redirect_url', sanitize_text_field( $_POST['custom_redirect_url'] ) );
}
}
// Redirect to the custom page after a successful sale
add_action( 'woocommerce_payment_complete', 'custom_redirect_after_sale' );
function custom_redirect_after_sale( $order_id ) {
$order = wc_get_order( $order_id );
//$order->update_status( 'completed' );
$items = $order->get_items();
// Get the first product in the order
$product = reset($items);
// Get the custom redirect URL for the product
//$redirect_url = get_post_meta( $product->get_product_id(), '_custom_redirect_url', true );
$redirect_url = get_post_meta( $product->get_id(), '_custom_redirect_url', true );
//echo "Meta retrieved: " . $redirect_url;
//error_log("callback fired");
//echo "Payment complete ho ho ho";
if( $redirect_url ) {
wp_redirect( $redirect_url );
exit;
}
}
It seems the woocommerce_payment_complete hook is not firing. I tried to echo out the redirect url and text but it doesn't seem to work.
I'm on localhost and I'm using the cash on delivery payment method.
Basing this answer on the great https://rudrastyh.com/ - specifically this tutorial https://rudrastyh.com/woocommerce/thank-you-page.html#redirects this is the code that should work for what you are trying to do.
First, you hook into the template_redirect action to determine the URL where the customer needs to go
Getting the Order ID, you can get the products purchased for that order
Once you have the purchased products, you can get their ID and meta data, the redirect URL you saved for each. Note that while you use WP functions for handling meta, when working with WooCommerce it is best practice to use its CRUD methods. In case in the future they port products to custom tables, your code will continue working.
Implement the redirect with the WP function wp_safe_redirect
Note that what you are trying to achieve will have problems if customers purchase orders with more than 1 product, and you have more than 1 redirect URL. In this implementation, the first product in the order that has a saved redirect URL will override all others
add_action( 'template_redirect', 'purchased_product_redirect');
function purchased_product_redirect(){
if( !function_exists( 'is_wc_endpoint_url' )){
return;
}
// do nothing if we are not on the order received page
if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
return;
}
// Get the order ID
$order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
if(!$product){
continue;
}
//Get the first product's redirect URL
$product_redirect_url = $product->get_meta('_custom_redirect_url');
if(!$product_redirect_url){
continue;
}
wp_safe_redirect( $product_redirect_url );
exit; // always exit after using wp_safe_redirect
}
}

Woocommerce - How to programmatically stop sending email notification (sometime)

I try not to send email notification on woocommerce, only in some cases. Not all the time.
For example, I do not want to send the email for a new order that will contain the product X.
I searched for a special hook, but I didn't find out...
I tried to put an "exit" on email-header.php but it's not the good solution.
Do you know if there is a hook to do that?
Thank you
Have a try with this code to add in the functions.php file of your theme.
Replace xxx with the ID of the product
function change_email_recipient_depending_of_product_id( $recipient, $order ) {
global $woocommerce;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( $product_id == xxx ) {
$recipient = '';
}
return $recipient;
}
}
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'change_email_recipient_depending_of_product_id', 10, 2 );

Cannot get Woocommerce order by ID

I have created a function that is supposed to update certain meta fields once an order has been placed with Woocommerce.
Although I can confirm that the function is fired, I cannot seem to be able to load the order by the ID. Here is my code so far:
add_action('woocommerce_new_order', 'wc_update_bib_options');
function wc_update_bib_options($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
// Some stuff...
}
}
I can confirm that $order_id is passed and is an integer. Also, if I enter an ID of another (previous) order, the code works just fine...
I also tried using
$order = new WC_Order( $order_id );
But I get the same results...
Can anyone help me out?

Save WooCommerce Order Product Name to User Meta Custom Field

Caveat: I'm a solo freelance designer not a developer ;-)
I've created a custom field in the Wordpress user meta called membership.
I've tried the following code to save the WooCommerce Product Name to the membership custom field on checkout with help from this answer.
Updated attempt:
function wascc_woocommerce_checkout_update_user_meta_membership ( $customer_id, $posted ) {
if (isset($posted['$orderid'])) {
$order = $posted['$orderid'];
}
$theorder = new WC_Order( $order );
$items = $theorder->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
}
if (!(empty($product_name))) {
update_user_meta( $customer_id, 'membership', $product_name);
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'wascc_woocommerce_checkout_update_user_meta_membership', 10, 2 );
It produces no error, but does not save the product name to membership.
Help appreciated.
Last question may be related.
as I've commented out, you should use woocommerce_checkout_update_order_meta.
Something like this:
function wascc_woocommerce_checkout_update_user_meta_membership ( $order_id ) {
$theorder = new WC_Order( $order_id );
$items = $theorder->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
}
if (!(empty($product_name))) {
// Gets the customer/user ID associated with the order. Guests are 0.
$customer_id = $theorder->get_user_id();
update_user_meta( $customer_id, 'membership', $product_name );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'wascc_woocommerce_checkout_update_user_meta_membership' );
I have doubts with your foreach loop though... you are looping just to get the last item?

Woocommerce Product publish, update and delete hooks

I need Woocommerce Product publish, update and delete hooks if any one know then please inform me.
I find this hook :
add_action('transition_post_status', 'wpse_110037_new_posts', 10, 3);
function wpse_110037_new_posts($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'publish'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
//add some cde here
}
}
but it's only display product id, title, publish status etc....but i want product price, category, tag, brand and stock status.
So please replay me if any one know.
Thanks,
Ketan.
Woocommerce Products are basically wordpress posts. You can use wordpress hooks
add_action( 'before_delete_post', 'wpse_110037_new_posts' );
add_action( 'save_post', 'wpse_110037_new_posts' );
function wpse_110037_new_posts($post_id){
$WC_Product = wc_get_product( $post_id);
}
wc_get_product() will return WC_Product object and you can get the product details from it.
I Prefer to check if the status if not a draft. Also you can have the third parameter updateto check if it's an update or not
add_action( 'save_post', array($this, 'wpse1511_create_or_update_product' ), 10, 3);
function wpse1511_create_or_update_product($post_id, $post, $update){
if ($post->post_status != 'publish' || $post->post_type != 'product') {
return;
}
if (!$product = wc_get_product( $post )) {
return;
}
// Make something with $product
// You can also check $update
}
The save_post and save_post_product hooks run before the post_meta is updated and since most of the WooCommerce product data is stored as post_meta, using them might cause issues.
Thankfully, since v3, there are specific WooCommerce hooks that run after a product is updated (woocommerce_update_product) and when a product is created (woocommerce_new_product).
add_action( 'woocommerce_new_product', 'on_product_save', 10, 1 );
add_action( 'woocommerce_update_product', 'on_product_save', 10, 1 );
function on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
// do something with this product
}
This hook will run after WC has updated the product in the DB:
add_action('save_post_product', 'ns_sync_on_product_save', 10, 3);
function ns_sync_on_product_save( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
}

Resources