I've made a wordpress video section for one of my clients. In the backend it's a custom post type where the users can place the ID (http://www.youtube.com/watch?v=kFHshctPO9E) of a video.
With this ID i embed the video and get the duration and rating of each video from the youtube api.
Now each time a video is loaded (in the overview template all the video's are loaded) it has to reach out to YouTube to get the duration (which i then parse to hours and minutes instead of seconds) and rating of all video's.
I would somehow like to cache or save these values to a custom field or somewhat.
Anyone got any idea?
Thank in advance!
Rick
You can use post_meta to save this information the first time the post is saved and then load the information from the database instead of using youtube api again.
I put you an example in pseudocode:
add_action('save_post', 'my_function_to_save_posts_action');
function my_function_to_save_posts_action($post_id) {
$the_post = get_post($post_id);
if ($the_post->type == 'my_custom_post_type') {
$youtube_data = my_function_to_retrieve_youtube_data();
if (my_function_exists_post_meta($post_id)) {
update_post_meta($post_id, 'my_video_duration', $youtube_data['duration']);
update_post_meta($post_id, 'my_video_rating', $youtube_data['rating']);
}
else {
add_post_meta($post_id, 'my_video_duration', $youtube_data['duration'], true);
add_post_meta($post_id, 'my_video_rating', $youtube_data['rating'], true);
}
}
}
And of course, when you are queryng for your custom post type you must retrive the post_meta information and show this information properly.
Related
Problem
We are using Zapier to fetch orders from WooCommerce, through the REST Api. We encountered an error that Zapier can't recieve some orders because of the data sent by WooCommerce is to large (more than 7MB).
This is caused by the plugin Fancy Product Designer, which sends a PNG image as a base64 string in the line_items meta_data (see example of this in the image above).
Can't find any documentation or hooks on how to filter the order data being sent by the Api.
Suggested solution
We need to filter the order data that WooCommerce is sending, and avoid sending the line_items -> meta_data -> field where the key is _fpd_product_thumbnail.
(We can't delete the field, since its used internally for other cases.)
With the example provided by #VijayHardaha I was able to put together a working code.
For anyone facing the same issue with Fancy Product Designer sending to large data to Zapier, you can use the following code to remove the image data from the REST API for Zapier only.
add_filter('woocommerce_rest_prepare_shop_order_object', 'filter_order_response', 10, 3);
function filter_order_response($response, $post, $request){
global $wpdb;
$zapier_user_id = 1320; // User ID of the "Zapier" account in WordPress
if(get_current_user_id() == $zapier_user_id) {
$i = 0;
$lineItemsCount = count($response->data["line_items"]);
while($i < $lineItemsCount) {
$j = 0;
$metaDataCount = count($response->data["line_items"][$i]["meta_data"]);
while($j < $metaDataCount) {
if($response->data["line_items"][$i]["meta_data"][$j]["key"] == "_fpd_product_thumbnail") {
$response->data["line_items"][$i]["meta_data"][$j]["value"] = "";
$response->data["line_items"][$i]["meta_data"][$j]["display_value"] = "";
}
$j++;
}
$i++;
}
}
return $response;
}
I have a custom field in my product's page that is saved to database and it is also visible in my editor
I am trying to access it through API as a meta data and I am successful in updating that field.
The problem is that it doesn't automatically gets updated on my frontend and I have to click update (product) to get updated value in woocommerce.
I am using this code to save it to my wp database
function save_woocommerce_product_custom_fields($post_id)
{
$product = wc_get_product($post_id);
$custom_fields_woocommerce_title = isset($_POST['alt_points']) ? $_POST['alt_points'] : '';
$product->update_meta_data('alt_points', sanitize_text_field($custom_fields_woocommerce_title));
$product->save();
}
add_action('woocommerce_process_product_meta', 'save_woocommerce_product_custom_fields');
now I need this code as an api to automatically show it on frontend
I am trying to manipulate the my_acf_save_post function to do some maths and update a field with the resulting number. This part is working, the fields full_market_price and example_price are used to work out calculated_price.
I did this some days ago but have now run in a problem whenever trying to save a page, or post that doesn't need this function, and doesn't contain the ACF fields. So every section of the website rather than just the 1 that requires this maths. It results in various errors and I can't save the pages properly.
How can I make this code snippet only work if the page being saved is within custom post type? So it won't break the other pages?
I'm trying something using
if (is_single() && is_post_type('nameofposttype'))
however I can't seem to get it right, PHP not my strongest! Is a better way to ask if the fields does not exist, then do nothing?
Many thanks for any help or ideas,
function my_acf_save_post( $post_id ) {
// get values
$fmp = get_field('full_market_price');
$saPercent = get_field('example_price');
// do something
$examplePrice = ($fmp / 100) * $saPercent;
update_field('calculated_price', $examplePrice, $post_id);
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I just ran into a similar issue. I would do something like:
function my_acf_save_post ($post_id) {
// If not CPT, exit
if (get_post_type($post_id) != 'nameofposttype') {
return;
}
// Remainder of code
...
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I'm trying to add a custom field of event_month when a post is published or saved. I'm using the save_post action and getting the contents of a custom field containing the date and trying to store this in a separate custom field with just the month. This works perfectly when saving a post that has already been created. I've shown my code below.
add_action('save_post', 'update_event_date');
function update_event_date($post_id){
$post_type = get_post_type($post_id);
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
if ($post_type == 'tribe_events'){
$month = date("m",strtotime($event_datee));
update_post_meta($post_id, 'event_month', $month);
}
}
The problem arises when creating a new post. I think this is because the action fires before the _EventStartDate meta has been created and therefore the month can't be taken from this.
The hook is firing correctly and as intended when saving/updating a post but doesn't correctly get the month from the meta when creating a new post.
I'd really appreciate it if someone could provide me with some guidance.
To access post meta passed together with yours, you can do something like this:
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
foreach($_POST['meta'] as $meta){
if($meta['key'] == '_EventStartDate'){
$event_datee = $meta['value'];
}
};
I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callbackĀ
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filterĀ
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();