How can I get, on Woocommerce, the date an order had its status changed to paid/complete?
I saw something about getting the orders from a costumer, but this would be just the first step of my algorithm. Then I would need to know when it changed to complete.
The idea is to make a membership area: a payment lasts 3 months. So I will count the days passed since it was bought
Something related
https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/
And this is what I use to know if a product was bought by the costumer
if (wc_customer_bought_product($customer_email, $user_id,$loop->post->ID)){
$courses[] = $this->find($loop->post->ID);
}
I think you should look into:
/mySite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php
There is a __get function:
public function __get( $key ) {
// Get values or default if not set.
if ( 'completed_date' === $key ) {
$value = ( $value = get_post_meta( $this->id, '_completed_date', true ) ) ? $value : $this->modified_date;
} elseif ( 'user_id' === $key ) {
$value = ( $value = get_post_meta( $this->id, '_customer_user', true ) ) ? absint( $value ) : '';
} elseif ( 'status' === $key ) {
$value = $this->get_status();
} else {
$value = get_post_meta( $this->id, '_' . $key, true );
}
return $value;
}
So my understanding is that if you pass 'completed date' as the argument then it will return the completed_date.
It also gives you a hint where this date is i.e.
get_post_meta
Well, at least that's where I would start.
Probably the "most correct" way is:
$order = new WC_Order($order_id);
$date_obj = $order->get_date_paid();
echo $date_obj->date('d/m/Y');
This should to the job on recente WP/WooCommerce.
$order = new WC_Order($sale_id);
echo $order->get_date_paid();
Related
I want to exclude WooCommerce revenue from the Google Analytics purchase event when the Product ID is 224112 or 159324.
I tried to find solutions to exclude sending the data to GA4 through GMT exclusion triggers or filters directly in the GA4 interface, but it seems the best way to accomplish this is to exclude the data from arriving into the datalayer in the first place.
I use the GTM4WP plugin to send the website data (WordPress, WooCommerce) to the GTM datalayer.
In the plugin, I found the part of the code that sends the purchase data to the datalayer. I'm not very familiar with this code and I dont want to mess things up on my site. Since the Google Analytics give the results only after 24 hours I want to make sure that I do the right thing right away.
This is the original plugin code:
function gtm4wp_get_purchase_datalayer( $order, $order_items ) {
global $gtm4wp_options, $gtm4wp_is_woocommerce3_7;
$dataLayer = array();
if ( $order instanceof WC_Order ) {
$woo = WC();
// variable for Google Smart Shopping campaign new customer reporting
// https://support.google.com/google-ads/answer/9917012?hl=en-AU#zippy=%2Cinstall-with-google-tag-manager
if ( $woo->customer instanceof WC_Customer ) {
// we need to use this instead of $woo->customer as this will load proper total order number and value from the database instead of the session
$woo_customer = new WC_Customer( $woo->customer->get_id() );
$dataLayer['new_customer'] = $woo_customer->get_order_count() === 1;
}
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCEXCLUDETAX ] ) {
$order_revenue = (float)( $order->get_total() - $order->get_total_tax() );
} else {
$order_revenue = (float) $order->get_total();
}
$order_shipping_cost = (float) $order->get_shipping_total();
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCEXCLUDESHIPPING ] ) {
$order_revenue -= $order_shipping_cost;
}
$order_currency = $order->get_currency();
if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) {
$dataLayer['event'] = 'gtm4wp.orderCompleted';
$dataLayer['transactionId'] = $order->get_order_number();
$dataLayer['transactionAffiliation'] = '';
$dataLayer['transactionTotal'] = $order_revenue;
$dataLayer['transactionShipping'] = $order_shipping_cost;
$dataLayer['transactionTax'] = (float) $order->get_total_tax();
$dataLayer['transactionCurrency'] = $order_currency;
}
if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) {
$dataLayer['event'] = 'gtm4wp.orderCompletedEEC';
$dataLayer['ecommerce'] = array(
'currencyCode' => $order_currency,
'purchase' => array(
'actionField' => array(
'id' => $order->get_order_number(),
'affiliation' => '',
'revenue' => $order_revenue,
'tax' => (float) $order->get_total_tax(),
'shipping' => (float)( $order->get_shipping_total() ),
'coupon' => implode( ', ', ( $gtm4wp_is_woocommerce3_7 ? $order->get_coupon_codes() : $order->get_used_coupons() ) ),
)
)
);
}
if ( isset( $order_items ) ) {
$_order_items = $order_items;
} else {
$_order_items = gtm4wp_process_order_items( $order );
}
if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) {
$dataLayer['transactionProducts'] = $_order_items['products'];
}
if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) {
$dataLayer['ecommerce']['purchase']['products'] = $_order_items['products'];
}
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) {
$dataLayer['ecomm_prodid'] = $_order_items['product_ids'];
$dataLayer['ecomm_pagetype'] = 'purchase';
$dataLayer['ecomm_totalvalue'] = (float) $_order_items['sumprice'];
}
}
return $dataLayer;
}
I think I could add an if statement just before the excludeTAX option. It would be something like this:
If ($productID = array (224112, 159324)) {
$order_revenue = 0;
} else {
$order_revenue = (float) $order->get_total();
}
I don't know what would be the correct variable for $productID and I am not sure that this would work.
Any help is appreciated.
$order_product_ids = array();
foreach ($order->get_items() as $item_key => $item) {
$product = $item->get_product();
$order_product_ids[] = $product->get_id();
}
$checklist_product_ids = array(224112, 159324);
$is_product_in_order = (count(array_intersect($order_product_ids, $checklist_product_ids))) ? true : false;
If ($is_product_in_order) {
$order_revenue = 0;
} else {
$order_revenue = (float) $order->get_total();
}
I'm currently trying to modify the name column in the WordPress admin dashboard. I've tried this code here but it's not working:
add_action('manage_users_custom_column', 'modify_users_column_content', 10, 3 );
function modify_users_column_content( $value, $column_name, $user_id ) {
if ( $column_name === 'name' ) {
$value .= '<span> |</span>';
}
return $value;
}
When I error_log the column_name parameter, I get only the last two columns from the user management plugin UltimateMember:
The first columns are not within the array. I've tried to understand it but no chance. I don't get it.
The first columns are not within the array. I've tried to understand
it but no chance. I don't get it.
Because the manage_users_custom_column filter is meant to be used to generate the output of a custom column and not default columns like the "Name" column.
However, you can achieve what you want by replacing the default "Name" column (keyed name) with a custom one like so:
add_filter( 'manage_users_columns', function( $columns ){
$columns2 = [];
// We could do $columns['name2'] = 'Name'; - but we are replacing a column.
foreach ( $columns as $key => $label ) {
if ( 'name' === $key )
$columns2['name2'] = 'Name';
else
$columns2[ $key ] = $label;
}
return $columns2;
} );
And then use the manage_users_custom_column filter to generate the output which is displayed in the custom column (name2):
add_filter( 'manage_users_custom_column', function( $output, $column_name, $user_id ){
if ( 'name2' === $column_name ) {
$user_object = get_userdata( $user_id );
$name = trim( $user_object->first_name . ' ' . $user_object->last_name );
$output = $name ? $name . '<span> |</span>' : '—'; // the custom output
}
return $output;
}, 10, 3 );
I'm trying to add meta data to each product when an order has been created by using the woocommerce_checkout_create_order_line_item.
However, I can't seem to access the ID of the order.
I've used print_r($order) and can see the order details in there but I can't see the ID of the order within the object. Is this because it hasn't been generated yet?
add_action('woocommerce_checkout_create_order_line_item', array($this, 'ticket_meta_to_line_item'), 20, 4 );
function ticket_meta_to_line_item( $item, $cart_item_key, $values, $order )
{
$_p = $item->get_product();
$key = 'Draw #';
$order_id = $order->id;
error_log( print_r( $order, true ) );
if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
{
$numbers = $this->add_tickets_to_order_meta($order_id, $order->get_user_id(), $_p->id);
error_log( print_r( $numbers, true ) );
$item->add_meta_data( $key , 1 , true );
}
}
If you wondering to add meta data, then there is no need to find the Order_ID, from below code you can easily do so.
function _woocommerce_add_order_item_meta_new_ver($item,$cart_key,$values) {
//HERE product_meta is just a random key I have used here, you have to use your key here
if (isset ( $values ['product_meta'] )) {
foreach ( $values ['product_meta'] as $key => $val ) {
$order_val = stripslashes( $val );
if($val) {
if($key == 'your_cart_item_key') {
$item->add_meta_data('Your Key',$order_val);
}
}
}
}
}
//This will add "Your Key" in your order_item_meta, just make sure you have used the same key "your_cart_item_key" in your cart_item_meta key too.
You can access order id using below code.
$order_id = $order->get_order_number();
Tested and works well
I have a problem with the following code when I make the price variable the function doesn't work but when I hardcode a price it works. (note I am in a testing face and haven't worried about security)
this, down't work:
function add_custom_price( $cart_obj ) {
global $product, $woocommerce ,$wpdb;
if ( isset( $_POST['fra'] ) ){
$GLOBALS['$_fra'] = urldecode( $_POST["fra"] );
} else {
$GLOBALS['$_fra'] = "";
}
if ( isset( $_POST['til'] ) ){
$GLOBALS['$_til'] = urldecode($_POST["til"]);
} else {
$GLOBALS['$_til'] = "";
}
if ( $GLOBALS['$_til'] !="" && $GLOBALS['$_fra'] !="" ){
$t1 = urldecode( $GLOBALS['$_til'] );
$t2 = urldecode( $GLOBALS['$_fra']);
$objArray = $wpdb->get_results("SELECT $t1 FROM test_priser WHERE city = '$t2'");
if ( isset($objArray[0]->$t1) ){
$priser = explode("/",$objArray[0]->$t1);
if ( !isset($priser[1]) ){
$priser[1] = intval($priser[0]);
}
}
echo "priser[1] = ".$priser[1]; //this outputs the expected value
}
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value['data']->set_price( $priser[1] );
}
}
When I add this line, and override the variable, it works:
$priser[1] = 1000;
I am quite confused as to what the problem might be.
for anyone else who might stumble upon this. Seems I need to pass the variable price in the session then it works.
you can find the answer here: custom price on woocommerce product
I would like to add a dynamic value to a custom field in wordpress.
I do not want to use update_post_meta() because it is a dynamic value that I want to add every time when the post is loaded ..
The custom field is tm_video_file, and retrieved with get_post_meta().
This will return a URL, I beed to append a query string to it.
I am trying to hook a function that will change the value of "tm_video_file", but without success.
// URL will be called as:
get_post_meta($post_id, 'tm_video_file', true);
// I try to add a filter for these meta data
add_filter('get_post_metadata', array($this,'add_hash_key'),1,4);
// And try to append the tm_video_file value here:
public function add_hash_key ($meta_value, $post_id, $meta_key, $single ) {
if($meta_key == 'tm_video_file') {
var_dump($meta_value); // ALWAYS NULL ???
var_dump($post_id);
var_dump($meta_key);
var_dump($single);
}
return $meta_value.'?hash=something-dynamic-here';
}
UPDATE:
I have found out some information on the net, and I think I am a bit closer to the solution:
public static function add_hash_key($meta_value, $object_id, $meta_key, $single) {
if ($meta_key == 'tm_video_file') {
$meta_type = 'post';
$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
if ( !$meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[$object_id];
}
if ( isset($meta_cache[$meta_key]) ) {
if ( $single ) {
$meta_value = maybe_unserialize( $meta_cache[$meta_key][0] );
} else {
$meta_value = array_map('maybe_unserialize', $meta_cache[$meta_key]);
}
}
// At this point $meta_value contains the right data!
$meta_value .= '?Hash-Here';
return array($meta_value); //
}
}
In wordpress file meta.php there is a function called:
get_metadata()
This will apply filters, and returns the value in the end:
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
if ( null !== $check ) {
if ( $single && is_array( $check ) )
return $check[0];
else
return $check;
}
//For some reason $check is ALWAYS null .. ? But the filter function did run for sure..