I have created an order using this code. An order is successfully created in the order section of the backend but the webhook is not called.
$address = array(
'first_name' => 'Fresher',
'last_name' => 'StAcK OvErFloW',
'company' => 'stackoverflow',
'email' => 'test#test.com',
'phone' => '777-777-777-777',
'address_1' => '31 Main Street',
'address_2' => '',
'city' => 'Chennai',
'state' => 'TN',
'postcode' => '12345',
'country' => 'IN'
);
$order = wc_create_order();
$order->add_product( get_product( '12' ), 2 ); //(get_product with id and next is for quantity)
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();
When I create an order normally the delivery URL is pinged successfully and i can see it in the log too.
Any idea how to get the webhook called?
You can manually trigger a hook with do_action():
do_action( 'some_wc_hook' );
Related
I am assigning a manager to an order with the code below. But I do this with manual IDs. What I want to do is "wp_dropdown_users(['role' => 'shop_manager']);" To make a selection with dropdown. How should I go about this.
woocommerce_wp_select( array(
'id' => '_store_manager_id',
'label' => 'Manager:',
'value' => $manager,
'options' => array(
'1' => 'Manager 1',
'2' => 'Manager 2',
'3' => 'Manager 3'
),
'wrapper_class' => 'form-field-wide'
) );
add_action( 'woocommerce_process_shop_order_meta', 'manager_save' );
function manager_save( $order_id ){
update_post_meta( $order_id, '_store_manager_id', wc_clean( $_POST[ '_store_manager_id' ] ) );
// wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
}
I want to attach media file after payment completion of order, for that I am using the filter "woocommerce_order_status_completed" with below snippet. But it is not attaching files to downloads tab of my account page.
add_filter( 'woocommerce_order_status_completed', 'attach_files' );
function attach_files($order_id){
$order = wc_get_order($order_id);
$md5_num = md5( 'http://localhost/wordpress/wp-content/uploads/abc.png');
$files[$md5_num] = array(
'download_url' => 'http://localhost/wordpress/wp-content/uploads/abc.png',
'download_name' => 'Download',
'downloads_remaining' => '1',
'access_expires' => NULL,
'download_id' => '7f7e4923ff94b7928c0b8b5e93fb4f89',
'product_id' => 17,
'order_id' => $order_id,
'order_key' => 'wc_order_5a382bef811e6',
'file' => array (
'name' => 'Download',
'file' => 'http://localhost/wordpress/wp-content/uploads/abc.png',
),
);
update_post_meta($order_id,'_files',$files);
}
Please help me to solve this.
I am using this function to create a new order for my customers...
public function create_order( $user_id, $address, $product_id ) {
if ( ! $address ) return false;
$address = array(
'first_name' => $address->billing_first_name,
'last_name' => $address->billing_last_name,
'email' => $address->billing_email,
'phone' => $address->billing_phone,
'address_1' => $address->billing_address_1,
'city' => $address->billing_city,
'state' => $address->billing_state,
'postcode' => $address->billing_postcode,
'country' => $address->billing_country
);
$default_args = array(
'status' => 'wc-pending',
'customer_id' => $user_id,
);
// Now we create the order
$order = wc_create_order( $default_args );
$order->add_product( get_product( $product_id ), 1 ); // This is an existing SIMPLE product
// set default address to the created order
$order->set_address( $address, 'billing' );
//calculate total order price
$order->calculate_totals();
$order->update_status( "pending", 'Order Created Successfully And Payment is not completed', TRUE );
return $order;
}
This does create a new order but it doesn't calculate shipping cost... See below screenshot
Created Order Total Table
I have flat rate shipping cost based on User Billing State and Total Item QTY.... so how do i set shipping calculation automatically like it happens on checkout..
I'm trying to create subscription order by code. I'm using the latest version of Woocommerce and Woocommerce Subscription plugins. It's working fine, but I got different order-pay page when the order contains subscription product.
When the order contains a subscription, I got all the billing and the shipping fields. If the order only contains simple products, I got a summary about the order and the payment methods. I would like the same behavior for subscriptions.
Example link:
https://test.eu/hu/cart/order-pay/4467/?pay_for_order=true&key=wc_order_161651fdsf56
I generate the link with the method get_checkout_payment_url and I got similar link in both cases, but if the order contains a subscription, I 'm redirected to the cart page immediately.
Does anybody faced with same problem?
Thank you for any help!
Best regards,
Mark
$start_date = $order_data['created_at'];
$billing_address = array(
'first_name' => $order_data['first_name'],
'last_name' => $order_data['last_name'],
'company' => $order_data['company_name'],
'email' => $order_data['email'],
'phone' => $order_data['phone_number'],
'address_1' => $order_data['street_address'],
'address_2' => $order_data['apartment_suite_unit'],
'city' => $order_data['town'],
'postcode' => $order_data['zip_code'],
'country' => $order_data['country']
);
$shipping_address = array(
'first_name' => $order_data['shipping_first_name'],
'last_name' => $order_data['shipping_last_name'],
'company' => $order_data['shipping_company_name'],
'address_1' => $order_data['shipping_street_address'],
'address_2' => $order_data['shipping_apartment_suite_unit'],
'city' => $order_data['shipping_town'],
'postcode' => $order_data['shipping_zip_code'],
'country' => $order_data['shipping_country']
);
$email = $order_data['email'];
if (!$user = get_user_by('email', $email)) $user = get_user_by('id', wc_create_new_customer($email));
$order = wc_create_order(array('customer_id' => $user->ID));
update_post_meta($order->ID, "phone_order", true);
foreach ($order_data['ordered_items'] as $item) {
$product = wce_get_product($item, $currency);
$order->add_product( $product, $item['quantity']);
}
$order->set_address( $billing_address, 'billing' );
$order->set_address( $shipping_address, 'shipping' );
$order->set_currency( $currency );
$order->set_payment_method( $order_data['payment_method'] );
$order->calculate_totals();
$order->calculate_taxes();
// Előfizetés
foreach ($order_data['ordered_items'] as $item) {
if (WC_Subscriptions_Product::is_subscription($item['id'])) {
$product = wce_get_product($item, $currency);
$sub = wcs_create_subscription(array('order_id' => $order->id, 'billing_period' => $period, 'billing_interval' => $interval, 'start_date' => $start_date));
$sub->add_product( $product, $item['quantity']/*, $args*/);
$sub->set_address( $billing_address, 'billing' );
$sub->set_address( $shipping_address, 'shipping' );
$sub->calculate_totals();
}
}
$payment_link = $order->get_checkout_payment_url();
return $payment_link;
}
I have developed a small wordpress application where It has Institutes(wp user), Trainers(wp user), Trainees(wp user), courses(custom post) and notifications(custom post). All the application is working fine but If I delete an institute all the information belongs to that institute like Trainers, Trainee, notifications & courses should also be deleted. While I was creating the 'institute' user I am storing the all the related info like first name, last name in wordpress table 'wp_users' and 'institute name' is storing as 'user meta' with key 'inistitute_name' in 'wp_usermeta' table like below is my code:
$user_data = array(
'ID' => '',
'user_pass' => '',
'user_login' => $first_name,
'user_email' => $user_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'admin'//get_option('default_option')
);
$random_password = wp_generate_password(8,false);
$user_id = wp_insert_user( $user_data );
update_user_meta( $user_id, 'inistitute_name',$insititute_name );
While creating Trainer (or) Trainee my code is like below:
$user_data = array(
'ID' => '',
'user_pass' => '',
'user_login' => $first_name,
'user_email' => $trainer_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'trainer'
);
$random_password = wp_generate_password(8,false);
$user_id = wp_insert_user( $user_data );
update_user_meta( $user_id, 'inistitute_name',$this->institute_name[0] );
wp_set_password($random_password, $user_id);
While creating courses my code is like below:
$user_ID = get_current_user_id();
$institute_name = get_user_meta($user_ID, 'inistitute_name', true);
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => "courses"
);
$id = wp_insert_post($post);
update_post_meta( $id, 'inistitute_name', $institute_name );
Suppose let us say if a institute 'abcd' is deleted then all the information like 'trainer', 'trainee', 'courses' & 'notifications' associated with that institute should also be deleted based on a 'user_meta' field. Is it possible to delete? (or) did I do any mistake? can anyone tell me what was I doing wrong?
Try this code:
$user_args = array(
'meta_key' => 'inistitute_name',
'meta_value' => 'your_value', //<-- replace it by your user_meta value.
'meta_compare' => '=',
'orderby' => 'ID',
'number' => -1
);
// Custom query.
$user_query = new WP_User_Query($user_args);
// Get query results.
$users = $user_query->get_results();
//print_r($users);
// Check for users
if (!empty($users))
{
// Loop over users.
foreach ($users as $user)
{
$user_id = $user->ID;
// Get each user's data.
$user_info = get_userdata($user->ID);
$user_display_name = $user_info->display_name;
$args = array(
'numberposts' => -1,
'post_type' => 'any', //<-- you can Specific type by array('post', 'my_custom_post')
'author' => $user_id
);
// get all posts by this user: posts, pages, attachments, etc..
$user_posts = get_posts($args);
if (!empty($user_posts))
{
// delete all the user posts
foreach ($user_posts as $user_post)
{
wp_delete_post($user_post->ID, true);
}
}
wp_delete_user( $user_id, $reassign );
}
}
else
{
//"no user found"
}
I haven't tested this code, out form my WP knowledge.
Please note before testing this code do take a Database backup.
I got the solution form wordpress.stackexchange.com link to that question is here
https://wordpress.stackexchange.com/questions/249435/how-to-delete-all-users-and-posts-based-on-user-meta/