Jump to content
Latest News: (loading..)
Psytanium

Detect if a product is special

Recommended Posts

Hello,

I have modified the calculate_price function in class/currencies.php to add a discount in some countries, it is working fine, but I want to exclude the discount if the product already have already a special price.

How can I add "if product is special" to this function ?

function calculate_price( $products_price, $products_tax, $quantity = 1) {
		global $currency, $country, $country_vat_disc;
		
		if (!in_array($country, $country_vat_disc)) {
			$discount = tep_add_tax( $products_price, $products_tax ) * 0.1;
		}

		return tep_round( tep_add_tax( $products_price, $products_tax ) - $discount, $this->currencies[ $currency ][ 'decimal_places' ] ) * $quantity;
	}

 

I tried this

function calculate_price( $products_price, $products_tax, $quantity = 1) {
		global $currency, $country, $country_vat_disc, $product_id;
		
		if( tep_get_products_special_price( $product_id ) !== null ) {
			$ifspecial = true;
		}
		
		if (!in_array($country, $country_vat_disc) && !$ifspecial) {
			$discount = tep_add_tax( $products_price, $products_tax ) * 0.1;
		} else {
			$discount = 0;
		}

		return tep_round( tep_add_tax( $products_price, $products_tax ) - $discount, $this->currencies[ $currency ][ 'decimal_places' ] ) * $quantity;
	}

but didn't work.

Thank you for any suggestion :)

Share this post


Link to post
Share on other sites

Have you confirmed that tep_get_products_special_price() returns a null and not a 0 or other unexpected result if the product does not have a special price? I'm always a bit leery of code that doesn't explicitly return a null, but relies on something of a side-effect of something not being set (possibly NULL default) in the database. See if == null or != null might be needed, rather than !== or === comparisons. When you say "it didn't work", that means that $ifspecial is never being set true, even when it should be? You might want to check if you need a different comparison for setting $ifspecial. That would be my best guess for where the problem is.


If you are running the "official" osC 2.3.4 or 2.3.4.1 download, your installation is obsolete! Get the latest community-supported responsive "Edge" release

Share this post


Link to post
Share on other sites

@MrPhil Thank you for your support on almost all topics :)

I tried if ( !empty(tep_get_products_special_price( $product_id )) ...

I think the variable $product_id is not passed to the function.

tep_get_products_special_price( $product_id )

I don't know if there is a ready to use function, to test if a product is special.

 

Share this post


Link to post
Share on other sites

in product_info.php i added

$product_id = $product_info[ 'products_id' ];

now the function is working.

But this solution require adding $product_id  in every page of the website, product_listing.php, shopping_cart, checkout pages, etc...

I don't know if there is easier solution, to test specials, without modifying every page.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×