I found the below script somewhere on here before and having trouble getting it to work. Basically I want to copy basic order details into a new table (which is in the same database), so I can run some reports off of it from non wordpress page. I have changed the table name and fields and copied this into my functions.php in the theme. But it doesnt seem to do anything. Any help would be great, im really stuck. I'm running woocommerce 2.2.8 if that makes any difference.
function add_neworders ($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$total = $order->order_total;
$date = $order->order_date;
global $wpdb;
$table = $wpdb->prefix . 'finacedata';
$wpdb->insert($table, array(
'desctipion' => $name,
'status' => '2',
'date' => $date,
'amount' => $total,
'invoice' => $order_id,
));
add_action( 'woocommerce_new_order', 'add_neworders' );}
This is probably a late answer, but the woocommerce_new_order action runs when the order is created, but the data is not populated just yet, so everything is empty.
Use the woocommerce_checkout_order_processed action instead, which runs when the order is created and all of the order data(like line items) are stored.
you can't add add_action into function. put outside the function:
function add_neworders ($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$total = $order->order_total;
$date = $order->order_date;
global $wpdb;
$table = $wpdb->prefix . 'finacedata';
$wpdb->insert($table, array(
'desctipion' => $name,
'status' => '2',
'date' => $date,
'amount' => $total,
'invoice' => $order_id,
));
}
add_action( 'woocommerce_new_order', 'add_neworders' );
Related
I am trying to get all orders from woocommerce. Following the instruction on https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
I put the following code in my function.php
// Get latest 3 orders.
$args = array(
'limit' => 3,
);
$orders = wc_get_orders( $args );
var_dump($orders);
However, it outputs an empty array.
I checked my code and found I actually used wc_get_orders in a hook as below
add_action( 'woocommerce_order_status_changed', 'change_role_on_first_purchase',10,4 );
function change_role_on_first_purchase( $order_id,$old_status, $new_status, $order ) {
$userID = $order->user_id;
$user = new WP_User( $userID );
if ( in_array('subscriber',$user->roles) ){
$args = array(
'customer' => $userID,
'exclude' => array( $order->get_id() ),
'status' => array('completed')
);
$orders = wc_get_orders($args);
if (!$orders && $new_status == "completed"){
$user->set_role('customer');
}
}
}
This is used to change the user's role from subscriber to customer after he places the first order. This function works on my site. So wc_get_orders works here. Why then it is not working in my function.php?
It outputs an empty array because Woocommerce has not yet registered order types/statuses when you're calling wc_get_orders()
Try adding it after init event:
add_action( 'init', 'test_init' );
function test_init() {
// Get latest 3 orders.
$args = array(
'limit' => 3,
);
$orders = wc_get_orders( $args );
var_dump($orders);
}
https://developer.wordpress.org/reference/hooks/init/
To begin with I have created a simple plugin that contains this piece of code:
function woo_create_credit_attribute_taxonomy() {
$attributes = wc_get_attribute_taxonomies();
$slugs = wp_list_pluck( $attributes, 'remaining_credits' );
if ( ! in_array( 'remaining_creds', $slugs ) ) {
$args = array(
'slug' => 'remaining_creds',
'name' => __( 'Remaining Credits', 'bidrop-credits' ),
'type' => 'select',
'orderby' => 'menu_order',
'has_archives' => false,
);
$result = wc_create_attribute( $args );
}
}
// On Activation create the credit attribute taxonomy
add_action( 'admin_init', 'woo_create_credit_attribute_taxonomy' );
This piece of code, simply creates a new Woocommerce Product Attribute.
And in the uninstall.php file of the same plugin, I use the WPDB query below to remove it uppon uninstall.
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE 1");
Notes: The Attribute is succesfully being deleted from the Database, but remains visible on front-end and when I try deleting it from the front-end it seems to be stuck there, Until I deactive and Reactivate Woocommerce.
Any ways I could adjust this code to flush_rewrite the woocommerces rules?
I have already tried the cassual flush_rewrite_rules() built-in function from wordpress. But it has no better results.
Thanks in advance for your time and effort
The woe_order_export_started filter hook is related to a plugin
This code is used for exporting orders with total > 10
add_filter( 'woe_order_export_started', function ( $order_id ) {
$order = new WC_Order($order_id);
return ($order->get_total() > 10.00) ? $order_id: false;
});
I am trying to create a php code to find out whether the order has order note or not, and to ignore the orders which has no order note.
I found out the code to get the order notes
$args = array(
'post_id' => $order->id,
'approve' => 'approve',
'type' => 'order_note',
'search' => 'Order status changed from Pending Payment to Processing.',
);
// woocommerce hides such records by default
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10 );
$notes = get_comments( $args );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
How to I implement this with the woe_order_export_started filter hook to determine if the order has order note or not.
Basically I want to avoid exporting the orders which has no order note. So it should return false if there is no order note.
The following answer returns false if there are no order notes, while using the woo_order_export_started filter hook
function filter_woe_order_export_started( $order_id ) {
// Get order notes
$notes = wc_get_order_notes( array(
'order_id' => $order_id,
'order_by' => 'date_created',
'order' => 'ASC',
));
// Notes is empty
if ( empty( $notes ) ) {
return false;
}
return $order_id;
}
add_filter( 'woe_order_export_started', 'filter_woe_order_export_started', 10, 1 );
WooCommerce 3.0 broke my app and I cannot figure out how to fix it now.
I have an action for when a subscription is added/changed running here:
Inside the function I was getting the order details and finding the line item for a variable subscription to update my custom DB with the option as well as getting custom order meta that I added via woocommerce_form_field:
This no longer works and everything appears protected? How can I update this to work with 3.0?
add_action( 'woocommerce_subscription_status_changed', 'update_subscription', 10, 3 );
function update_subscription( $id, $old_status, $new_status ) {
$sitelink_db = new SSLA_DB_Sitelink();
$order = new WC_Order( $id );
$items = $order->get_items();
$subscription_type = '';
$user_id = $order->get_user_id();
$sitelink_domain = get_post_meta( $order->id, 'ssla_sitelink_url', true );
foreach ($items as $item) {
if( "SiteLink Subscription" === $item['name'] ) {
$subscription_type = $item['brand'];
}
}
$customer_data = array(
'user_id' => $user_id,
'subscription_type' => $subscription_type,
'domain_referrer' => $sitelink_domain,
'active_subscription' => $new_status,
'date_modified' => date( 'Y-m-d H:i:s' ),
);
$sitelink_db->add( $customer_data );
}
Basically I need to get that variation name of the subscription to store in my DB, as well as that custom meta field I made. Which does not work anymore either
Here's my best guess. It's impossible to test since I don't have the same setup as you.
Few notes:
The $subscription object is passed to the woocommerce_subscription_status_changed hook so let's use it.
$order->id should be replaced by $order->get_id() in WC3.0, but we're going to use the the $subscription object (the subscription order class extends the order class so it's similar).
getters must be used on the WC_Order_Item_Product object that is returned when looping through get_items() so $item['name'] becomes $item->get_name()
Here's the full code block:
add_action( 'woocommerce_subscription_status_changed', 'update_subscription', 10, 4 );
function update_subscription( $subscription_id, $old_status, $new_status, $subscription ) {
$match_this_id = 99; // Change this to the product ID of your special subscription
$sitelink_db = new SSLA_DB_Sitelink();
$items = $subscription->get_items();
$subscription_type = '';
$user_id = $subscription->get_user_id();
$sitelink_domain = $subscription->get_meta( 'ssla_sitelink_url' );
foreach ($items as $item) {
if( $match_this_id === $item->get_product_id() ) {
$product = $item->get_product();
if( $product->is_type( 'variation' ) ){
$subscription_type = $product->get_attribute( 'brand' );
}
}
}
$customer_data = array(
'user_id' => $user_id,
'subscription_type' => $subscription_type,
'domain_referrer' => $sitelink_domain,
'active_subscription' => $new_status,
'date_modified' => date( 'Y-m-d H:i:s' ),
);
$sitelink_db->add( $customer_data );
}
Im building a plugin for wordpress. I want to creat a row in a custom database table i have created called jp_test. I need the wordpress user ID in my custom table. My code do far is in the plugin index file but I have tried it in the theme functions.php too and it still doesnt work. can anyone suggest anything?
function add_to_jp( $user_id ) {
global $wpdb;
$jp_data = array(
'wp_user_ID' => $user_id,
'jp_email' => '',
'jp_telephone' => '',
'extra' => ''
);
$wpdb->insert( 'jp_test', $jp_data);
}
add_action( 'user_register', 'add_to_jp)');
can you try this ?
function add_to_jp( $user_id ) {
$jp_data = array(
'wp_user_ID' => $user_id,
'jp_email' => '',
'jp_telephone' => '',
'extra' => ''
);
global $wpdb;
$wpdb->insert( 'jp_test', $jp_data, array('%d', '%s', '%s', '%s') );
}
add_action( 'user_register', 'add_to_jp');
Hope this work :/