Search code examples
phpwordpressobjectwoocommerceinstance

WooCommerce issue: Object of class WC_Product_Variation could not be converted to int


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.


Solution

  • On your first code you can't get the right product variation ID from:

    // 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];
    

    as this doesn't give a variation id that you could use in:

    // Then I can get the right variation this way $prod
    $product_variation = new WC_Product_Variation($default_variation_id);
    

    Instead use the following (as you have only two variations for the parent variable product):

    // Getting all variations IDs of the variable product
    $children_ids = $product->get_children();
    
    // Get first WC_Product_Variation instance Object
    $variation = wc_get_product(reset($children_ids));
    
    // Check if specific product attribute value is set on the first variation
    if( $variation->get_attribute("unite-de-commande") !== "Kg" ) {
        // If not we get the 2nd WC_Product_Variation instance Object
        $variation = wc_get_product($children[1]);
    }
    
    // I can finally get the regular price of the right product variation
    $regular_price = $variation->get_regular_price();
    

    It should better work now.


    On your 2nd code snippet there is a problem at this line:

    $product_0 = wc_get_product( $product_var_0_id );
    

    as $product_var_0_id is not an expected product variation Id but a WC_Product_Variation() object instance.

    Also you can use WC_Product methods instead, and the correct code should be:

    $children_ids        = $product->get_children();
    $fist_variation      = wc_get_product( reset($children_ids) );
    $fist_attr_udc_value = $fist_variation->get_attribute( 'unite-de-commande' );
    
    if ( isset( $_REQUEST['_prix_variable_'. $fist_attr_udc_value] ) && $fist_attr_udc_value === "Kg") {
        $first_variable_price = esc_attr($_REQUEST['_prix_variable_'. $fist_attr_udc_value]);
    
        // Updating active price and regular price
        $fist_variation->set_regular_price( $first_variable_price );
        $fist_variation->set_price( $first_variable_price );
        $fist_variation->save() // Save data and Clear/refresh caches and transients
    }
    

    This 2nd code snippet should work.