php - Woocommerce - How to get product data

I am trying to get the product data for a plug in I am modifying but the$product
variable always seems to be null. in other functions it returns an array of product data. I have tried many solutions but cannot figure it out.
Any help would be appreciated.
if ( ! function_exists( 'wmc_get_price' ) ) {
function wmc_get_price( $price, $currency_code = false, $is_shipping = false ) {
if ( is_admin() && ! is_ajax() ) {
return $price;
}
$setting = WOOMULTI_CURRENCY_Data::get_ins();
$allow_multi_pay = $setting->get_enable_multi_payment();
$equivalent_currency = $setting->get_param( 'equivalent_currency' );
if ( isset( $price ) ) {
$price = (float) str_replace( ',', '.', $price );
}
echo $product;
if ( ! $allow_multi_pay && is_checkout() && ! $equivalent_currency ) {
return $price;
}
/*Check currency*/
$selected_currencies = $setting->get_list_currencies();
$current_currency = $setting->get_current_currency();
if ( ! $current_currency ) {
return $price;
}
if ( $price ) {
// IF product === nameyourprice {$price = $price;}
if ( ywcnp_product_is_name_your_price( $product ) ) {
$price = $price;
$price = $is_shipping ? $price : apply_filters( 'wmc_get_price', $price, $currency_code );
}
elseif ( $currency_code && isset( $selected_currencies[ $currency_code ] ) ) {
$price = $price * (float) $selected_currencies[ $currency_code ]['rate'];
$price = $is_shipping ? $price : apply_filters( 'wmc_get_price', $price, $currency_code );
// $price = apply_filters( 'wmc_get_price', $price, $currency_code );
} else {
$price = $price * (float) $selected_currencies[ $current_currency ]['rate'];
$price = $is_shipping ? $price : apply_filters( 'wmc_get_price', $price, $current_currency );
// $price = apply_filters( 'wmc_get_price', $price, $current_currency );
}
}
return (float) $price; //(float)
}
}
Answer
Solution:
Update
I fixed it by wrapping the function inadd_action( 'woocommerce_init', 'wc_init' ); function wc_init(){}
This allowed me to retrieve thewc_get_price()
function and the$product
variable.
Just figuring out Wordpress include system.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.