How to get shipping class name not slug woocommerce - wordpress

global $product;
$shippingClass = $product->get_shipping_class();
this returns the slug but I need the name how do I do this?

After $shippingClass add below lines it will get the name.
$shipp_classname= get_term_by('slug',$shippingClass ,'product_shipping_class');
echo $shipp_classname->name;
So complete code is following.
global $product;
$shippingClass = $product->get_shipping_class();
$shipp_classname= get_term_by('slug',$shippingClass ,'product_shipping_class');
echo $shipp_classname->name;

Related

how do you get current userid in wordpress

Hi have a simple php file in my wordpress site, where I need the current userid
I have tried the following, but keep getting
Call to undefined function get_current_user_id()
<?php
include '../../mydbfile.php';
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
You need to include wp-load.php to call the global $wpdb
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
This has been tested and works.

How to access Wordpress' global $wpdb from PHP?

I'm trying to execute a query in PHP against a MySQL database. My code is below:
<?php
require_once ("wp-includes/wp-db.php");
global $wpdb;
$myrows = $wpdb->get_results( "SELECT id, name FROM wp_db_posts" );
echo $myrows;
?>
After executing this code, I get a Fatal error: Call to a member function get_results() on null in C:\Apache24\htdocs\wordpress\me.php on line 4
I'm actually new to WordPress and want to know exactly how can I access the $wpdb global variable from PHP. My databses are pre-fixed with wp_db_.
What am I doing wrong? Can anyone help me here?
assuming you script placed in the root directory of WordPress you should load wordpress not access the wp-db.php directly so your code should be like this:
<?php
require_once "wp-load.php";
global $wpdb;
$myrows = $wpdb->get_results( "SELECT ID, post_name FROM {$wpdb->prefix}posts" );
and you can't echo myrows as you will get object not string you should use instead
var_dump() or print_r()

woocommerce get shipping cost from zone id

I am working in woocommerce add order from API. I already added order by using wc_create_order() function. In that, I have added shipping address, billing address, and product details as well. But the problem is I want to add shpping cost as per shipping zone.
Here is screenshot :
However I found class with function called : WC_Shipping_Zones::get_zones().
But it return unarranged (unmanaged) array.
I have state code and then I want to get shipping cost from state code.
However, I have found zone_id.
I have already tried to use WC_Shipping_Zones::get_zone_by('zone_id',$zone_id),WC_Shipping_Zones::get_zone($zone_id);
But none of functions return cost anyhow.
Here is the code to get the zone cost. I hope it helps.
global $post, $woocommerce;
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone ) {
echo $the_zone['zone_name'];
echo "<br/>";
foreach ($the_zone['shipping_methods'] as $value) {
echo $value->cost;
}
echo "<br/>";
}

how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();

wordpress page id in friendly url

How to get the page id in wordpress in the case of friendly url?
Inside the Loop: http://codex.wordpress.org/Function_Reference/the_ID
Outside the Loop: global $wp_query; $id = $wp_query->post->ID;
I'm guessing you're talking about doing it via the Admin interface. Can't you just add it into the custom format textbox using the %post_id% string:
/archives/%post_id%
If you are within the Loop (most of the time meaning within the while(have_posts()) you should be able to use: global $post; $id = $post->ID; to get the ID

Resources