I tried about 5 hooks to get the order hook for completed and the function doesn't run at all but woocommerce_add_to_cart for example is working!
1. woocommerce_order_status_changed
2. woocommerce_new_order
I just make the alert to know if the function runs but the fucntion is quite large and I make order manullay as a test before deploying the plugin in this function in the plugin in index.php
function SP_order_token($order_id)
{
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
echo "hello";
global $woocommerce, $post;
echo $order_id;
$order = wc_get_order($order_id);
$order_data = $order->get_data(); // The Order data
var_dump($order_data);
}
// the final hook is when an order successfully paid
add_action('woocommerce_order_status_changed', 'SP_order_token',10,1);
Your approach for debugging php is wrong. You can't alert or do JS things on the server side. Also, var_dump and echo will work but you don't know where they gonna echo or dump the output.
The correct way for php debugging will be to write your output in external files or in error logs. but I prefer writing in files on the root of wordpress.
Here is the code snippet that you can use:
function sp_order_token( $order_id ) {
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
/**
* We're using file_put_contents to create a debug.txt file in the root of wordpress where your wp-config.php exists.
*
* we're also passing true in print_r so that print_r returns the output instead of printing it during code execution.
*
* ABSPATH is a constant of wordpress root defined by core WordPress.
*/
file_put_contents( ABSPATH . 'debug.txt', print_r($order_data, true ) );
}
add_action( 'woocommerce_order_status_changed', 'sp_order_token', 10, 1 );
Once your action will run in wordpress root you'll find debug.txt with your expected output.
Please replace with this and check again.
function woo_order_status_change_custom_check($order_id) {
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
// echo "hello";
$order = new WC_Order( $order_id );
$orderstatus = $order->status;
var_dump($orderstatus);
}
add_action('woocommerce_order_status_changed', 'woo_order_status_change_custom_check', 10, 1);
Related
I'm trying to introduce a shortcode on the thankyou.php page to show the details of the order just placed by a customer.
If I write the code in php like this it works and shows the total:
<?php echo $order->get_total(); ?>
Now I'm trying to get the same result through a shortcode, but I don't know some parameters and therefore can't get it to work.
<?php
add_shortcode( 'custom-woocommerce-total' , 'custom_total' );
function custom_total(){
$customer_id = get_current_user_id();
$order = new WC_Order($order_id); // I suppose that's not right.
return $order->get_total();
} ?>
can anyone help me understand what I'm doing wrong?
You would need to get the order id first by using global $wp variable. Try this:
add_shortcode('custom-woocommerce-total', 'custom_total');
function custom_total($attr)
{
if (is_wc_endpoint_url('order-received'))
{
global $wp;
$order_id = absint($wp->query_vars['order-received']);
if ($order_id)
{
$order = new WC_Order($order_id);
if($order)
{
return $order->get_total();
}
}
}
}
And in the thankyou page template use it like this:
echo do_shortcode('[custom-woocommerce-total]');
Don't forget to override the template.
I'm trying to create a function that will retrieve an order by its ID. For some reason I can't get the WooCommerce global function get_order to work. I'm passing a valid order id to the function and trying to print it out to verify that it's working. The function has been placed in my functions.php file.
function getWC_order_details($id){
global $woocommerce;
$order = get_order( $id );
print "<pre>";
print_r($order);
print "</pre>";
}
I have tested echoing other data out of the function without a problem.
First of all make function like this :
function getWC_order_details($order_id) {
$order = new WC_Order( $order_id );
var_dump($order);
}
After that, use it with some woo_commerce action or filter.
function use_after_cart_table(){
getWC_order_details(40);
}
add_action( 'woocommerce_after_cart_table', 'use_after_cart_table' );
So after adding any product to the cart, you will see after cart table that there is one array containing all the details.
NOTE : You can use any other action or filter and you can find them here.
EDITED:
function getWC_order_details($order_id) {
$order = new WC_Order( $order_id );
//var_dump($order);
$order_shipping_total = $order->get_shipping();
$order_shipping_method = $order->get_shipping_methods();
var_dump($order_shipping_total);//Use it for debugging purpose or to see details in that array
var_dump($order_shipping_method);//Use it for debugging purpose or to see details in that array
$_order = $order->get_items(); //to get info about product
foreach($_order as $order_product_detail){
//var_dump($order_product_detail);
echo "<b>Product ID:</b> ".$order_product_detail['product_id']."<br>";
echo "<b>Product Name:</b> ".$order_product_detail['name']."<br><br>";
}
//var_dump($_order);
}
Try this. It might be useful to you.
function getWC_order_details($id)
{
$array = WC_API_Orders::get_order( $id, $fields );
print "<pre>";
print_r($order);
print "</pre>";
}
Source:
File name: woocommerce/includes/api/class-wc-api-orders.php
how to get a product by id outside the loop
this is my function:
function rakhsh_product_info($id){
$result = get_product( $id );
die($result);
}
get Call to undefined function get_product() error
Try this:
if ( function_exists( 'get_product' ) ) {
$result = get_product( $id );
} else {
$result = new WC_Product( $id );
}
Hope that helps.
All of these errors indicate that your plugin is loaded before Woocommerce. Place the calls to woocommerce functions at least in plugins_loaded action or later. Example from wordpress.org:
<?php
add_action( 'plugins_loaded', 'my_plugin_override' );
function my_plugin_override() {
// your code here
}
?>
Check here for list of available action hooks and action execution order.
I'm new to Wordpress & PHP, so kindly excuse the naivety of the question, if any.
I'm building a plugin where I need to select values from the database and create a new HTML page with the values. I'm using a custom template file.
What I've done till now
Extracted the values from database
Load & display my own template in my plugin file
add_action( 'init', 'leb_add_endpoint' );
function leb_add_endpoint()
{
add_rewrite_endpoint( 'result', EP_PERMALINK );
}
add_action( 'template_include', 'leb_render_template' );
function leb_render_template($default_template) {
//some code removed for brevity
if ( ! is_singular() || !isset($wp_query->query_vars['result']) )
{
return $default_template;
}
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
$default_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $default_template;
}
The content of my-custom-template.php is as follows
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
?>
The page gets displayed without any problem. All I want is to insert $sample_result and other similar results pulled form database into my-custom-template.php
I need to generate dynamic pages based on values pulled from DB. So each time, the final page created might be different. E.g. if one hits www.example.com/sample-post/result, a page will be shown with values pulled from the DB. If one hits www.example.com/another-sample-post/result, a different page will be shown with different values. Both these pages will have the same design, only a few values will be different. This is what I'm trying to achieve.
How can I do that? Please help me. I'm stuck. :(
You need to define "result" in query vars.
Use EP_ROOT Endpoint Mask ( result/{var} is located in the root )
Inside template_include hook, you can find result value inside $wp_query object.
I've already tested the code
// Add a new var to query vars
function result_add_query_vars( $vars ){
$vars[] = 'result';
return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );
// Add endpoint
function result_add_endpoint() {
add_rewrite_endpoint( 'result', EP_ROOT );
}
add_action( 'init', 'result_add_endpoint');
// change the template
function result_render_template($template)
{
global $wp_query;
if ( array_key_exists( 'result', $wp_query->query_vars ) ) {
$result = get_query_var('result');
$new_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $new_template;
} else {
return $template;
}
}
add_action( 'template_include', 'result_render_template' );
Now you can retrieve the query var in your custom template
/*
* Template Name: My Custom Template
*/
$result = get_query_var('result');
echo $result;
Well why don't you use $wp_query Inside your my-custom-template.php
<?php
/* Template Name: My Template*/
global $wp_query;
echo '<pre>';
print_r($wp_query); // Use this in case you want to see what else do you have with you.
echo '<pre/>';
// Now you can use $wp_query to build your dynamic query at run time.
// This will allow you to perform task at run time
?>
To Retrieve Meta
If you have saved something as a meta then
<?php
$meta_values = get_post_meta( $post_id, $key, $single );
?>
To Retrieve Child Post
If you want to retrieve child posts then
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
// Do your stuff here such as below
the_title();
the_content();
endwhile;
else:
echo 'No Post Found';
endif;
?>
1) Write this function in function.php in your active template.
function leb_render_template() {
//some code removed for brevity
//$sql = 'YOUR_QUERY_CODE'
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
return $my_template;
}
add_action('wp_ajax_leb_render_template', 'leb_render_template');
add_action('wp_ajax_nopriv_leb_render_template', 'leb_render_template');
2) Call function in your custom template.
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
$result = leb_render_template();
print_r($result); // Print Your function output.
?>
I am making a plugin, to register a shortcode, which when used like this: [append_css mycss] it will look for a custom field called mycss and add the contents to the head of the document. It all works great, except that the code is being added to the body, and I don't know how to get it to add to the head.
I have tried adding an action wp_head but I don't know how to pass variables whilst doing that, and it does not seem to fire from within the shortcode callback anyway.
function append_css_short($params){
global $post;
if(sizeof($params)){
$key = $params[0];
} else {
$key = 'css';
}
return '<style type="text/css">'.
get_post_meta($post->ID, $key, true)
.'</style>';
}
add_shortcode('append_css','append_css_short');
How would I get this to write to the head instead of body?
Is there a better approach to the problem?
If you want to print something to the head of the document you need use add_action with wp_head hook.
A better approach might be to not use a shortcode at all. Shortcodes are intended to add content or modify content. Instead create a function that you can attach to wp_head that will print your css. You may want to force the user to keep the custom field name consistent. Or better yet have your plugin add a metabox to posts that they can enter custom css into. Then you will always be certain of the name of the field.
function append_css() {
global $post;
if ( ! get_post_meta( $post->ID, 'mypluginname_css', true ) ) {
return;
}
return '<style type="text/css">'. get_post_meta($post->ID, $key, true) .'</style>';
}
add_action( 'wp_head', 'append_css' );
The only thing i am not certain of is whether or not $post with the custom field data will be available outside the loop. So you may have to add some extra querying in there to pull the custom data at the time that wp_head is fired.
With some minor adjustments to the answer provided by #Jrod, here is the complete working plugin, with usage as follows:
Create custom field called append_css (or append_js) with required code
... of course, there is no number 2 ;)
Code
<?php
/**
* #package Append_CSS_JS
* #version 0.1
*/
/*
Plugin Name: Append CSS JS
Plugin URI: http://itaccess.org/wordpress-plugins/append-css-js/
Description: Append Css or Js to any page using Custom Fields
Author: Billy Moon
Version: 0.1
Author URI: http://billy.itaccess.org/
*/
function append_css() {
global $post;
if ( ! get_post_meta( $post->ID, 'append_css', true ) ){
return;
}
echo '<style type="text/css">'. get_post_meta($post->ID, 'append_css', true) .'</style>';
}
add_action( 'wp_head', 'append_css' );
function append_js() {
global $post;
if ( ! get_post_meta( $post->ID, 'append_js', true ) ) {
return;
}
echo '<script type="text/javascript">'. get_post_meta($post->ID, 'append_js', true) .'</script>';
}
add_action( 'wp_head', 'append_js' );
?>
I don't think this is possible that way, because shortcodes will be called after wp_head(), so you cannot hook on it.
Here's the list of all wordpress hooks http://adambrown.info/p/wp_hooks
EDIT:
Here's a workaround:
In your template:
<?php
//get the content of your post:
if(have_posts()) while(have_posts()) the_post();
$pcontent = get_the_content();
endwhile;
?>
<html>
<head>
...
</head>
<body>
...
<?php echo $pcontent; ?>
</body>
</html>