Change order status for backordered items in Woocommerce - php
I am configuring a webshop using WordPress and WooCommerce. I have a plugin installed called Woo Multi Order Creator. What is does is that whenever an order of more than 1 item is made, it divides the main order into multiple orders, one order per purchased item.
Now, what I want to do is to make it change the order status of each order, if the item in each order is containing meta key with the value "Backordered".
For example if product A and product B are purchased. Product A is in backorder, and therefore has
{$wpdb->prefix}woocommerce_order_itemmeta.meta_key = 'Backordered'
and product B would be in stock, and it would not have the metakey for backorder.
Now, the thing is that the plugin divides this order into two orders, one per purchased item. What I want it to do further, is to see if the items containted in each new order has the metakey for backorder, and if it has then change the order status to 'waiting', for every order.
Plugin code:
<?php /*
Plugin Name: Woo Product Wise Multi Order Creator
Description: Create multiple orders in woocommerce product wise.
Plugin URI: http://www.thewpexperts.co.uk
Version: 2.0.0
Author: TheWPexperts
Author URI: http://www.thewpexperts.com/
*/
/*
Copyright 2017 The WP Experts
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
define( 'WOO_MULTI_ORDER_URL', plugins_url('/', __FILE__) );
define( 'WOO_MULTI_ORDER_DIR', dirname(__FILE__) );
if ( ! class_exists( 'WC_CPInstallCheck' ) ) {
class WC_CPInstallCheck {
static function install() {
if ( !is_plugin_active('woocommerce/woocommerce.php')){
deactivate_plugins(__FILE__);
$error_message = __('This plugin requires WooCommerce plugins to be active!', 'woocommerce');
die($error_message);
}
}
}
}
register_activation_hook( __FILE__, array('WC_CPInstallCheck', 'install') );
add_action('init','woo_multi_order_load_function');
function woo_multi_order_load_function() {
$enableMultiOrder = get_option('enableMultiOrder');
$customOrderPermit = get_option('customOrderPermit');
add_action('admin_menu', 'register_multi_order_setting_submenu',99);
if($enableMultiOrder == 1){
add_action('woocommerce_multiorder_checkout_before_order_info', 'destinationStep');
add_action( 'woocommerce_thankyou', 'multi_order_generator', 10, 1 );
if($customOrderPermit ==1){
add_action( 'woocommerce_after_order_notes', 'woo_order_dates_checkout_field' );
add_action( 'wp_head', 'date_picker_scripts' );
add_action( 'woocommerce_checkout_update_order_meta', 'checkout_update_delivery_meta' );
add_filter("manage_edit-shop_order_columns", "woo_order_extra_columns");
add_action("manage_posts_custom_column", "woo_order_extra_columns_content");
add_filter( 'manage_edit-shop_order_sortable_columns', 'my_sortable_cake_column' );
}
}
}
function your_plugin_settings_link($links) {
$settings_link = 'Settings';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'your_plugin_settings_link' );
function register_multi_order_setting_submenu() {
add_submenu_page( 'woocommerce', 'Multi-order Setting', 'Multi-order Setting', 'manage_options', 'multi-order-setting', 'multi_order_setting' );
}
function multi_order_setting(){
include(WOO_MULTI_ORDER_DIR.'/admin/settings.php');
}
function date_picker_scripts() {
$customOrderPermit = get_option('customOrderPermit');
if(is_page('checkout')){
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );
wp_register_style('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
wp_enqueue_style( 'jquery-ui' );
}
}
function checkout_update_delivery_meta( $order_id ) {
$dateArray = array();
$customOrderPermit = get_option('customOrderPermit');
$dayDifference = get_option('dayDifference');
$dt = date('d/m/y', strtotime("+".$dayDifference." days"));
if ( count($_POST['customDatePicker'])>0) {
foreach($_POST['customDatePicker'] as $customDatePicker){
if(!empty($customDatePicker)){
$dateArray[] = $customDatePicker;
}else{
$dateArray[] = $dt;
}
}
}
$dateValue = join( ', ', $dateArray );
update_post_meta( $order_id, 'Delivery_Dates', $dateValue);
}
function woo_order_dates_checkout_field( $checkout ) {
global $woocommerce,$orderRate;
$customOrderPermit = get_option('customOrderPermit');
$dayDifference = get_option('dayDifference');
$dt = date('Y-m-d', strtotime("+".$dayDifference." days"));
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$price = get_post_meta($values['product_id'] , '_price', true);
echo "<b>".$_product->post_title."</b> ";
echo '<input type="text" class="deliveryDate" id="datepicker'.$_product->ID.'" name="customDatePicker[]" placeholder="Choose dates">'; ?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#datepicker<?php echo $_product->ID;?>").datepicker({
dateFormat: 'dd/mm/yy',
minDate: new Date('<?php echo $dt; ?>'),
});
});
</script>
<?php
}
}
function multi_order_generator($order_id) {
$customOrderPermit = get_option('customOrderPermit');
$order = new WC_Order( $order_id );
$items = $order->get_items();
$count =0;
$productID = array();
$productQty = array();
$deliveryDate = get_post_meta( $order_id, 'Delivery_Dates',true);
$dateArray = explode(',',$deliveryDate);
foreach($items as $order_item_id => $item){
if($count >0){
$productName = get_the_title($item['product_id']);
$orderValueID = createNewOrderAndRecordPayment($order_id);
addProductWithOrder($orderValueID,$item['product_id'],$item['qty']);
if($customOrderPermit == 1){
update_post_meta($orderValueID,'_Delivery_Date',$dateArray[$count]);
}
wc_delete_order_item($order_item_id);
}else{
if($customOrderPermit == 1){
update_post_meta($order_id,'_Delivery_Date',$dateArray[0]);
}
}
$count++;
}
}
function addProductWithOrder($orderID,$productID,$productQty){
$ProOrderItem = new WC_Order($orderID);
$ProOrderItem->add_product(get_product($productID),$productQty);
}
function createNewOrderAndRecordPayment($orderID) {
global $wpdb;
global $woocommerce;
$original_order = new WC_Order($orderID);
$currentUser = wp_get_current_user();
//1 Create Order
$order_data = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'ping_status' => 'closed',
'post_author' => $currentUser->ID,
'post_excerpt' => $original_order->customer_message,
'post_password' => uniqid( 'order_' ) // Protects the post just in case
);
$order_id = wp_insert_post( $order_data, true );
if ( is_wp_error( $order_id ) ){
$msg = "Unable to create order:" . $order_id->get_error_message();;
throw new Exception( $msg );
} else {
$order = new WC_Order($order_id);
//2 Update Order Header
update_post_meta( $order_id, '_order_shipping', get_post_meta($orderID, '_order_shipping', true) );
update_post_meta( $order_id, '_order_discount', get_post_meta($orderID, '_order_discount', true) );
update_post_meta( $order_id, '_cart_discount', get_post_meta($orderID, '_cart_discount', true) );
update_post_meta( $order_id, '_order_tax', get_post_meta($orderID, '_order_tax', true) );
update_post_meta( $order_id, '_order_shipping_tax', get_post_meta($orderID, '_order_shipping_tax', true) );
update_post_meta( $order_id, '_order_total', get_post_meta($orderID, '_order_total', true) );
update_post_meta( $order_id, '_order_key', 'wc_' . apply_filters('woocommerce_generate_order_key', uniqid('order_') ) );
update_post_meta( $order_id, '_customer_user', get_post_meta($orderID, '_customer_user', true) );
update_post_meta( $order_id, '_order_currency', get_post_meta($orderID, '_order_currency', true) );
update_post_meta( $order_id, '_prices_include_tax', get_post_meta($orderID, '_prices_include_tax', true) );
update_post_meta( $order_id, '_customer_ip_address', get_post_meta($orderID, '_customer_ip_address', true) );
update_post_meta( $order_id, '_customer_user_agent', get_post_meta($orderID, '_customer_user_agent', true) );
//3 Add Billing Fields
update_post_meta( $order_id, '_billing_city', get_post_meta($orderID, '_billing_city', true));
update_post_meta( $order_id, '_billing_state', get_post_meta($orderID, '_billing_state', true));
update_post_meta( $order_id, '_billing_postcode', get_post_meta($orderID, '_billing_postcode', true));
update_post_meta( $order_id, '_billing_email', get_post_meta($orderID, '_billing_email', true));
update_post_meta( $order_id, '_billing_phone', get_post_meta($orderID, '_billing_phone', true));
update_post_meta( $order_id, '_billing_address_1', get_post_meta($orderID, '_billing_address_1', true));
update_post_meta( $order_id, '_billing_address_2', get_post_meta($orderID, '_billing_address_2', true));
update_post_meta( $order_id, '_billing_country', get_post_meta($orderID, '_billing_country', true));
update_post_meta( $order_id, '_billing_first_name', get_post_meta($orderID, '_billing_first_name', true));
update_post_meta( $order_id, '_billing_last_name', get_post_meta($orderID, '_billing_last_name', true));
update_post_meta( $order_id, '_billing_company', get_post_meta($orderID, '_billing_company', true));
//4 Add Shipping Fields
update_post_meta( $order_id, '_shipping_country', get_post_meta($orderID, '_shipping_country', true));
update_post_meta( $order_id, '_shipping_first_name', get_post_meta($orderID, '_shipping_first_name', true));
update_post_meta( $order_id, '_shipping_last_name', get_post_meta($orderID, '_shipping_last_name', true));
update_post_meta( $order_id, '_shipping_company', get_post_meta($orderID, '_shipping_company', true));
update_post_meta( $order_id, '_shipping_address_1', get_post_meta($orderID, '_shipping_address_1', true));
update_post_meta( $order_id, '_shipping_address_2', get_post_meta($orderID, '_shipping_address_2', true));
update_post_meta( $order_id, '_shipping_city', get_post_meta($orderID, '_shipping_city', true));
update_post_meta( $order_id, '_shipping_state', get_post_meta($orderID, '_shipping_state', true));
update_post_meta( $order_id, '_shipping_postcode', get_post_meta($orderID, '_shipping_postcode', true));
//6 Copy shipping items and shipping item meta from original order
$original_order_shipping_items = $original_order->get_items('shipping');
foreach ( $original_order_shipping_items as $original_order_shipping_item ) {
$item_id = wc_add_order_item( $order_id, array(
'order_item_name' => $original_order_shipping_item['name'],
'order_item_type' => 'shipping'
) );
if ( $item_id ) {
wc_add_order_item_meta( $item_id, 'method_id', $original_order_shipping_item['method_id'] );
wc_add_order_item_meta( $item_id, 'cost', wc_format_decimal( $original_order_shipping_item['cost'] ) );
}
}
// Store coupons
$original_order_coupons = $original_order->get_items('coupon');
foreach ( $original_order_coupons as $original_order_coupon ) {
$item_id = wc_add_order_item( $order_id, array(
'order_item_name' => $original_order_coupon['name'],
'order_item_type' => 'coupon'
) );
// Add line item meta
if ( $item_id ) {
wc_add_order_item_meta( $item_id, 'discount_amount', $original_order_coupon['discount_amount'] );
}
}
//Payment Info
update_post_meta( $order_id, '_payment_method', get_post_meta($orderID, '_payment_method', true) );
update_post_meta( $order_id, '_payment_method_title', get_post_meta($orderID, '_payment_method_title', true) );
update_post_meta( $order->id, 'Transaction ID', get_post_meta($orderID, 'Transaction ID', true) );
$order->payment_complete();
//6 Set Order Status to processing to trigger initial emails to end user and vendor
$order->update_status('processing');
}
return $order_id;
}
function woo_order_extra_columns($columns)
{
$newcolumns = array(
"cb" => "<input type = \"checkbox\" />",
"delivery" => esc_html__('Delivery', 'woocommerce'),
);
$columns = array_merge($newcolumns, $columns);
return $columns;
}
function woo_order_extra_columns_content($column)
{
global $post;
$order_id = $post->ID;
switch ($column)
{
case "delivery":
$daliveryDate = get_post_meta($order_id,'_Delivery_Date',true);
if ( empty( $daliveryDate ) )
echo __( '-' );
else
printf( __( '%s' ), $daliveryDate );
break;
}
}
function my_sortable_cake_column( $columns ) {
$columns['delivery'] = 'delivery';
return $columns;
}
It changes the order status to processing at line 253.
I'm guessing that it is around here that a bit of code should be added to make it change order status to waiting instead, if the item in the order has the metakey for backorder.
I hope that someone with more expertise than my very limited knowlegde can come to my assistance.
Here is a hooked function that is triggered when orders get a "processing" status.
Inside the function we look for order items that are "Backordered" with a simple and light SQL query from the order ID.
If a "Backordered" item is found, we change the order status to "on-hold" (the correct order status for your case).
The code:
add_action('woocommerce_order_status_processing', 'pending_back_orders_to_on_hold', 60, 2 );
function pending_back_orders_to_on_hold( $order_id, $order ) {
global $wpdb;
// The SQL query for Order items in backorder
$results = $wpdb->get_col( "
SELECT woim.meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woim.order_item_id = woi.order_item_id
WHERE woi.order_id = '$order_id' AND woim.meta_key = 'Backordered'
" );
// If an item is 'Backordered' we change the order status to "on-hold"
if(reset($results) > 0)
$order->update_status('on-hold');
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
Update (related to your comment)
If you want to change the code inside the plugin for that purpose, you will replace line 253:
$order->update_status('processing');
By the following code:
$line_item_ids = array_keys( $order->get_items() );
$backordered = wc_get_order_item_meta( reset($line_item_ids), 'Backordered', true );
$changed_status = $backorders > 0 ? 'on-hold' : 'processing';
$order->update_status( $changed_status );
It should work, but this is not a good practice.
Related
WOOCOMMERCE call function from URL
I have a php function that creates a new session (WC()->session->set). I want to make a link on site's header that calls a function to remove that session ( WC()->session->set( 'the_session', null ); how it should be done? couldn't find a way :( tried to use $_get on functions, which didn't work EDIT: here is the code Im using, it add's the option to edit orders that have been made already: /** * #snippet Edit Order Functionality # WooCommerce My Account Page * #how-to Get CustomizeWoo.com FREE * #sourcecode https://businessbloomer.com/?p=91893 * #author Rodolfo Melogli * #compatible WooCommerce 4.1 * #donate $9 https://businessbloomer.com/bloomer-armada/ */ // ---------------- // 1. Allow Order Again for Processing Status add_filter( 'woocommerce_valid_order_statuses_for_order_again', 'bbloomer_order_again_statuses' ); function bbloomer_order_again_statuses( $statuses ) { $statuses[] = 'processing'; return $statuses; } // ---------------- // 2. Add Order Actions # My Account add_filter( 'woocommerce_my_account_my_orders_actions', 'bbloomer_add_edit_order_my_account_orders_actions', 50, 2 ); function bbloomer_add_edit_order_my_account_orders_actions( $actions, $order ) { if ( $order->has_status( 'processing' ) ) { $actions['edit-order'] = array( 'url' => wp_nonce_url( add_query_arg( array( 'order_again' => $order->get_id(), 'edit_order' => $order->get_id() ) ), 'woocommerce-order_again' ), 'name' => __( 'שנה בחירה', 'woocommerce' ) ); } return $actions; } // ---------------- // 3. Detect Edit Order Action and Store in Session add_action( 'woocommerce_cart_loaded_from_session', 'bbloomer_detect_edit_order' ); function bbloomer_detect_edit_order( $cart ) { if ( isset( $_GET['edit_order'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-order_again' ) ) WC()->session->set( 'edit_order', absint( $_GET['edit_order'] ) ); } // ---------------- // 4. Display Cart Notice re: Edited Order add_action( 'woocommerce_before_cart', 'bbloomer_show_me_session' ); function bbloomer_show_me_session() { if ( ! is_cart() ) return; $edited = WC()->session->get('edit_order'); if ( ! empty( $edited ) ) { $order = new WC_Order( $edited ); $credit = $order->get_total(); wc_print_notice( 'A credit of ' . wc_price($credit) . ' has been applied to this new order. Feel free to add products to it or change other details such as delivery date.', 'notice' ); } } // ---------------- // 5. Calculate New Total if Edited Order add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_use_edit_order_total', 20, 1 ); function bbloomer_use_edit_order_total( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $edited = WC()->session->get('edit_order'); if ( ! empty( $edited ) ) { $order = new WC_Order( $edited ); $credit = -1 * $order->get_total(); $cart->add_fee( 'Credit', $credit ); } } // ---------------- // 6. Save Order Action if New Order is Placed add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_edit_order' ); function bbloomer_save_edit_order( $order_id ) { $edited = WC()->session->get( 'edit_order' ); if ( ! empty( $edited ) ) { // update this new order update_post_meta( $order_id, '_edit_order', $edited ); $neworder = new WC_Order( $order_id ); $oldorder_edit = get_edit_post_link( $edited ); $neworder->add_order_note( 'Order placed after editing. Old order number: ' . $edited . '' ); // cancel previous order $oldorder = new WC_Order( $edited ); $neworder_edit = get_edit_post_link( $order_id ); $oldorder->update_status( 'cancelled', 'Order cancelled after editing. New order number: ' . $order_id . ' -' ); WC()->session->set( 'edit_order', null ); } } I want to make a button on my website header, that removes the session after the user clicked "edit order", if he decided to cancel the editing and stay with his original order, the line that does that is WC()->session->set( 'edit_order', null ); and I want it to be a link
Found the right way: add_action('afterheader','leave_edit_order'); function after_header_block_yhm() { do_action( 'afterheader'); } add_shortcode('under_header_location','after_header_block_yhm'); function leave_edit_order(){ $edited = WC()->session->get('edit_order'); if(!empty($edited) && isset($_GET['cancel_edit_order'])){ WC()->session->set( 'edit_order', null ); wc_print_notice( 'You left "edit order" session, enjoy your stay.' ); } add_action('afterheader','leave_edit_order'); Made a new action called 'afterheader" and a new shortcode called 'under_header_location', then used elementor to put the shortcode in the place I wanted, and then called the function in that new action I made on the function Im checking that the user has ?cancel_edit_order on his url and then calling the function
Woocommerce coupon code for personalized customer
I have generated coupon code dynamically for each customer when they purchasing a product from the website and i have set certain conditions. When I have dynamically created a coupon code, it stored in woocommerce > coupons section. function couponCodeGeneration($order_id, $i){ // Get the order ID $coupon_code = $order_id."".$i; // Coupon Code $amount = '100%'; // Amount $discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product wp_coupon_exist( $coupon_code ); if( wp_coupon_exist( $coupon_code ) ) { //coupon code exists. } else { //coupon code not exists, so inserting coupon code $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' //'post_category' => array(1) ); $new_coupon_id = wp_insert_post( $coupon ); //SET THE PRODUCT CATEGORIES wp_set_object_terms($post_id, 'Holiday Season offers', 'product_cat'); // Add meta update_post_meta( $new_coupon_id, 'discount_type', $discount_type ); update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); update_post_meta( $new_coupon_id, 'individual_use', 'yes' ); update_post_meta( $new_coupon_id, 'product_ids', '' ); update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); update_post_meta( $new_coupon_id, 'usage_limit', '1' ); update_post_meta( $new_coupon_id, 'expiry_date', '2019-07-31' ); update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' ); update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '1' ); update_post_meta( $new_coupon_id, 'usage_limit_per_user', '1' ); update_post_meta( $post_id, 'times', '1' ); echo '<div class="couponCode"><strong>Your Coupon Code for your next purchase - '.$coupon_code.'</strong><hr></div>'; } } I need help in the following situation. The generated coupon code should not be used by another customer. The coupon only personal to that customer. Coupon code cannot be transferred. When customer placing an order, the generated coupon code not stored in admin orders page. How do I know which coupon code is generated by which customer. Can someone give me a suggestions.
Since Woocommerce 3 your code is really outdated, with a lot of mistakes. wp_coupon_exist() function doesn't exist. To restrict the coupon to a specific customer, you can use Email restrictions WC_Coupon method that will also allow you to know which customer has generated the coupon code. You could also set a some custom meta data if needed saving the user ID or the user complete name. So there is two functions in my code: The first one that checks if the coupon code doesn't exist The second one that generate the coupon code as you planned. The code: // Utility function that check if coupon exist function does_coupon_exist( $coupon_code ) { global $wpdb; $value = $wpdb->get_var( " SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_coupon' AND post_name = '".strtolower($coupon_code)."' AND post_status = 'publish'; "); return $value > 0 ? true : false; } function coupon_code_generation( $order_id, $i ){ $coupon_code = $order_id."".$i; // Coupon Code // Check that coupon code not exists if( ! does_coupon_exist( $coupon_code ) ) { // Get a new instance of the WC_Coupon object $coupon = new WC_Coupon(); // Get the instance of the WC_Order object $order = wc_get_order( $order_id ); ## --- Coupon settings --- ## $discount_type = 'percent'; // Type $coupon_amount = '100'; // Amount $customer_email = array( $order->get_billing_email() ); // Customer billing email $product_categories_names = array('Holiday Season offers'); $date_expires = '2019-07-31'; // Convert to term IDs $term_ids = array(); foreach( $product_categories_names as $term_name ) { if ( term_exists( $term_name, 'product_cat' ) ) $term_ids[] = get_term_by( 'name', $term_name, 'product_cat' )->term_id; } ## --- Coupon settings --- ## // Set the necessary coupon data $coupon->set_code( $coupon_code ); $coupon->set_discount_type( 'percent' ); $coupon->set_amount( 100 ); if( is_array($term_ids) && sizeof($term_ids) > 0 ) $coupon->set_product_categories( $term_ids ); $coupon->set_email_restrictions( $customer_email ); $coupon->set_individual_use( true ); $coupon->set_usage_limit( 1 ); $coupon->set_usage_limit_per_user( 1 ); $coupon->set_limit_usage_to_x_items( 1 ); $coupon->set_date_expires( date( "Y-m-d H:i:s", strtotime($date_expires) ) ); // Save the data $post_id = $coupon->save(); } echo isset($post_id) && $post_id > 0 ? sprintf( '<div class="couponCode"><strong>%s <code>%s</code></strong>.<hr></div>', __("Your Coupon Code for your next purchase is", "woocommerce"), $coupon_code ) : __("Sorry, a coupon code already exist.", "woocommerce"); } Code goes in function.php file of your active child theme (or active theme). tested and works. You will get an output something like (when the coupon is generated): Your Coupon Code for your next purchase is 1198A Or if coupon exist: Sorry, a coupon code already exist.
I have fixed my issue. Add the following code in functions.php add_filter( 'woocommerce_coupon_is_valid', 'wc_riotxoa_coupon_is_valid', 20, 2 ); if ( ! function_exists( 'wc_riotxoa_coupon_is_valid' ) ) { function wc_riotxoa_coupon_is_valid( $result, $coupon ) { $user = wp_get_current_user(); $restricted_emails = $coupon->get_email_restrictions(); return ( in_array( $user->user_email, $restricted_emails ) ? $result : false ); } } Ref: https://gist.github.com/riotxoa/f4f1a895052c195394ba4841085a0e83
Add custom field as an order note in Woocommerce
I'm trying to add a custom field jckwds_date as an order note. I can't for the life of me figure out why this code isn't working in functions.php? The code also only allows the note to be added in the are a certain role type. function wdm_my_custom_notes_on_single_order_page($order){ $user = wp_get_current_user(); $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer'); if( array_intersect($allowed_roles, $user->roles ) ) { $value = get_post_meta( $product->get_id(), 'jckwds_date', true ); echo $value; $order->add_order_note( $value, $is_customer_note = 1 ); } } Basically I need THIS: To be added HERE:
Update: The following code will add from the order custom field 'jckwds_date' (or checkout posted field value 'jckwds_date') an order note that will appear in backend for defined user roles: add_action( 'woocommerce_checkout_update_order_meta', 'product_custom_field_to_custom_order_notes', 100, 2 ); function product_custom_field_to_custom_order_notes( $order_id, $data ){ // HERE define allowed user roles $allowed_roles = array('administrator', 'super_wholesale_customer', 'wholesale_customer'); $user_id = get_post_meta( '_customer_user', 'jckwds_date', true ); $user = new WP_User( $user_id ); // Exit if no matched user roles if( ! array_intersect( $allowed_roles, $user->roles ) ) return; // Get the date custom field (or checkout field) if( get_post_meta( $order_id, 'jckwds_date', true ) ){ $note = get_post_meta( $order_id, 'jckwds_date', true ); } elseif( isset( $_POST['jckwds_date'] ) ){ $note = sanitize_text_field( $_POST['jckwds_date'] ); } // The order note if( isset($note) && ! empty($note) ){ $order = wc_get_order( $order_id ); // The WC_Order Object $order->add_order_note( $note ); // Add the note $order->save(); // Save the order } } Code goes in function.php file of the active child theme (or active theme). It should work.
add_filter( 'woocommerce_checkout_fields' , 'custom_add_checkout_fields' ); // Our hooked in function - $fields is passed via the filter! function custom_add_checkout_fields( $fields ) { unset($fields['order']['order_comments']); $fields['order']['order_note']['priority'] = 5; $fields['order']['order_note'] = array( 'label' => __('Order Notes', 'woocommerce'), 'placeholder' => _x('Order Notes', 'placeholder', 'woocommerce'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; }
Try this $order = wc_get_order( $order_id ); $note = __("This my custom note..."); $order->add_order_note( $note ); $order->save();
Try this add_action('woocommerce_checkout_update_order_meta', 'checkout_field_update_order_meta'); function checkout_field_update_order_meta($order_id) { if (!empty($_POST['field_name'])) { update_post_meta($order_id, 'MY Custom Field', sanitize_text_field($_POST['field_name'])); } }
Try this code. add_action( 'woocommerce_thankyou', 'my_note_custom' ); function my_note_custom( $order_id ) { $order = new WC_Order( $order_id ); $note = __("This my custom note..."); $order->add_order_note( $note ); $order->save(); }
Found out it was a simple as changing the $product to $order as it is the order custom field value I'm trying to retrieve. Full code below: function wdm_my_custom_notes_on_single_order_page($order){ $user = wp_get_current_user(); $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer'); if( array_intersect($allowed_roles, $user->roles ) ) { $value = get_post_meta( $order->get_id(), 'jckwds_date', true ); $note = '<b>Wholesale</b>'; echo $value; $order->add_order_note( $value, $is_customer_note = 1 ); } }
need help in wp_insert_post
this is very weird i had this code working but then i needed some data from my database inorder to make sure that i am not creating the same post title again since if its not unique i want to generate another one. i will post the code with what i added and made wp_insert_post stop. however everywhere i searched i found that people should be including them: <?php define( 'SHORTINIT', true ); //local require_once( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' ); //live //require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' ); global $wpdb; $subEmail = isset($_POST['subEmail']) ? $_POST['subEmail'] : null; if($subEmail){ //Check if $subEmail is in DB $dbEmails = $wpdb->get_results( 'SELECT meta_value FROM wp_postmeta WHERE meta_key = "_field_newsletteremail"' ); foreach($dbEmails as $emailObj){ if($subEmail === $emailObj->meta_value){ echo "exists"; die(); } } //else create a coupon for him $couponCodeValue = $subEmail.time(); $couponCodeValue = str_replace(' ', '', $couponCodeValue); $couponCodeValue = md5($couponCodeValue); //Get Existing Coupons we created $dbCoupons = $wpdb->get_results( 'SELECT post_title FROM wp_posts WHERE post_type = "shop_coupon" AND post_status = "publish"' ); $x = 0; $y = 6; $couponCodeSixValue = substr($couponCodeValue, $x , $y); for($y; $y < strlen($couponCodeValue); $y++){ //if code found generate new one, else just us the current if($dbCoupons[$i]->post_title === $couponCodeSixValue){ $couponCodeSixValue = substr($couponCodeValue, ($x+1) , ($y+1)); } $x++; } imagineme_create_coupon($couponCodeSixValue); } function imagineme_create_coupon( $couponCodeSixValue ){ require_once( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' ); $coupon_code = $couponCodeSixValue; // Code $amount = '10'; // Amount $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product $coupon = array( 'post_title' => ''.$coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); $new_coupon_id = wp_insert_post( $coupon ); // Add meta update_post_meta( $new_coupon_id, 'discount_type', $discount_type ); update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); update_post_meta( $new_coupon_id, 'individual_use', 'yes' ); update_post_meta( $new_coupon_id, 'product_ids', '' ); update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); update_post_meta( $new_coupon_id, 'usage_limit', '1' ); update_post_meta( $new_coupon_id, 'expiry_date', '' ); update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' ); update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); echo $coupon_code; } ?> the function at the very bottom was working before i added : define( 'SHORTINIT', true ); //local require_once( $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php' ); //live //require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' ); global $wpdb; but without this i can not access the wpdb from this side script. how can i make sure that all files for wp_insert_post are loaded and access it, since i am getting PHP Fatal error: Uncaught Error: Call to undefined function wp_insert_post()
WooCommerce: Creating an order programmatically with a variable product
I'm trying to create an order programmatically. Using wc_create_order() this is pretty straightforward: $myProduct = new WC_Product(100); $order = wc_create_order(); $order->add_product($myProduct, 1); $order->calculate_totals(); This works as expected, and an order is created for a simple product with ID 100 for the correct amount. However, if I try to do this with a variation, it doesn't seem to behave correctly. After much trial and error, I got it to sort-of work this way: $membershipProduct = new WC_Product_Variable(100); $theMemberships = $membershipProduct->get_available_variations(); $trueProduct = new WC_Product(100); $variationsArray = array(); foreach ($theMemberships as $membership) { if ($membership['sku'] == $chosenVariation) { $variationID = $membership['variation_id']; $variationsArray = $membership['attributes']; } } if ($variationID) { $trueProduct->variation_id = $variationID; } $order = wc_create_order(); $order->add_product($trueProduct, 1, $variationsArray); $order->calculate_totals(); However, although it does create the order with the correct product and the correct variation ID, the total for the order is always 0 (which, coincidentally, is the price of the first variation). Originally I was trying $order->add_product() with the object created from new WC_Product_Variable(), but that resulted in no products being added to the order at all, which leads me to believe it's an issue with creating orders programmatically with variable products. However, following the WooCommerce source code for these calls, I can't see what I'm doing wrong. Is there something I'm missing, or a better way to create an order with a variable product?
Solved it. Even though I could have sworn I'd tried (and failed) doing it this way, the answer was to not add the parent product ($trueProduct in the example), but to add the variation product by its ID. This may have failed previously because, as #helgatheviking noted, my $variationsArray was formatted incorrectly according to the source; I needed an array with a ['variation'] key to send the correct variation array of attributes. In total, my working code now looks like this: $membershipProduct = new WC_Product_Variable(100); $theMemberships = $membershipProduct->get_available_variations(); $variationsArray = array(); foreach ($theMemberships as $membership) { if ($membership['sku'] == $chosenVariation) { $variationID = $membership['variation_id']; $variationsArray['variation'] = $membership['attributes']; } } if ($variationID) { $varProduct = new WC_Product_Variation($variationID); $order = wc_create_order(); $order->add_product($varProduct, 1, $variationsArray); $order->calculate_totals(); }
if you already have the variation_id you can just do this $product_variation = new WC_Product_Variation($variation_id); $order = wc_create_order(); $args=array(); foreach($product_variation->get_variation_attributes() as $attribute=>$attribute_value){ $args['variation'][$attribute]=$attribute_value; } $order->add_product($product_variation, $product['quantity'], $args);
Here is the solution that worked for me: function add_item_to_order( $order_id, $prod_id ) { $order = wc_get_order( $order_id ); $_product = wc_get_product( $prod_id ); // Set values $item = array(); $item['product_id'] = $_product->id; $item['variation_id'] = isset( $_product->variation_id ) ? $_product->variation_id : ''; $item['variation_data'] = $item['variation_id'] ? $_product->get_variation_attributes() : ''; $item['name'] = $_product->get_title(); $item['tax_class'] = $_product->get_tax_class(); $item['qty'] = 1; $item['line_subtotal'] = wc_format_decimal( $_product->get_price_excluding_tax() ); $item['line_subtotal_tax'] = ''; $item['line_total'] = wc_format_decimal( $_product->get_price_excluding_tax() ); $item['line_tax'] = ''; $item['type'] = 'line_item'; // Add line item $item_id = wc_add_order_item( $order_id, array( 'order_item_name' => $item['name'], 'order_item_type' => 'line_item' ) ); // Add line item meta if ( $item_id ) { wc_add_order_item_meta( $item_id, '_qty', $item['qty'] ); wc_add_order_item_meta( $item_id, '_tax_class', $item['tax_class'] ); wc_add_order_item_meta( $item_id, '_product_id', $item['product_id'] ); wc_add_order_item_meta( $item_id, '_variation_id', $item['variation_id'] ); wc_add_order_item_meta( $item_id, '_line_subtotal', $item['line_subtotal'] ); wc_add_order_item_meta( $item_id, '_line_subtotal_tax', $item['line_subtotal_tax'] ); wc_add_order_item_meta( $item_id, '_line_total', $item['line_total'] ); wc_add_order_item_meta( $item_id, '_line_tax', $item['line_tax'] ); wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => array(), 'subtotal' => array() ) ); // Store variation data in meta if ( $item['variation_data'] && is_array( $item['variation_data'] ) ) { foreach ( $item['variation_data'] as $key => $value ) { wc_add_order_item_meta( $item_id, str_replace( 'attribute_', '', $key ), $value ); } } } $item['item_meta'] = $order->get_item_meta( $item_id ); $item['item_meta_array'] = $order->get_item_meta_array( $item_id ); $item = $order->expand_item_meta( $item ); $order->calculate_totals(); }