Set product sale price programmatically in WooCommerce 3

前端 未结 3 497
梦如初夏
梦如初夏 2020-12-18 00:50

I have a Woocommerce store set up with various product categories.

I want to apply a 20% discount to all products, which belong to the product category Cuck

3条回答
  •  一个人的身影
    2020-12-18 01:32

    I realiced, you more or less need all of the following filters to make the HTML work out of the box.

    add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
    add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );
    

    And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients if you want to display it correctly for variable products.

    You may find the gist i've created for a client useful

    https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020

提交回复
热议问题