Search code examples
phpwordpressapiwoocommercegetcustomattributes

How to update woocommerce sale price with custom product attribute value?


I'm trying to update the meta "Sale price" with the custom product attribute "value" named "Cost_price" for every product (simple & variable). This custom product attribute is API connected with other site and It will change the value (price) once a week, so the code should be able to change prices in "sale price" when the products custom attribute is updated.

add_filter( 'woocommerce_product_variation_get_price', 'conditional_product_sale_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'conditional_product_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'conditional_product_sale_price', 10, 2 );

add_filter( 'woocommerce_variation_prices_sale_price', 'conditional_product_sale_price', 10, 2 );

global $product;
$new_price = array_shift( wc_get_product_terms( $product->id, 'pa_cost_price', array( 'fields' => 'names' ) ) );

function conditional_product_sale_price( $price, $product ) {
    if( is_admin() ) return $price;
            $price = $new_price;
    }
if( !empty($sale_price) ){
    update_post_meta( $product_id, '_sale_price', $new_price );
}
    return $price;

I look for different variations here, but didn't able to find anything that works. Does anyone know what I'm doing wrong? P.S I'm a total noob for this.


Solution

  • I was looking for an answer in the last two days and find really good posts here about woocommerce custom code variations. Finally, I found the right code for my problem.

    I used a custom field instead of a custom product attribute, because I don't know how to "get custom product attribute value" to this code. This solution suits me as well.

    add_filter('woocommerce_product_get_price', 'custom_cost_price', 10, 2); 
        add_filter('woocommerce_product_get_regular_price', 'custom_cost_price', 10, 2 );
        // Variations
        add_filter('woocommerce_product_variation_get_regular_price', 'custom_cost_price', 10, 2 );
        add_filter('woocommerce_product_variation_get_price', 'custom_cost_price', 10, 2 );
        function custom_cost_price( $price, $product ) {
            if( $product->get_meta('_costprice') );
                $price = $product->get_meta('_costprice');
        
            return $price;
        }
        add_filter('woocommerce_variation_prices_price', 'custom_variable_cost_price', 99, 3 );
        add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_cost_price', 99, 3 );
        function custom_variable_cost_price( $price, $variation, $product ) {
            // Delete product cached price  (if needed)
            // wc_delete_product_transients($variation->get_id());
             if( $product->get_meta('_costprice') );
                $price = $product->get_meta('_costprice');
        
            return $price;
        }
    

    Thanks to @LoicTheAztec, but one thing I didn't do.. Should I add woocommerce_get_variation_prices_hash to allow refresh cached prices?