Dynamic due date with ACF Pro for Elementor Pro - wordpress

I have a function to add a dynamic due date to the Countdown Timer widget from Elementor (Pro). With ACF Pro I created a options page with a date and time picker to change the date, but it doesn't work and I cannot see why. I also tried other codes from this page, but they also don't work. I get a timer with 0 days, hours, minutes and seconds or with the date entered via the Elementor edit page.
Here's the code:
add_action( 'elementor/frontend/widget/before_render', function(\Elementor\Element_Base $element){
if ("countdown" != $element->get_name()) return;
$countdown_date = get_post_meta(get_the_id(), "my-date-field",true);
$element->set_settings("due_date",date("Y-m-d H:i",strtotime($countdown_date))) ;
});
Thanks for any help.

Please make sure that you named the Field Name is "my-date-field" and the Field Type is "Date Time Picker"(look at below image for more detail).
If this is not fix your issue then:
Do you get any error message on WP Dashboard or Debug console?
Update:
After take a look on your backend and make some debug, i found that you set your custom field is in option page, while this piece of code use to get the value of the custom field base on the post's ID.
To get this value, you will need a different code like this:
add_action( 'elementor/frontend/widget/before_render', function(\Elementor\Element_Base $element){
if ("countdown" != $element->get_name()) return;
$countdown_date = get_field('my-date-field', 'option', false);
$element->set_settings("due_date",date("Y-m-d H:i",strtotime($countdown_date)));
});
Also, please make sure that you set the correct return format to "y-m-d H:i:s" for the code working as below image:

Related

How to add Date and Time to Google App Maker?

Can someone help me, I want to add time and date to an app without the user having to insert this.
I have figured out how to fix the Date, but not the time.
Can anyone help me ?
Loading a text widget automatically on page refresh
Well, I'm kind of new to appmaker so this isn't necessarily the easiest way to to it. But this works. I added a new text widget named todaysDate to the screen as shown below:
Then I went into clientside script and added this code in ready function.
$(function(){
google.script.run
.withSuccessHandler(upDateToDaysDate)
.getTodaysDate();//this code
google.script.run
.withSuccessHandler(upDateLastRecipient)
.getLastEmailRecipient();//this was already there
});
And I added this function above it because it appears that functions have to be defined before they are used.
function upDateToDaysDate(s){
app.pages.Email.descendants.TodaysDate.value=s;
}
And then in server script I added this function:
function getTodaysDate(){
return Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"E MMM d, yyyy");//for time just add HH:mm:ss to the format parameter
}
When you load the page or preview, the the ready function will call the server script and it will return todays date and upDateTodaysDate() will load it into the todaysDate text widget.

woocommerce order to mark as completed upon visiting a specific page

I want a woocommerce order to be automatically marked as completed upon visiting a specific page in WordPress
This specific page will have the order_id in GET variable. Now what i need to know is how to find an order by order_id and mark the status as completed on a specific WordPress page.
You can do it with following piece of code:
if(is_page('page_title')){
$order = new WC_Order($_GET['your_order_id']);
//wc-completed, wc-processing
$update_status = array('ID'=>$_GET['your_order_id'],'post_status'=>'wc-completed');
wp_update_post( $update_status );
}
First check whether It is on your specific page or not.
Second thing is to get your order
Third step is to update status of order.
Let me know if you have any doubt.
EDITED
You can refer codex for more details on is_page.
If you have page_id then you need to put condition like if(is_page(42)) where 42 is ID of your page.
So, Rohil_PHPBeginner's answer works like a charme. If you urgently search for a "just copy and paste" solution and have no time to figure things out, go ahead and use this ready to use snippet for your functions.php:
Don't forget to replace the number 42 with the page id of your desired page that will handle as an order-completion-url. Afterwards you can call the order completion by url by: http://your-url.com/yourdeletionpage/?your_order_id=ORDER_ID_TO_BE_COMPLETED
function my_abhaken() {
if(is_page(42)){
$order = new WC_Order($_GET['your_order_id']);
//wc-completed, wc-processing
$update_status = array('ID'=>$_GET['your_order_id'],'post_status'=>'wc-completed');
wp_update_post( $update_status );
}
}
add_action('wp_head', 'my_abhaken');

Discussion pagination in elgg 1.8 is not working properly

We have a website created using elgg, and we have discussion part of elgg where pagination is not working properly.
When listing all discussion list, pagination is working correctly, from latest to oldest
But pagination inside topic (one of the discussion) where replies are too many, and they separated by pagination (Previous, next), it is showing replies incorrectly:
In the first page we have:
As you can see it is ordered from latest to oldest: 17 days ago and then comes 21 days ago.
But when you go next page:
It is ordered inside the page from latest to oldest, but the the pages should be swapped.
How can I fix it?
Thanks forward
It works properly. Discussions are ordered by last action on the thread. You may have older thread first due to it having most recent response of all threads.
To change it, look at discussion_handle_list_page function in mod/groups/lib/discussion and how it uses order_by parameter. You need to override this page and just remove order_by to use default ordering, which is creation time of the entity (in his case thread). You could do it by tapping to the "route" plugin hook, or overriding whole discussions page handler.
And that's why not just change it inline: http://learn.elgg.org/en/1.9/guides/dont-modify-core.html
I have found finally going through all pagination libraries, and annotation views.
The problem is when listing replies for discussion on replies.php file which in /views/default/discussion there are two jquery functions on the bottom of the page that reverse the order.
function reverse(arr){
var newArr = [];
for(var iter = arr.length -1; iter >= 0; iter--){
newArr.push(arr[iter])
}
return newArr;
}
I have change the iter to start from 0 and go to arr.lenthg.
But it is not all yet, in order to get the latest replies first, I have added to
$options = array(
'guid' => $vars['entity']->getGUID(),
'annotation_name' => 'group_topic_post',
);
this line:
'order_by'=> 'time_created desc'
And then I got my replies ordered correctly: from latest to oldest.

WooCommerce shipping methods not working for logged-in users?

I have developed a custom build on woocommerce.
All is fine except for the checkout page.
The site ships to UK only, and i have Flat Rate shipping enabled.
However, on the checkout page there are two issues. The first is an invalid argument in a foreach loop:
Warning: Invalid argument supplied for foreach() in /home/****/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 291
And the second problem is:
There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.
I am bashing my head against a wall here. I have a shipping method enabled, so why is it continuing to tell me there is no method available?
All i want it to do there is simply list all the shipping methods i have enabled so the user can select one.
I also noticed that the shipping methods display when user is not logged in, but when they are logged in it shows Flat Rate - then a random price that isn't entered anywhere in admin.
To elaborate a little. This problem does not occur if i add a variable product to the art using the ?add-to-cart url, but as soon as i use the add_to_cart function it causes those errors.
woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));
The above code works fine if it is used in a URL, but i want to call it via ajax with the function.
Go to woo commerce settings and add a shipping zone. See here.
On existing products and when adding new products always ensure u set the shipping location. See here
I had a similar problem, I got that message even if Shipping Zones and Shipping Methods were configured correctly. I expected just one flat rate, no choices available.
Doing more tests the bug disappeared when there were 2 or more methods available, it was weird. I found the issue in the theme template, the first control was a wrong:
if ( 1 < count( $available_methods ) ) :
instead of
if ( $available_methods ) :
Perhaps you have a bug in the theme template too. You can do a diff/compare between the original template (/wp-content/plugins/woocommerce/templates/cart/cart-shipping.php) and the custom file (/wp-content/themes/YOURTHEME/woocommerce/cart/cart-shipping.php)

Modifying Wordpress post status on Publish

I am attempting to validate fields on a custom post type in the admin panel Edit Post page.
When the user clicks "Publish" I want to validate fields in the POST data, and change the post_status to "pending" if the data does not pass the tests. When this occurs, I'd also like to add errors to the page in the admin notices area.
I've been trying this with an added hook to the "wp_insert_post" action which also saves our own data. I'm not certain of the order of operations, but I'm assuming that the wp_insert_post events happen first, and then my function gets called via the hook.
The problem is that it's the Wordpress function which is doing the post publish actions, so by the time I get to validate data, Wordpress has already saved the post with a status of "publish". What I need to do is either prevent that update, or change the status back to "pending", but I'm having little success in finding a way to do this within the API.
So, here's an order of operations I'd like to effect:
1. admin user edits post data and clicks "Publish"
2. via wp_insert_post, my data validation and post meta save routine is called
3. If data passes validation, post status is "published"
4. Otherwise, post status set to "pending" & message shown in admin notice area
Surely someone has done this, but extensive Googling just leads me to the same seemingly irrelevant pages. Can someone point me in the right direction here? Thanks in advance-
UPDATE:
So, RichardML was indeed correct, hooking to the wp_insert_post_data filter gave me the right place to validate admin post edit page fields. I'm updating this however to note what the rest of the solution is, specifically getting the reason reported in the admin notice area.
First off, you can't just output data or set a field because the admin page is the result of a redirect, and by the time you get to rendering the admin post page again, the admin_notices action is already gone. The trick was something I picked up from another forum, and it's hackish, but it works.
What you'll need to do is in your validation filter function, if you determine that you will need to display errors, is use set_option() to add a blog option with a unique name (I used 'publish_errors'). This should be HTML code in a div with a class of "error".
You will also need to add an action hook for 'admin_notices', pointing at a function which checks for the existence of the 'publish_errors' option, and if it finds it, prints it to the page and deletes it with delete_option().
You can use the wp_insert_post_data filter to inspect and modify post data before it's inserted into the database.
In response to your update I don't think it's necessary to temporarily add an option to the database. It should be possible to simply add a query string variable to the Wordpress redirect, something like this:
add_filter('wp_insert_post_data', 'my_post_data_validator', '99');
function my_post_data_validator($data) {
if ($data['post_type'] == 'post') {
// If post data is invalid then
$data['post_status'] = 'pending';
add_filter('redirect_post_location', 'my_post_redirect_filter', '99');
}
return $data;
}
function my_post_redirect_filter($location) {
remove_filter('redirect_post_location', __FILTER__, '99');
return add_query_arg('my_message', 1, $location);
}
add_action('admin_notices', 'my_post_admin_notices');
function my_post_admin_notices() {
if (!isset($_GET['my_message'])) return;
switch (absint($_GET['my_message'])) {
case 1:
$message = 'Invalid post data';
break;
default:
$message = 'Unexpected error';
}
echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}

Resources