I built a plugin to access and modify some products fields with the Quick Edit of Woocommerce. In the shop I have Simple
and Variable
products. Each Variable
product has two attribute: Kg
and Pièce
.
What are the different fields ?
There are two fields that get the regular price
of both variations of each Variable
product.
I also need one custom field
known as _number_field
that simply is the same information as one of the variations. This custom field gets the regular price of one of the variation (which has the attribute "Kg").
What do I need ?
For the custom field
, I need to access the regular_price
of one of the two available variations of each Variable
product.
In order to access the regular price of this variation, I create a new WC_Product_Variation
class with the right (already created) variation ID.
I also need to get each variation's regular_price
.
The code to get the custom field
The following code (only part of the full code) is sent via the action woocommerce_product_options_general_product_data
.
// Getting both variations IDs of the product
$children = $product->get_children();
// I'm setting the default variation to the first variation ID
$default_variation_id = $children[0];
// I'm creating a first variation with the first variation ID in order to get it's attribute
$product_variation = new WC_Product_Variation($default_variation_id);
// I can now compare it's attribute with the string "Kg".
// If it is true that means that the first variation ID has Kg has an attribute
// Otherwise I want the second variation, with has the string "Pièce" has an attribute
$product_variation->get_attribute("unite-de-commande") === "Kg" ? $default_variation_id = $children[0] : $default_variation_id = $children[1];
// Then I can get the right variation this way
$product_variation = new WC_Product_Variation($default_variation_id);
// I can finally get the regular price of this product variation
$product_default_kg = $product_variation->get_regular_price();
The issue I'm getting
The issue is that the values in the quick edit fields do not save, except for the first value which is the only one I access with the new WC_Product_Variation
class.
In the logs, I can see the current error: Object of class WC_Product_Variation could not be converted to int
.
The other two values are not accessed with this particular class. Here's an excerpt of the code I use to save the other two fields. This only includes one of the variations.
The code to get one of the variation's regular price
$variations = $product->get_children();
$product_var_0_id = wc_get_product( $variations[0] );
$product_0 = wc_get_product( $product_var_0_id );
$product_0_def_attr = $product_0->get_attribute( 'unite-de-commande' );
if ( isset( $_REQUEST['_prix_variable_'. $product_0_def_attr] ) && $product_0_def_attr === "Kg") {
$prix_variable_0 = $_REQUEST['_prix_variable_'. $product_0_def_attr];
// Updating active price and regular price
update_post_meta( $product_var_0_id, '_regular_price', $prix_variable_0 );
update_post_meta( $product_var_0_id, '_price', $prix_variable_0 );
wc_delete_product_transients( $product_var_0_id ); // Clear/refresh the variation cache
}
Could you help me understand why I'm getting this error ?
Thank you.