Woocommerce - Hook when a coupon is created - woocommerce

For woocommerce: is there a hook fired when a woocommerce coupon is created?
I need to add this hook in myTheme/functions.php to fire an action just at a coupon creation.
UPDATE:
I included this code in my functions.php but it doesn't seem to work. It should print "it works" at coupon creation but it doesn't. No error, nothing at all.
function after_new_coupon_created( $coupon_id, $coupon ){
echo "it works";
}
add_action( 'woocommerce_new_coupon', 'after_new_coupon_created', 10, 2 );

As Alon Eitan said in the comment you can use like below.
function after_new_coupon_created( $coupon_id, $coupon ){
// your code here.
}
add_action( 'woocommerce_new_coupon', 'after_new_coupon_created', 10, 2 );

Related

Add a shortcode above the WooCommerce product short description

I am trying to add a shortcode to all my woocommerce single product pages. I want the shortcode to appear above the short description.
I have found this code and added it to functions.php, but I can see that it adds the shortcode below the short description (and above the add to cart button).
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text = do_shortcode('[my-shortcode]');
return $description.$text;
}
I would be very gratefull for some help :)
function customShortcodeBeforeShortDescription( $post_excerpt )
{
echo do_shortcode('[wpqr-code]');
}
add_filter('woocommerce_short_description','customShortcodeBeforeShortDescription', 10, 1);
You can use the woocommerce_single_product_summary action hook. try the below code. The code will go in your active theme functions.php file.
function add_shortcode_before_short_description(){
echo do_shortcode( '[wpqr-code]' );
}
add_action( 'woocommerce_single_product_summary', 'add_shortcode_before_short_description', 15, 1 );
Tested and works

wc_print_notices is not displaying any messages in woocommerce pages?

I have used default woocommerce templates for shop, cart and single page.
I have not removed any hooks either but also I am not getting any message.
Any Idea?
add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
echo 'Hook is working fine';
}
I am getting this message 'Hook is working fine' but not wc_print_notices();.
I'm not really sure what is the problem. Your question needs further details. With that said, can you try adding this code to your functions.php of your current theme.
add_action( 'template_redirect', 'test' );
function test() {
wc_add_notice( __( 'Sorry there was a problem.', 'woocommerce' ), 'error' );
}
Let me know if it does something.
UPDATE
If you have something like this:
add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
$notices = WC()->session->get('wc_notices');
print_r($notices);
}
it will not work because $notices will be empty once wc_print_notices() is called.
try changing priority and you will get something. Should be something like this:
add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 9 );
use priority below 10. Because WooCommerce is using 10.
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );

Adding a hook to "Place order" button in woocommerce

When the user gets to the checkout, there is a button , the "Place order" button at the bottom of the form. I have been trying to add a hook to this button in woocommerce, but I don't seem to find the correct one, I have tried woocommerce_checkout_place_order... but it doesnt do anything.
function my_function() {
//write function
}
add_action( "woocommerce_order_status_pending", "my_function");
Thanks in advance!
You need this hook woocommerce_review_order_after_submit. It will execute any function you hook to it just after the submit area. with this hook you can add some html on the checkout page after the submit button.
But if you need to call a function after the user have pressed the "Place order" button - use woocommerce_checkout_order_processed. This one will hook you just after the order was created so you can use the freshly generated order details:
add_action( 'woocommerce_checkout_order_processed', 'is_express_delivery', 1, 1 );
function is_express_delivery( $order_id ){
$order = new WC_Order( $order_id );
//something else
}
You may check this site for some more hooks you might use on the checkout page.
## I USED THIS CODE FOR ADDING DELIVERY CHARGES DEPENDING UPON THE CART SUBTOTAL AND SOME POST FIELDS ##
function action_woocommerce_checkout_process($wccs_custom_checkout_field_pro_process )
{
global $woocommerce;
//Add Fuel Surcharge & CAF
function woo_add_cart_fee() {
global $woocommerce;
if ( WC()->cart->cart_contents_total < 1500 &&
$_POST['delivery_type']=='Pick Up') {
$fuel_surchargeandCAF = get_option( 'fuel_surchargeandCAF',
70 );
WC()->cart->add_fee( __('Delivery Charges', 'woocommerce'),
$fuel_surchargeandCAF, TRUE, '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
};
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10,
1 );

WordPress: on submitting post check if post is updated

In WordPress with the publish_{post_type} (http://codex.wordpress.org/Post_Status_Transitions) you can hook into the loop if a post with custom type is published. But publish_{post_type} is also triggers if a a post is updated.
I am basically looking for a way to check the old and new status, within the publish_post_type hook. Does anyone have a nifty idea as to how to accomplish that?
I am doing this:
function doStuff( $post_ID ) {
// do stuff
return $post_ID;
}
add_action( 'publish_ttplaned', 'doStuff' );
So basically I need to check the old and new status of the post within the fuction doStuff().
In case someone else is looking for the aswer. I ended up using Williams solution.
add_action( 'draft_to_publish', 'doStuff', 10, 1 );
add_action( 'pending_to_publish', 'doStuff', 10, 1 );
function doStuff( $post ) {
if ( get_post_type( $post ) == "my_custom_post_type" ){
// do stuff
}
}
This works since a new post starts out as a draft even if you publish it right away.
As #rnevius says, there's no hook in WordPress core called publish_post_type.
There is also publish_post - which I've just tested it to make sure it only runs when the post is first published, not when it's subsequently edited.
So you can do something like:
function my_function($post_ID, $post) {
if ("foo" == $post->post_type) {
// do something
}
}
add_action( 'publish_post', 'my_function', 10, 2 );
There is also the wp_transition_post_status(), which calls several actions:
transition_post_status with $new_status, $old_status, $post
or, if you want to do it the other way around:
{$old_status}_to_{$new_status} and passes a WP_Post object as the only parameter.

wooCommerce call function in checkout page

I have a function in my themes functions.php file which displays some information about the product. On the checkout page below the billing address I want to out put the information there.
Here is my function in the themes functions.php
function wc_checkout_description_so_1( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
echo '<div>HTML OUTPUT HERE</div>';
}
I have tried to use add_filter to below the billing address but doesnt not work:
add_filter( 'woocommerce_before_checkout_shipping_form', 'wc_checkout_description_so_1', 10, 2 );
All I need is the output below the shipping information and the above I think should work?
Thanks
J
woocommerce_after_checkout_shipping_form might be more appropriate for displaying something after the shipping address. Either way, the only variable that is passed to the woocommerce_after_checkout_shipping_form hook is the $checkout variable. You can var_dump that variable to see what it is available.
add_action( 'woocommerce_after_checkout_shipping_form', 'wc_checkout_description_so_1' );
function wc_checkout_description_so_1( $checkout )
{
var_dump( $checkout );
}

Resources