Custom function that get product reviews count in WooCommerce - php

I'm trying to make a function that get the count of product reviews in WooCommerce product page.
I need to use it in another function in a logical operation ... Can't figure out whats wrong.
function reviews_count() {
$id = $product->get_id();
$product = wc_get_product($id);
$count = $product->get_review_count();
return $count;
}

Try the following instead (for single product_pages):
function reviews_count() {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
return $product->get_review_count();
}
Or you can also add the product Id as function argument (to use it in another function) like:
function reviews_count( $product_id ) {
$product = wc_get_product( $product_id );
return $product->get_review_count();
}
So in your other function, you will be able to pass the product Id a bit like:
function my_other_function() {
global $product;
$count = reviews_count( $product->get_id() );
}

Related

Set New Value for Variation Product Stock Quantity in Woocommerce

I need to set the total number of products as stock quantity in variation products and I don't know which hook to use.
I'm currently using the woocommerce_admin_process_product_object hook and I don't know if it's correct, I guess I won't be able to get out of this without help.
Also here I tried wc_update_product_stock and set_stock_quantity as functions, neither of them give any results, so I thought the hook might not be correct.
add_action( 'woocommerce_admin_process_product_object', 'set_new_variation_stock_quantity' );
function set_new_variation_stock_quantity( $product ) {
global $product;
$total = 0;
// Get variation product total quantity
if ( $product->is_type('variable') ) {
foreach ( $product->get_visible_children() as $variationId ) {
$variation = wc_get_product( $variationId );
$total += $variation->get_stock_quantity();
}
}
// Just for variation product
if ( $product->is_type( 'variable' ) ) {
// Try wc_update_product_stock function
$product = wc_update_product_stock( $product, $total, 'set', 'false' );
return $product;
// Try other set_stock_quantity
$product = $product->set_stock_quantity( $product, $total);
$product->save();
return $product;
}
return $product;
}

How can i get product visibility in WooCommerce loop?

How can i get the value of a product visibility ?
I would like to set a conditional display in a loop, based on product visibility if hidden.
Something like :
if($my_product is hidden) {
}
You can simply use the WC_Product method is_visible() on the WC_Product Object like:
global $product;
// Be sure to get the WC_Product instance object
if( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
// Check product visibility
if( ! $product->is_visible() ) {
// Not visible
} else {
// Visible
}
It should work.

How do I hide shipping methods on conditional function in woocomerce, wordpress?

I can't figure out how to get a global variable created and defined in the functions.php file. I'm using WooCommerce (3.7.1) in Wordpress.
I created a function called action_before_cart() and this function checks many a condition if the cart contains X products. In this function I declare a global variable called $shipping and the value after using this function should be: 0 or 1.
Now, depending on the $shipping global variable value, in the hide_shipping_method_based_on_shipping_method function, only some selected shipping methods will be hidden during cart or checkout. I do it by unset each method by unset($rates[$method_key_id]); in a for each loop.
So basically what Im trying to do is getting the global variable $shipping made in the function action_before_cart() and use it inside the hide_shipping_method_based_on_shipping_method function.
add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
$hasOfficePack = false;
$hasSundayCombo = false;
$officePack = array('office pack', '77');
$sundayCombo = array('sunday combo', '85');
// Loop through cart items if there's $officePack or $sundayCombo
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for $officePack category products in cart
if ( has_term( $officePack, 'product_cat', $cart_item['product_id'] ) ) {
$hasOfficePack = true;
}
// Check for $sundayCombo category products in cart
if ( has_term( $sundayCombo, 'product_cat', $cart_item['product_id'] ) ) {
$hasSundayCombo = true;
}
}
global $shipping; //the most important variable, which I want to be saved and used in the next function below
if ( $hasOfficePack ) {
$shipping = 0;
}
if ( $hasSundayCombo ) {
$shipping = 1;
}
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_method', 1, 2 );
function hide_shipping_method_based_on_shipping_method( $rates, $package ) {
global $shipping; //the value of $shipping it's supposed to be set in the previous function
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ($shipping == 0) {
$method_key_ids = array('flat_rate:2', 'free_shipping:8', 'flat_rate:9');
} elseif($shipping == 1) {
$method_key_ids = array('flat_rate:2', 'flat_rate:9');
}
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
return $rates;
}

Display product data with custom shortcodes in Woocommerce single product pages

In Woocommerce, I use 3 different shortcode functions that are displaying:
The product categories list,
The product SKU
Values for a specific attribute.
My 3 function looks like this:
function display_woo_sku() {
global $product;
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
function display_woo_farve() {
global $product;
return $product->get_attribute( 'farve' );
}
add_shortcode( 'woo_farve', 'display_woo_farve' );
function display_woo_style() {
global $post, $product;
$categ = $product->get_categories();
return $categ;
}
add_shortcode( 'woo_style', 'display_woo_style' );
This actually works, but throws errors when updating products and make issues with Facebook Pixel add ons.
I can't see What is wrong with my code, but there must be something wrong since it conflicting with plugins and third party pixel tools.
Any help is appreciated.
Your code is making some errors for many reasons… Try this instead:
function display_woo_sku() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// Get an instance of the WC_Product Object
$product = wc_get_product( $post->ID );
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
function display_woo_attr_farve() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// Get an instance of the WC_Product Object
$product = wc_get_product( $post->ID );
return $product->get_attribute( 'farve' );
}
add_shortcode( 'woo_farve', 'display_woo_attr_farve' );
function display_woo_cats() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// $categ = $product->get_categories(); // <== <== <== <== IS DEPRECATED
return wc_get_product_category_list( $post->ID );
}
add_shortcode( 'woo_cats', 'display_woo_cats' );
This code goes on function.php file of your active child theme (or theme). Tested and works.
It should solve your issue…

How do I get items categories from the WooCommerce cart?

I am writing the function which should check up whether there is in the cart an item, which has a particular category.
My idea was:
add_filter( 'woocommerce_package_rates', 'remove_flat_rate_from_used_products', 10, 2 );
function remove_flat_rate_from_used_products($rates, $package) {
if( is_woocommerce() && ( is_checkout() || is_cart() ) ) {
if( check_whether_item_has_the_category() ) {
unset( $rates['flat_rate'] );
}
}
return $rates;
}
I guessed, the get_cart() function returns the contents of the cart, and I would be able to get information about items categories out there. I need to know the structure of the array get_cart() returns, so I wrote:
function check_whether_item_has_the_category() {
global $woocommerce;
var_dump(WC()->cart->get_cart());
}
And got
Warning: Invalid argument supplied for foreach() in ...wp-content\plugins\woocommerce\includes\class-wc-shipping.php on line 295
Then I tried find the category name in results of the get_cart() function:
function check_whether_item_has_the_category() {
global $woocommerce;
if( in_array('used', WC()->cart->get_cart()) ) {
echo 'do something';
}
}
And got the same error.
The use of $woocommerce instead of WC() give nothing, as well as the remove of global $woocommerce
What am I doing wrong and how do I get items categories (or check them for existing of the particular one)?
The variable $package contains also the cart contents ($package['contents']), that is an array with all the products in cart.
So you can loop through that and see if the single product has the category wanted. To get the categories you can use wp_get_post_terms():
function remove_flat_rate_from_used_products($rates, $package) {
// for each product in cart...
foreach ($package['contents'] as $product) {
// get product categories
$product_cats = wp_get_post_terms( $product['product_id'], 'product_cat', array('fields' => 'names') );
// if it has category_name unset flat rate
if( in_array('category_name', $product_cats) ) {
unset( $rates['flat_rate'] );
break;
}
}
return $rates;
}

Categories