Wp All import prevent price update if product has a specific tag

164 views Asked by At

I have several xml from suppliers that importing and updating via Wp All import I want to prevent price update if the product has the tag "special"

I have tried the function below but the prices are still updating

function prevent_special_product_price_update( $price, $product ) {
    // Check if the product has the "special" tag
    if ( has_term( 'special', 'product_tag', $product->get_id() ) ) {
        // Get the original price before the import
        $original_price = $product->get_regular_price();
        
        // If the product price is changing during the import, prevent the update and keep the original price
        if ( $original_price !== $price ) {
            $price = $original_price;
            
            // Optional: You can add a notice to inform the user about the prevented price update
            wc_add_notice( __( 'Price update is not allowed for products with the "special" tag.', 'your-text-domain' ), 'error' );
        }
    }
    
    return $price;
}
add_filter( 'woocommerce_product_set_price', 'prevent_special_product_price_update', 10, 2 );
0

There are 0 answers