I want to update woocommerce variable product price by code. Here I wrote some code,, its create product but not update variable price:

$post = array(

    'post_title'   => 'Product Title',

    'post_content' => '',

    'post_status'  => 'publish',

    'post_type'    => "product"

);

$new_post_id = wp_insert_post( $post );

wp_set_object_terms ($new_post_id,'variable','product_type');



/**

 * Add product attribute.

 */

$attr_names = array(

    'Length'      => array( '12', '13','14','15' )

);

$attr_data = array();

foreach ( $attr_names as $attr_name => $attr_values ) {

    $attr_sanitized_name="pa_" . sanitize_title( $attr_name );

    $attr_data += array(

        $attr_sanitized_name => array(

            'name'          => $attr_name,

            'value'         => implode( '|', $attr_values ),

            'is_visible'    => 1,

            'is_variation'  => 1,

            'is_taxonomy'   => 0,

            'position'      => 0,

        )

    );

}

$variation_id = update_post_meta( $new_post_id, '_product_attributes', $attr_data, TRUE );

update_post_meta($variation_id, '_regular_price', '100');

1 Answer
1

I believe you want the woocommerce_variable_price_html filter hook. But there are several price related filters listed in the official docs here:
WooCommerce Api Docs: Hooks

woocommerce_variable_price_html in 3.0 is defined in /class-wc-product-variable.php
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Product_Variable.html#145

Hooking it with something like:

add_filter( 'woocommerce_variable_price_html', 'NAME_OF_YOUR_FUNCTION', 10, 2 );

Here is some Additional info on Filter Hooks in WordPress


There is also a woocommerce_variation_prices filter applied at line 221 of
class-wc-product-variable-data-store-cpt.php
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Product_Variable_Data_Store_CPT.html#221

Leave a Reply

Your email address will not be published. Required fields are marked *