Insert a post meta in the product with variable

168 views Asked by At

I state that I am not an expert! My function modifies the visibility of the product based on the presence of the price. It works well with simple products, but not with products that have variables. To be more precise, the visibility of the variable is changed in the variable product, but not the parent product.

My question is: how do I, in case of variables, set the visibility filter on the parent product? This is my current code:

if ($listinoWeb == "")    
{
    delete_post_meta($product_id, 'wwpp_product_wholesale_visibility_filter', 'all');
    add_post_meta($product_id, 'wwpp_product_wholesale_visibility_filter', 'wholesale_customer', TRUE);
}
else
{
    add_post_meta($product_id, 'wwpp_product_wholesale_visibility_filter', 'all', TRUE);
    delete_post_meta($product_id, 'wwpp_product_wholesale_visibility_filter', 'wholesale_customer');
}
1

There are 1 answers

1
Terminator-Barbapapa On

You can call the get_parent_id() function on the product object. Which will return 0 if you call it on a simple/parent product. So you can do something like this:

$product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();

This way you're sure the $product_id variable always contains the parent id.