Any way to Overwrite get_stock_quantity in my functions.php?
Asked Answered
P

3

5

I am working in Wordpress Multisite and trying to ensure that all the stock info is fetched from the base site tables. I am trying to overwrite get_stock_quantity() woocomerce function in my theme's functions.php. What i found was

public function get_stock_quantity( $context = 'view' ) {
    return $this->get_prop( 'stock_quantity', $context );
}

Now this is neither a filter nor an action. I overwrote one filter that is

add_filter( 'woocommerce_product_get_stock_quantity', 'wcs_custom_get_stock_quantity', 1, 2);

function wcs_custom_get_stock_quantity( $availability, $_product ) {
    global $wpdb;
    $productQuantity = $wpdb->get_results( 'SELECT * FROM '.$wpdb->base_prefix.'postmeta WHERE post_id = '.$_product->get_id() ." AND meta_key = '_stock'", OBJECT );
    return $productQuantity[0]->meta_value;
}

But woocommerce_product_get_stock_quantity only works on the Products List page. The individual product-edit page uses get_stock_quantity.

How can I overwrite it without changing Core files? Thanks

Pish answered 2/12, 2017 at 18:41 Comment(0)
E
11

The correct way to alter get_stock_quantity() method depends on the product. AS you have already notice looking at the source code you see that:

return $this->get_prop( 'stock_quantity', $context );

Now get_prop() method is a part of WC_Data class and has a generic filter hook:

$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );

And if you look to get_hook_prefix() method you have this:

return 'woocommerce_' . $this->object_type . '_get_';

For product variations the object_type is: product_variation
For other products the object_type is: product
The $prop argument is stock_quantity

So know you can built the related filter hooks. Try this:

add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
function custom_get_stock_quantity( $value, $product ) {
    $value = 15; // <== Just for testing
    return $value;
}

So for product variations you use woocommerce_product_variation_get_stock_quantity filter hook.
And for the other cases woocommerce_product_get_stock_quantity filter hook

Embroil answered 3/12, 2017 at 18:25 Comment(3)
ok I did that but i am facing same issue. The filters you wrote do exactly what mine was doing but i need to overwrite the stock quantity function of "Edit Product" page. At the moment, on products list table I am able to get the base site's stock quantity but on edit product page it shows the stock of its own table (Subsite Table) rather than fetching it from the base site's table.Pish
@Pish facing the same problem, looks like there is no chance hook in before to change the $contect to 'edit'.Streamway
@anibal Sorry but this is the WooCommerce official composite hook for WC_Product method get_stock_quantity() and it still work perfectly on last WooCommerce version. Note that this hook works only on "view" context for front end, but not as "edit" context in backend.Embroil
W
0

You can update the database, but remember this code is executed one time each time you list your products, and when you update your product


    add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
    function custom_get_stock_quantity( $value, $product ) {
        $value = 15; // <== Just for testing
        //update dabatabase:
        update_post_meta( $product->get_id(), '_stock', $value );
        return $value;
    }

Wafd answered 30/6, 2020 at 1:36 Comment(0)
V
0

From what I understand you can use $product->set_stock($value); as well to set the value in database

    add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
function custom_get_stock_quantity( $value, $product ) {
    $value = 15; // <== Just for testing
    //update dabatabase:
   $product->set_stock($value);
    return $value;
}
Vaporific answered 19/8, 2021 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.