Mario Muti
Mario Muti

Reputation: 21

How to pass product information to woocommerce plugin?

I try to develop a woocommerce plugin. I need to change product price and product availability

I'm using woocommerce_before_single_product hook but i don't understand how the hook can pass the id product to my function.

Here is my code

add_action('woocommerce_before_single_product', 'call_ws_for_as400', 10 , 1);

function call_ws_for_as400() {

    $product = wc_get_product( $context['post'] -> ID );

    $id = $product->id;

    error_log($product);

    error_log($id);
}

product and id is always empty, why?

Upvotes: 2

Views: 3432

Answers (1)

helgatheviking
helgatheviking

Reputation: 26319

No parameter is passed to the woocommerce_before_single_product hook:

do_action( 'woocommerce_before_single_product' );

So you need to use the global $product:

add_action('woocommerce_before_single_product', 'call_ws_for_as400');

function call_ws_for_as400() {
    global $product;
    $id = $product->get_id();
    error_log($product);
    error_log($id);
}

Upvotes: 6

Related Questions