WooCommerce如何程序化地更新可变产品价格?

时间:2017-05-13 作者:Mohammad Shahnewaz Sarker

我想通过代码更新woocommerce可变产品价格。我在这里写了一些代码,它创建产品,但不更新可变价格:

$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 个回复
SO网友:hwl

我相信你想要woocommerce_variable_price_html 过滤器挂钩。但这里的官方文件中列出了几个与价格相关的过滤器:WooCommerce Api Docs: Hooks

woocommerce_variable_price_html 在3.0中,定义在wc类产品变量中。phphttps://docs.woocommerce.com/wc-apidocs/source-class-WC_Product_Variable.html#145

将其与以下内容挂钩:

add_filter( \'woocommerce_variable_price_html\', \'NAME_OF_YOUR_FUNCTION\', 10, 2 );
这里有一些Additional info on Filter Hooks in Wordpress

还有一个woocommerce_variation_prices 应用于wc类产品变量数据存储cpt第221行的过滤器。phphttps://docs.woocommerce.com/wc-apidocs/source-class-WC_Product_Variable_Data_Store_CPT.html#221

结束

相关推荐