0

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 Answer 1

0

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.

1
  • If this answer works for you please mark it as accepted. Commented Sep 24, 2020 at 21:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.