I'm editing a packing slip, which is generated when the order is paid and being packed. The packing slip automatically adds a coupon which will be printed on it as well, if a product of a certain category is included.
My problem is, that every time I refresh or reopen the packing slip, a new coupon is being generated with the same coupon code. So I would like to implement a rule, that checks if the coupon_code already exists.
add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2 );
function wpo_wcpdf_custom_text_categories ($template_type, $order) {
// collect categories from order items
$order_cats = array();
$items = $order->get_items();
foreach ($items as $item_id => $item) {
$product = $order->get_product_from_item($item);
if ($product) {
$terms = get_the_terms( $product->id , 'product_cat' );
foreach ($terms as $term) {
$order_cats[$term->term_id] = $term->name;
}
}
}
$target = array('Testtragen', 'Testtragetuch');
//
// check for your category requirement
if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){
$coupon_code = $order->get_order_number();
$discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product
$order_date = get_post_meta( $order->id, '_wcpdf_order_date', true );
$due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 60 days') );
$email = $order->billing_email;
$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', 'no' );
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', '' );
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' );
*/
}
}
So I would like to add an if statement before the wp_insert_post($coupon), that all this code only gets executed if the coupon with the coupon_code doesn't already exist.
I tried : term_exists( $coupon_code, ‘coupons’)but this didn't work.
Thanks for your help!
Update (added WooCommerce 3 compatibility)
Here it is a solution creating a special meta_key for the $order->id to avoid new coupon generation with the same coupon code, when refreshing or reopening the packing slip.
Here is your changed code (with comment explanations):
add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2 );
function wpo_wcpdf_custom_text_categories ($template_type, $order) {
// collect categories from order items
$order_cats = array();
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Loop through order items
foreach ($order->get_items() as $item_id => $item) {
$product = version_compare( WC_VERSION, '3.0', '<' ) ? $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id() : $item->get_product();
$product_id = method_exists( $product, 'get_id' ) ? $item->get_product_id() : $item['product_id'];
if ($product) {
$terms = get_the_terms( $product_id , 'product_cat' );
foreach ($terms as $term) {
$order_cats[$term->term_id] = $term->name;
}
}
}
$target = array('Testtragen', 'Testtragetuch');
//
// check for your category requirement
if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){
$coupon_code = $order->get_order_number();
$discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product
$order_date = get_post_meta( $order_id, '_wcpdf_order_date', true );
$due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 60 days') );
$email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email;
$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'
);
// ### We create a meta_key '_wcpdf_coupon' for this order with value 'no'
if( empty( get_post_meta( $order_id, '_wcpdf_coupon', true) ) ) {
add_post_meta( $order_id, '_wcpdf_coupon', 'no', true );
}
// ### if this meta_key for this order has a value = 'no' was update it to 'yes'
if( get_post_meta( $order_id, '_wcpdf_coupon', true) == 'no' ) {
// Now we update it to 'yes'. This avoid the coupon be reused for this order.
update_post_meta( $order_id, '_wcpdf_coupon', 'yes');
$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', 'no' );
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', '' );
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' );
}
}
}
This should solve this issue…
Related
I add coupons at woocommerce with this code programmatically.
if(empty($coupon_post)){
$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', '' );
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' );
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
update_post_meta( $new_coupon_id, 'product_categories', '' );
update_post_meta( $new_coupon_id, 'exclude_product_categories', '' );
update_post_meta( $new_coupon_id, 'minimum_amount', '' );
update_post_meta( $new_coupon_id, 'customer_email', '' );
}
But it says always the usage_limit = 1.
I add additional this code to update:
add_action( 'save_post_shop_coupon', 'my_child_after_coupon_save', 10, 3 );
function my_child_after_coupon_save( $post_id, $post, $update ) {
update_post_meta( $post_id, 'usage_limit', '');
}
But it doesn't work first.But if I open the coupon in the backend and update without any changes. The usage limit is set to unlimited.
How can trigger this, that I needn't to open all coupons.
Since WooCommerce 3, your code is a bit outdated as for example apply_before_tax is not used anymore. You should better use all available WC_Coupon setter methods, for coupon creation.
In the code below I just use the necessary setter methods (related to your code):
// Get an empty instance of the WC_Coupon Object
$coupon = new WC_Coupon();
// Set the necessary coupon data (since WC 3+)
$coupon->set_code( $coupon_code ); // (string)
// $coupon->set_description( $description ); // (string)
$coupon->set_discount_type( $discount_type ); // (string)
$coupon->set_amount( $coupon_amount ); // (float)
// $coupon->set_date_expires( $date_expires ); // (string|integer|null)
// $coupon->set_date_created( $date_created ); // (string|integer|null)
// $coupon->set_date_modified( $date_created ); // (string|integer|null)
// $coupon->set_usage_count( $usage_count ); // (integer)
$coupon->set_individual_use( true ); // (boolean)
// $coupon->set_product_ids( $product_ids ); // (array)
// $coupon->set_excluded_product_ids( $excl_product_ids ); // (array)
$coupon->set_usage_limit( 0 ); // (integer)
// $coupon->set_usage_limit_per_user( $usage_limit_per_user ); // (integer)
// $coupon->set_limit_usage_to_x_items( $limit_usage_to_x_items ); // (integer|null)
// $coupon->set_free_shipping( $free_shipping ); // (boolean) | default: false
// $coupon->set_product_categories( $product_categories ); // (array)
// $coupon->set_excluded_product_categories( $excl_product_categories ); // (array)
// $coupon->set_exclude_sale_items( $excl_sale_items ); // (boolean)
// $coupon->set_minimum_amount( $minimum_amount ); // (float)
// $coupon->set_maximum_amount( $maximum_amount ); // (float)
// $coupon->set_email_restrictions( $email_restrictions ); // (array)
// $coupon->set_used_by( $used_by ); // (array)
// $coupon->set_virtual( $is_virtual ); // (array)
// Create, publish and save coupon (data)
$coupon->save();
Now to update coupons you can use the following hooked function with any setter method like:
add_action( 'woocommerce_coupon_options_save', 'action_coupon_options_save', 10, 2 );
function action_coupon_options_save( $post_id, $coupon ) {
$coupon->set_usage_limit( 0 );
$coupon->save();
}
Code goes in functions.php file of the active child theme (or active theme).
I've been racking my brain for a few days on this issue and can't figure out why my code to import is creating duplicate product categories.
Problem: I am pulling in about 30,000 products from the third party API in JSON format it is then is inserted into a custom database, from which I pull each product's data and format for importing into WooCommerce, these processes are working fine the problem comes when I am inserting the category terms. When categories are inserted using wp_insert_term some categories are duplicated but have a different URL slug.
ie...
— Ranges, Cooktops & Ovens
— — Range Hoods & Downdraft Ventilation (slug: range-hoods-downdraft-ventilation )
— — Range Hoods & Downdraft Ventilation (slug: range-hoods-downdraft-ventilation-ranges-cooktops-ovens)
The child category gets duplicated but with a different slug.
This is the Category array I'm looping through to create the hierarchical category paths:
"categoryPath":[{"id":"cat00000","name":"Company Name"},{"id":"abcat0900000","name":"Appliances"},{"id":"abcat0904000","name":"Ranges, Cooktops & Ovens"},{"id":"abcat0904002","name":"Range Hoods & Downdraft Ventilation"}]
Here is my single product import code the relevant category portion in at the bottom starting with $categoryPath, just wanted to give some context of what Im doing:
$result= $wpdb->get_var( "SELECT `results_array`
FROM $table_name
WHERE `arr_serialize2` = $textProductSku
AND `update_type` = 'create'
");
$final_product_formate_arrays = json_decode($result, true);
$post = array(
'post_title' => $final_product_formate_arrays['parent_or_simple_product']['title'],
'post_content' => $final_product_formate_arrays['parent_or_simple_product']['longDescription'],
'post_excerpt' => isset( $final_product_formate_arrays['parent_or_simple_product']['shortDescription']) ? $final_product_formate_arrays['parent_or_simple_product']['shortDescription'] : '' ,
'post_status' => "publish",
'post_parent' => '',
'post_type' => "product",
);
$post_id = wp_insert_post( $post );
if (!$post_id)
{
return false;
}
update_post_meta($post_id,'_complete_product_data',json_encode($final_product_formate_arrays));
update_post_meta($post_id, '_sku', $final_product_formate_arrays['parent_or_simple_product']['sku']);
update_post_meta( $post_id, '_visibility', 'visible' );
if($final_product_formate_arrays['parent_or_simple_product']['inStoreAvailability'] > 0)
{
update_post_meta( $post_id, '_manage_stock', "yes" );
update_post_meta( $post_id, '_stock', $final_product_formate_arrays['parent_or_simple_product']['stockQuantity'] );
update_post_meta( $post_id, '_stock_status', 'instock');
} elseif($final_product_formate_arrays['is_variation']){
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_stock_status', 'instock');
} else {
update_post_meta( $post_id, '_manage_stock', "yes" );
update_post_meta( $post_id, '_stock_status', 'outofstock');
update_post_meta( $post_id, '_stock', 0 );
}
update_post_meta( $post_id, '_regular_price', $final_product_formate_arrays['parent_or_simple_product']['regularPrice'] );
update_post_meta( $post_id, '_sale_price', $final_product_formate_arrays['parent_or_simple_product']['salePrice'] );
update_post_meta( $post_id, '_price', $final_product_formate_arrays['parent_or_simple_product']['salePrice'] );
update_post_meta( $post_id, '_weight', $final_product_formate_arrays['parent_or_simple_product']['weight'] );
update_post_meta( $post_id, '_length', $final_product_formate_arrays['parent_or_simple_product']['depth'] );
update_post_meta( $post_id, '_width', $final_product_formate_arrays['parent_or_simple_product']['width'] );
update_post_meta( $post_id, '_height', $final_product_formate_arrays['parent_or_simple_product']['height'] );
update_post_meta( $post_id, '_wc_average_rating', $final_product_formate_arrays['parent_or_simple_product']['reviewAverage'] );
**$categoryPath = $final_product_formate_arrays['parent_or_simple_product']['categoryPath'];
if(!empty($categoryPath))
{
$parent_id='';
foreach ($categoryPath as $key => $value) {
if($key!=0) {
$new_str = str_replace( array( ',', '&' ), '-', $value['name'] );
$new_str = sanitize_title( $new_str );
$parent_term = term_exists( $parent_id, 'product_cat' ); // array is returned if taxonomy is given
if( isset( $parent_term )) {
$parent_id = $parent_term['term_id']; // get numeric term id
} else{
$parent_id = 0;
}
$term = wp_insert_term( $value['name'], 'product_cat', [
'description'=> $value['name'],
'parent' => intval( $parent_id ),
'slug' => $new_str,
]
);
if( is_wp_error( $term ) && isset( $term->error_data['term_exists'] ) )
{
$term_id = isset( $term->error_data['term_exists'] ) ? $term->error_data['term_exists'] :'';
$term_error = $term->get_error_message();
$term_error .="\n";
error_log($term_error, 3, $pluginlog);
} else if ( !is_wp_error( $term ) ) {
$new_term_id = isset( $term['term_id']) ? $term['term_id'] : '';
$term_id = $new_term_id;
}
$term_id = intval($term_id);
$parent_id = !empty($term_id)?$term_id:'';
wp_set_post_terms($post_id, $term_id, 'product_cat');**
// wp_set_object_terms($post_id, $term_id, 'product_cat');
} else {
continue;
}
}
}
I'm fixing a plugin-loaded Woocommerce site and now I have a requirement to implement an automatic coupon generator based on the purchase amount: for example, I would like to establish a rule for every $100 purchase, if the customer buys $450, 4 discount coupons of $40 will be generated. All of this codes should be sent to the customer.
I've reviewed the WooCommerce documentation where I can create the coupons: https://docs.woocommerce.com/document/create-a-coupon-programatically/ and already implemented it and sending in the order review mail (after payment has been confirmed):
function add_order_email_instructions( $order, $sent_to_admin ) {
if ( ! $sent_to_admin && $order->get_user_id() && 'processing' == $order->get_status()) {
$lista_cupones = array();
$amount = '40';
for ($i=1; $i<=$order->get_total()/100; $i++) {
$longitud = 12;
$key = '';
$pattern = '1234567890ABCDFGHIJKLMNOPQRSTUWYZ';
$max = strlen($pattern)-1;
for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
$coupon_code = $key;
$discount_type = 'fixed_cart';
$coupon = array(
'post_title' => $coupon_code,
'post_content' => ''.$to,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
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, 'minimum_amount', '100' );
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'yes' );
update_post_meta( $new_coupon_id, 'exclude_sale', 'yes' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', strtotime("+3 days") );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
array_push($lista_cupones, $coupon_code);
}
echo '<h2>';
printf( __( 'Obtén 40 soles de descuento')
);
echo '</h2>';
printf(
__( 'Gracias por su compra. Use el código de descuento <strong>%2$s</strong> para recibir %1$s soles de descuento en su siguiente compra.'),
$amount,
$coupon_code
);
echo '</p>';
}
}
add_action('woocommerce_email_before_order_table','add_order_email_instructions',10);
Now this half works: it validates if order total is less than $100 and doesn't generate a coupon but if it's higher it only gives me one coupon even if order total is 200, 300 400, etc. (it doesn't even generate more than one coupon, seems like it only goes through the loop once and stops).
Thanks!
Already solved, this is the code:
function create_coupon () {
$amount = '40';
$longitud = 12;
$key = '';
$pattern = '1234567890ABCDFGHIJKLMNOPQRSTUWYZ';
$max = strlen($pattern)-1;
for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
$coupon_code = $key;
$discount_type = 'fixed_cart';
$coupon = array(
'post_title' => $coupon_code,
'post_content' => ''.$to,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
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, 'minimum_amount', '100' );
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'yes' );
update_post_meta( $new_coupon_id, 'exclude_sale', 'yes' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', strtotime("+3 days") );
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, 'customer_email', $order->get_billing_email());
return $coupon_code;
}
function add_order_email_instructions( $order, $sent_to_admin ) {
if ( ! $sent_to_admin && $order->get_user_id() && 'processing' == $order->get_status()) {
$lista_cupones = [];
$numero_cupones = $order->get_total()/100;
for ($i=1; $i<=$numero_cupones; $i++) {
array_push($lista_cupones, create_coupon());
}
echo '<h2>';
printf( __( 'Obtén 40 soles de descuento')
);
echo '</h2>';
printf(
__( 'Gracias por su compra. Use el código de descuento <strong>%2$s</strong> para recibir %1$s soles de descuento en su siguiente compra.'),
$amount,
$lista_cupones
);
echo '<br></p>';
}
}
add_action('woocommerce_email_before_order_table','add_order_email_instructions',10);
This is unfinished, now I have to show it in the adequate array format within the mail
I added new custom post type 'giftcard' and extended WooCommerce simple product by adding checkbox 'Gift Card'
Whenever order status changed to 'processing' and it contains product type giftcard, it creates new giftcard post by following code
function status_order_processing( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$is_gift_card = get_post_meta( $item['product_id'], '_woo_giftcard', true );
if($is_gift_card == 'yes'){
$token = base64_encode(openssl_random_pseudo_bytes(32));
$token = bin2hex($token);
$hyphen = chr(45);
$uuid = substr($token, 0, 8).$hyphen
.substr($token, 8, 4).$hyphen
.substr($token,12, 4).$hyphen
.substr($token,16, 4).$hyphen
.substr($token,20,12);
$gift_card = array(
'post_title' => $uuid,
'post_status' => 'publish',
'post_type' => 'giftcard',
);
$gift_card_id = wp_insert_post( $gift_card, $wp_error );
update_post_meta( $gift_card_id, 'woo_gift_card_amount', (int)$item['total'] );
}
}
add_action( 'woocommerce_order_status_processing', 'status_order_processing' );
New post name is a token generated in above code and save item total in meta field 'woo_gift_card_amount'.
Is there any way if I enter giftcard post type token in coupon field and it subtracts amount from Order amount according to meta field 'woo_gift_card_amount' of that post.
Any help would be appreciated.
Coupons are also a custom post. To use your gift card token/uuid as woocommerce coupon, you will need to insert it as a new post in shop_coupon post type.
A quick example (this should go inside your status_order_processing function, or you can use separate function - whichever way suits you):
$coupon_code = $uuid;
$amount = (int)$item['total'];
$discount_type = 'fixed_cart'; //available types: 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 );
if ( $new_coupon_id ) {
//add coupon/post 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, 'expiry_date', $expiry_date);
//update_post_meta($new_coupon_id, 'usage_limit', '1');
//update_post_meta($new_coupon_id, 'individual_use', 'no');
//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', '' );
//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' );
}
I am trying to use customer email address as their own coupon code with a special discount. To achieve this I tried bellow code but nothing happen shows error Call to undefined function get_currentuserinfo(). Is it possible to use coupon code virtually not saving like custom post_type?
Here is the code what I'm trying so far.
global $current_user;
get_currentuserinfo();
$user_email = $current_user->user_email ;
$coupon_code = $user_email; // 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', 'no' );
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', '' );
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' );
The get_currentuserinfo() function is deprecated. Use wp_get_current_user() instead.
You should use in your code:
// (Optional) depending where you are using this code
is_user_logged_in(){
global $current_user;
// If global $current_user is not working
if(empty($current_user))
$current_user = wp_get_current_user();
// Here goes all your other code below… …
}
After that, I have never tried to set an email as coupon code slug programatically, but it should work as it's possible to set in woocommerce a coupon code with an email address (I have test it with success)…
add_action( 'user_register', 'coupon_email', 10, 1 );
function coupon_email( $user_id ) {
if ( isset( $_POST['email_address'] ) )
$coupon = array(
'post_title' => $_POST['email_address'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
}
This will add a coupon with the users email address as the title and input every time a new user registers for your website.