I'm using this code in my functions.php file of the Storefront child theme in Wordpress.
<?php
function action_woocommerce_api_create_order( $order_id, $data, $instance ) {
include get_stylesheet_directory().'/agilephpcode/CurlLib/curlwrap_v2.php';
$event_json = array(
"start"=>1455256687,
"end"=>1455246687,
"title"=>"this is a test event",
"contacts"=>array(5631986051842048),
"allDay"=>false
);
$event_json = json_encode($event_json);
curl_wrap("events", $event_json, "POST", "application/json");
}
add_action( 'woocommerce_api_create_order', 'action_woocommerce_api_create_order');
?>
I've tested the code from the Include to the end of the curl_wrap on an individual php page and it creates an event in Agile CMS. The event would be like a party or a meeting, not a coding event :)
Anyway, it doesn't create the event and I've tried for about four hours to get it to work. Is there something big that I'm missing here? I simply want to make the call. I realize making a party event or something from the creation of an order (after they've paid) isn't logical.
<?php
include get_stylesheet_directory().'/agilephpcode/CurlLib/curlwrap_v2.php';
function action_woocommerce_api_create_order( $order_id, $data, $instance ) {
$event_json = array(
"start"=>1455256687,
"end"=>1455246687,
"title"=>"this is a test event4",
"contacts"=>array(5631986051842048),
"allDay"=>false
);
$event_json = json_encode($event_json);
curl_wrap("events", $event_json, "POST", "application/json");
}
add_action( 'woocommerce_new_order', 'action_woocommerce_api_create_order');
?>
Related
I want to create short code every time when post is published using that specific post content so that i have short codes for each different post, for this i wrote this code, but its not working, can anyone please help.
add_action('publish_adsmanager_banner','create_banner_shortcode');
function create_banner_shortcode()
{
add_shortcode( 'banner_'.get_the_ID().'', 'custom_shortcode' );
}
function custom_shortcode($post_id) {
$message = get_the_content($post_id);
return $message;
}
What if you will just pass an id as an attribute to this shortcode?
add_shortcode( 'my_banner', 'custom_shortcode' ); // [my_banner id="123"]
function custom_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => null,
),
$atts
);
$message = get_the_content( $atts['id'] );
return $message;
}
In your original idea, you cannot create an id-named shortcode just once, it should be registered on each wp init. But in this case, it is hard to get a post context just from the shortcode name, you have to pass an id on each shortcode registration. The solution above will allow you to keep DRY especially if you know the ID of each banner when using them.
On my woocommerce theme I get some errors on Google structured data such as:
I've found code into mytheme/woocommerce/loop/ratings.php
In this file there are only this method:
$product->get_average_rating();
The question is: there's a hook or action that implement this function?
I need to implement "ItemReviewed" props.
This is down to your product schema, option one is to remove the error but wont push the reviews is
/**
* Remove the generated product schema markup from Product Category and Shop pages.
*/
function wc_remove_product_schema() {
remove_action( 'woocommerce_shop_loop', array( WC()->structured_data, 'generate_product_data' ), 10, 0 );
}
add_action( 'woocommerce_init', 'wc_remove_product_schema' );
if you want to aggregate your reviews you will need to update your schema if you are using yoast and woocommerce this can be achieved by looking into the specific yoast woocoommerce plugin and the plugin docs to add the correct schema
https://developer.yoast.com/schema-documentation/woocommerce-seo/
I had the same problem - reviews were not validating and throwing an error
Managed to get it fixed by editing /wp-content/themes/YOURTHEME/woocommerce/single-product/review.php
I added the below code:
<p>Item Reviewed: <span itemprop="itemReviewed"><?php echo get_the_title(); ?></span></p>
That generated the itemReviewed markup in Structured Data Tool and my reviews validated.
Might want to place it in the child theme just in case :)
define the woocommerce_structured_data_review callback
function filter_woocommerce_structured_data_review( $markup, $comment ) {
global $product;
$markup['itemReviewed']['sku'] = $product->get_sku();
$markup['itemReviewed']['brand'] = $product->get_attribute( 'brand' ) ?? null;
$markup['itemReviewed']['description'] = wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) );
$markup['itemReviewed']['image'] = wp_get_attachment_url( $product->get_image_id() );
$markup['itemReviewed']['isbn'] = $product->get_attribute( 'isbn' ) ?? null;
$markup['itemReviewed']['AggregateRating'] = $product->get_average_rating();
return $markup;
};
Woocommerce Reviews filter
add_filter( 'woocommerce_structured_data_review',
'filter_woocommerce_structured_data_review', 10, 2 );
I would like to receive a custom value from a request made by WordPress Heartbeat API.
The problem is that the filter only work in functions.php and not from plugin constructor as I always do.
I have plenty others hooks which perfectly work. The syntax is correct. Can I misunderstand the loading sequence ?
In plugin constructor (don't work):
add_filter( 'heartbeat_received', array( $this, 'test_heartbeat_received' ), 10, 2 );
function test_heartbeat_received( $response, $data ) {
$response['test'] = 'test';
return $response;
}
In functions.php (work):
add_filter( 'heartbeat_received', 'test_heartbeat_received', 10, 2 );
function test_heartbeat_received( $response, $data ) {
$response['test'] = 'test';
return $response;
}
I expect this JSON response : { test: "test", "wp-auth-check": true, server_time: 1546856046 },
but only get { "wp-auth-check": true, server_time: 1546856046 }.
Thank you for your help !
EDIT
I have managed to make it work by putting the filter outside the main class, the problem could be in relation with POO but I don't understand the process...
add_filter( 'heartbeat_received', array( new Test, 'test_heartbeat_received' ), 10, 2 );
My class, related to front page, was instantiated with the conditionnal tag !is_admin() so all AJAX actions were not triggered. Without this condition it works !
We have a few processes that team member must follow when changing a Wordpress status. Because of this I am trying to display a message on status change as a reminder.
I found the post transition function on the WP Codex and think this is the right function for the job, I tried setting up simple JS Alert as a test but does not seem to work. I tested my function was being called using a die() test.
Any suggestions would be appreciated, does not need to be JS as long as I can display a message on status change..
My current function:
function status_change_dtp( $post ) {
echo "<script>alert('Display a message on change')</script>";
}
add_action( 'draft_to_publish', 'status_change_dtp', 10, 3 );
Came up with a solution, rather than using the post transition hook I simply updated the status messages themselves...
add_filter( 'post_updated_messages', 'rw_post_updated_messages' );
function rw_post_updated_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$messages['cpt_operator'] = array(
6 => __( "Operator Published - <script>alert('Please Set to Live in Protostar')</script>." ),
10 => __( 'Draft Saved.' )
);
return $messages;
}
I don't want the customers to get linked to the myaccount-dashboard with the annoying Hello xy, from here you can blabla, when they click on "My Account".
Is there an endpoint for the my account dashboard? I didn't find it on the backend in woocommerce>Settings>Accounts...
What's working is: I set up a custom link under menu/navigation... called it "My Account" and set the link to /myaccount/downloads for example.
So, when customers are logged in and they click on My Account, they get redirected to Downloads.
I'm wondering, is there another way to get rid of the dashboard?
Or a redirect solution? Thanks.
Remove it using below function. change downloads to your requirement.
function custom_my_account_menu_items( $items ) {
unset($items['downloads']);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Looking more closely it seems like there are a handful of ways to do this. Take a look at the my-account.php template. You could override this, but I found that if you remove the woocommerce_account_content hook, you get a deprecation notice and WooCommerce thinks you have an outdated template and adds the content anyway.
Looking at the template you will see two hooks. The side menu navigation is added to the woocommerce_account_navigation and the content is added to woocommerce_account_content function. You can remove the defaults from their hooks and add back just the downloads content.
function so_41983566_remove_account_dadshboard(){
remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' );
remove_action( 'woocommerce_account_content', 'woocommerce_account_content' );
add_action( 'woocommerce_account_content', 'so_41983566_download_content' );
}
add_action( 'woocommerce_account_navigation', 'so_41983566_remove_account_dadshboard', 1 );
function so_41983566_download_content(){
do_action( 'woocommerce_account_downloads_endpoint' );
}
Or woocommerce_account_content() and woocommerce_account_navigation() are both pluggable functions and you can just define new versions in your theme/plugin.
This link explains everything:
https://github.com/woocommerce/woocommerce/wiki/Customising-account-page-tabs
First you need to create an endpoint:
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
Then create the menu item:
function my_custom_my_account_menu_items( $items ) {
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
$items['customer-logout'] = $logout;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
Add content to the endpoint
function my_custom_endpoint_content() {
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );