Easy Digital Downloads - Fire PHP when transaction was successful goes wrong - php

I’m using Easy Digital Downloads for my Wordpress webshop. After someone buys a item it needs to add credits to the MySQL database. I got this working by adding PHP code to shortcode-receipt.php.
This is working correct but when I reload the receipt via browser or mail the PHP code will fire again:
php
<?php
if( edd_is_payment_complete( $payment->ID ) && edd_receipt_show_download_files( $item['id'], $edd_receipt_args, $item ) ) :
?>
Could someone help me out here?
What is the best method to fire PHP code when a payment is successful?
Thanks in advance!

There is action edd_complete_purchase which fires when order is completed.
So, in your case, I would remove the code from the shortcode and created an plugin. Inside the plugin should be something like this
function my_edd_receipt( $payment_id ){
if( edd_is_payment_complete( $payment_id ) && edd_receipt_show_download_files( $item['id'], $edd_receipt_args, $item ) ) :
}
add_action( 'edd_complete_purchase', 'my_edd_receipt');

Related

woocommerce hook on custom order status changed

hey I was trying to make a plugin that sends WhatsApp messages on order status change so this is my code
add_action("woocommerce_order_status_changed", "order_status_wapp",10,3);
function order_status_wapp($order_id, $old_status, $new_status){
if( $new_status == "processing" && carbon_get_theme_option( 'show_processing' )) { require("incl/apicall.php");
$message = carbon_get_theme_option( 'processing_message' );
require("incl/message_attr.php");
}
the code is working well when changing the status from the order table area ( actions ), but changing the status from the order details or programmatically shows no effect. is there any hook that triggers status change no matter what the way of changing is?
in the end, it worked.
My issue was that I was testing the event with the ACF Update field, which did not work.
However, when I changed it to the main event (send SMS), it worked perfectly.

WordPress execute specific cronjob on specific Page

I am having a small tour page that uses built-in cron jobs (wp-cron) to sync tours and sessions via an API every 4 hours and until here everything is perfect.
Now, what I am trying to achieve now is to also and "forcefully" execute this specific cronjob (regardless of it's schedule) when being on a specific page, e.g. Thank you page (not Woocommerce).
What's the best way to have this achieved? Some expert help would be greatly appreciated, thank you.
I think you can try by using admin_init hooks and check the page id or url then run the execute function.
add_action( 'travel_booking_do_tour_search_action', 'my_custom_cron_function');
function my_custom_cron_function(){
// your execute action
}
add_action( 'admin_init', 'execute_cron_on_specific_page' );
function execute_cron_on_specific_page{
if( is_page( 2094 ) ) // page_id = 2094 (eg:thank you page)
{
my_custom_cron_function();
}
}
I have been left with the doubt, are you using the native Cron job of your server or the wp-cron of WordPress?

Weird issue with PHP on localhost vs test server (apache2)

I am working on a website running WordPress and woocommerce
I have a custom function to add stuff to the cart which is the following:
function custom_add_products_to_cart(){
WC()->cart->empty_cart();
$request_body = file_get_contents('php://input');
$decoded = json_decode($request_body);
$cartElements = $decoded->addToCart;
foreach ( $cartElements as $product_id ) {
WC()->cart->add_to_cart( $product_id );
}
if ( $decoded->Uid ) {
WC()->session->set( 'uid', $decoded->Uid );
}
die();
};
This is quite a straightforward function. All it does is iterating through a list of products id and launching the default Woocommerce add to cart function.
The issue I've got is that this works perfectly fine on my localhost (also debugging it ut behaves just like expected) but when I try it on a test server it doesn't work.
The function is firing (i tried to print some messages) but the cart is not emptying and the new products are not added.
I check the code, commit and revision, and everything is correct.
What else can it be?
My last thought was on the version of PHP:
my localhost runs 7.1.2 while the test server runs 7.0.22 - can it be the PHP version or not? any idea on what else I could try?
Sorry if I cannot provide much more details but unfortunately there's not much more to add...
Also, I am not posting this to WordPress community for now as I think it's not a WordPress related stuff (nor woocommerce) but rather PHP code (maybe my function is somehow wrong?) or PHP version
Thanks in advance to everyone
i think the problem is here
file_get_contents('php://input');
depending on your PHP configuration, maybe you need to change the allow_url_fopen setting in you php.ini.
You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.
I recommend using cURL over file_get_contents() anyways, since it was built for this.
At the end i simply solve this by calling two functions:
the first one empties the cart:
function empty_cart(){
WC()->cart->empty_cart();
}
and the second one carries on adding to the cart:
function custom_add_products_to_cart(){
$request_body = file_get_contents('php://input');
$decoded = json_decode($request_body);
$cartElements = $decoded->addToCart;
foreach ( $cartElements as $product_id ) {
WC()->cart->add_to_cart( $product_id );
}
if ( $decoded->Uid ) {
WC()->session->set( 'uid', $decoded->Uid );
}
die();
};
and it now works properly.

WooCommerce: How to retain checkout info when client leaves then comes back?

Is there a simple way or a plugin to retain checkout information entered by the client after he/she leaves and comes back?
This plugin retains "fields information for customers when they navigate back and forth" however it has quite a lot of recent bad reviews so I don't think I'll use that for production. Any alternative suggestion?
---- Update ----
The code below is working, but only if data is submitted!
The only possible ways are javascript/jQuery form event detection on checkout fields and worpress Ajax:
Using ajax connected to some session transients function (as in code below).
Using (javascript) web Storage: localStorage, sessionStorage…
I have found some real interesting code in this thread that is using sessions transients to store checkout data.
// this function sets the checkout form data as session transients whenever the checkout page validates
function set_persitent_checkout ( $a ) {
$arr = array();
foreach ( $a as $key => $value )
if ( ! empty($value) )
$arr[$key] = $value;
WC()->session->set( 'form_data', $arr );
return $a;
}
add_action( 'woocommerce_after_checkout_validation', 'set_persitent_checkout' );
// this function hooks into woocommerce_checkout_get_value to substitute standard values with session values if present
function get_persistent_checkout ( $value, $index ) {
$data = WC()->session->get('form_data');
if ( ! $data || empty($data[$index]) )
return $value;
return is_bool($data[$index]) ? (int) $data[$index] : $data[$index];
}
add_filter( 'woocommerce_checkout_get_value', 'get_persistent_checkout', 10, 2 );
// This is a fix for the ship_to_different_address field which gets it value differently if there is no POST data on the checkout
function get_persitent_ship_to_different ( $value ) {
$data = WC()->session->get('form_data');
if ( ! $data || empty($data['ship_to_different_address']) )
return $value;
return is_bool($data['ship_to_different_address']) ? (int) $data['ship_to_different_address'] : $data['ship_to_different_address'];
}
add_action( 'woocommerce_ship_to_different_address_checked', 'get_persitent_ship_to_different' );
Add this code to the functions.php file located in your active child theme or theme.
Explanations from the author:
1. Save the form data:
The first function set_persitent_checkout hooks into woocommerce_after_checkout_validation.
Whenever that hook is fired, any current form data is saved as a WordPress transient via the WC_Session_Handler class (which was recently updated in version 2.5 to be a lot more efficient).
2. Check the saved data on reload:
Next we hook woocommerce_checkout_get_value with get_persitent_checkout. As the name suggests, here we check the session transients and return any matches for the current field if found.
3. Make ship_to_different_address work:
The only difficult was the ship_to_different_address field, which gets its value through a different method.
To get around this the final function was added. This works exactly the same as the previous function, but hooks into woocommerce_ship_to_different_address_checked.
There you have it. It would be nice if the data was saved after every field update on checkout, but the woocommerce_after_checkout_validation hook fires enough to capture the data at all the important points.
Functions.php snipped posted by LoicTheAztec didn't work for me.
I found this plugin which remembers everything I type or select in Woocommerce checkout, including shipping fields and my custom additions to the template:
Save Abandoned Carts – WooCommerce Live Checkout Field Capture
Account passwords, if creating during checkout, are naturally not remembered.

WooCommerce add_to_cart metadata not sticking

I am a WP noob but very comfortable in PHP.
I am working with a client and we have built a product customization tool as an Angular.js single page application. When the product is finished being customized we are seeking to inject it into a WooCommerce cart so the client can check out. To do this we are $_POSTing the data to a PHP file in the root directory of the WP install. The code to catch it looks like:
require_once('./wp-load.php' );
global $woocommerce;
$woocommerce->session->set_customer_session_cookie(true);
$woocommerce->cart->empty_cart();
$id_arr = $_GET['productID'];
$pdfName = $_GET['pdfName'];
for($i=0; $i<count($id_arr); $i++){
$id = $id_arr[$i];
if ($id==0) continue;
if ($i==0){
$ret = $woocommerce->cart->add_to_cart($id, 1, '', '', array('pdfName'=>$pdfName));
}else{
$ret = $woocommerce->cart->add_to_cart($id);
}
}
wp_redirect(site_url().'/cart/');
The products are all correctly added to the cart but after checkout there is no sign of the metadata. After extensive research, I have found an article here: https://wpml.org/forums/topic/woocommerce-add-to-cart-does-not-work-with-wpml-activated/ that shows me that plugins can cause this behavior. So I have two specific questions?
Does my code make sense, am I creating the metadata array correctly?
Do I need to create something in WooCommerce called pdfName before I can do this?
Is there another way that metadata can be added to an order in
WooCommerce that may work around this problem?
It looks like you are adding the metadata correctly. However, as soon as you refresh, WooCommerce re-creates the cart data. Therefore you have to tell WooCommerce to maintain the metadata when it is pulling the cart from the stored session. Well, at least that is my understanding of it. So I think you need to filter thee $cart_item as it is run through the woocommerce_get_cart_item_from_session filter:
add_filter( 'woocommerce_get_cart_item_from_session', 'so_29660316_get_cart_item_from_session', 11, 2 );
function so_29660316_get_cart_item_from_session( $cart_item, $values ) {
if ( isset( $values['pdfName'] ) ) {
$cart_item['pdfName'] = $values['pdfName'];
}
return $cart_item;
}

Categories