Getting product stock quantity isn't working - wordpress

On single product page I'm trying to get stock quantity or atleast stock status from woocommerce product but it seems not working properly, even if it's simple product (manage stock status is checked) or variable product the button is not showing. And strange thing happens - I've got Yith Product Bundle plugin and even if every item is in stock it shows that button but whatever, that's not main problem.
So here is code I'm using
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
$stockQ=$product->get_stock_quantity();
if ( $stockQ < 1 ) {
echo "<button>Hello</button>";
}
}
I've also tried but completely nothing happens.
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
if ( ! $product->is_in_stock() ) {
echo "<button>Hello</button>";
}
}

First check if the hook is firing properly.
add_action( 'woocommerce_before_add_to_cart_button', 'show_wishlist' );
function show_wishlist(){
echo '<p>Some custom text before</p>';
}
If you are able to confirm that the hook is working then use the below code.
function show_wishlist() {
global $product;
// Change In Stock Text
if ( $product->managing_stock() && $product->is_in_stock() ) {
//add your code here for Products in Stock
}
else{
//add your code here for Products out of Stock
}
}

Related

In WooCommerce, Is it possible to display specific country only if cart total is over 100?

I have tried to do it using a filter hook called "woocommerce_countries". I tried getting the cart total in this hook but it's not working. Is anyone have any idea on this OR suggest any hook?
Please try with this code. WooCommerce 3.6.2, they have removed the ability to check the cart data. But cart session is accessible right now so we use this function WC()->session->cart
add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
function products_disable_country( $countries ) {
$cartdata = WC()->session->cart;
if (is_array($cartdata) && !empty($cartdata)) {
foreach ($cartdata as $itemdata) {
$sum += $itemdata['line_total'];
}
if ($sum >= 100) {
unset($countries["CA"]);
}
}
return $countries;
}

How to override the category template on the /shop/ page when showing filtered products?

I already created a function that will filter products using URL parameters and display them on the /shop/ page. That works fine.
But, that only works if the products template is set on the WooCommerce /shop/ page.
In my case the requirement is to have the category list set as default template on the /shop/ page. That means I need to override that template and show the filtered product list, when I run a product query with URL parameters.
(eg. example.com/shop/?param1=foo&param2=bar)
I tried following code (and some iterations), but I'm stuck and have no clue how to override the default template when a product filter is applied:
add_filter( 'woocommerce_locate_template', [$this, 'my_include_template_function'], 10, 3 );
public function my_change_template_function($template, $template_name, $template_path)
{
if (!empty($_GET['param1']) && is_shop() && $template_name == 'content-product_cat.php') {
return wc_get_template_part('content', 'product');
} else {
return $template;
}
}
So the source of the categories loop is added in to the loop start [via a filter][1]
add_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
and the woocommerce_maybe_short_product_subcategories() function changes the output based on the:
$display_type = woocommerce_get_loop_display_mode();
However, the woocommerce_get_loop_display_mode() result isn't filterable directly.
But it relies on some values from get_option() (since the setting is in the customizer) and get_option() is filterable via option_$option
So a potential way to tell WooCommerce not to display the categories when you have a particular $_GET parameter, you could filter the shop display mode option to be products when that parameter is detected in the URL. Like this:
/**
* Filter shop display type.
*
* #param string $value - 'products' | 'subcategories' | 'both'
* #return string
*/
function kia_woocommerce_shop_page_display( $value ) {
if ( ! empty( $_GET['param1'] ) {
$value = 'products';
}
return $value;
}
add_filter( 'option_woocommerce_shop_page_display', 'kia_woocommerce_shop_page_display' );

Woocommerce Export Parent Products

for my woocommerce e-shop i use WpAllImport plugin for importing my products with their variations. Typically one product with the variations is shown below
The parent product has no price/sale price values and the price/sale price values are all the same for all variations.
Now i want to export an xml file with only my parents products with WpAllExport plugin. As there is no price/sale price values the tag <price> </price> and <sale price> </sale price>is return empty with no values.
I realise that i need php function for the export to pass any variation price/sale price to parent product price fields.
Does someone help me with the php function i have to write?
Thank you in advance
OK, finally I add the following code and it works fine.
<?php
function get_regular_price_for_xml($parent, $price)
{
if ($parent == "0") {
global $product;
$product_variations = $product->get_available_variations();
if (!empty($product_variations) && is_array($product_variations)) {
$variation_product_id = $product_variations[0]['variation_id'];
$variation_product = new WC_Product_Variation($variation_product_id);
return $variation_product->regular_price;
} else {
return $price;
}
} else {
return $price;
}
}

Woocommerce display product variations

Im having a issue with function:
I need to display product variations (not parent) on category page. -
Let's say:
If is category "t-shirts" I need to display all product variations with attribute "t-shirts".
Im using this function but it's not working:
add_action( 'woocommerce_product_query', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $product ) {
if (is_tax('pa_tshirts') || is_tax('pa_shirts') || is_product_category('summer')) {
$product->set('post_type', 'product_variation');
}
}
Hope anyone can help.
Thanks

Is there a way to just show stock on a single product when a user is logged in

I'm trying to just show stock on a single product when a user is logged in. I've tried manipulating two different scripts I've found, but it's not working. Please help!
function show_stock() {
global $product;
if ( $product->get_stock_quantity() ) { // if manage stock is enabled
if ( number_format($product->get_stock_quantity(),0,'','') < 3 ) { // if stock is low
echo '<div class="remaining">Only ' . number_format($product->get_stock_quantity(),0,'','') . ' left in stock!</div>';
} else {
echo '<div class="remaining">' . number_format($product->get_stock_quantity(),0,'','') . ' left in stock</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
add_action( 'init', 'hide_stock_not_logged_in' );
function hide_stock_logged_in() {
if ( !is_user_logged_in() ) {
remove_action('woocommerce_after_shop_loop_item','show_stock', 10);
}
}
You may,
1- Use wp_footer hook, which is called every page load
2- Check if user logged in inside this hook
3- If no, just make "display:none" for css attributes (class or id) of stock item.
Example:
function hide_stock_if_user_not_logged_in()
{
if ( !is_user_logged_in() )
{
echo "<style>p.stock.in-stock { display: none }</style>";
}
}
add_action( 'wp_footer', 'hide_stock_if_user_not_logged_in' );
You may need to change the "style" part. Tested and works fine. I hope this will help you.
#MrEbabi 's solution is perfect but as an alternative you could also just use css like so:
.logged_in .remaining{
display:none;
}

Resources