milenmk
milenmk

Reputation: 548

Woocommerce price output on product page

I need to mod a plugin for my needs. Plugin code is as follow:

1. update_post_meta($post_id, '_regular_price', (isset($product['price']) && !empty($product['price'])) ? $product['price'] : '');
2. update_post_meta($post_id, '_sale_price', (isset($product['sale-price']) && !empty($product['sale-price']) && ($product['sale-price']<$product['price']) ) ? $product['sale-price'] : '');
3. update_post_meta($post_id, '_price', (isset($product['price'])) ? $product['price'] : '');

First two rows input the regular price and discount (sale) price values into the database. The third row is the output on the product page, which I need to change so:

  1. If discounted price _sale_price / [sale-price] is present it should be shown as default _price / [price] on row 3.
  2. If discount price _sale_price / [sale-price] is not present the normal price from row 1 should be shown as [price] on row 3

I've tried if (isset($product['sale-price'])){? $product['sale-price'] : '';}else{? $product['price'] : '';} but this throws 500 error.

Any help appreciated.

Upvotes: 1

Views: 283

Answers (2)

milenmk
milenmk

Reputation: 548

My final working code is as follow:

update_post_meta($post_id, '_regular_price', (isset($product['price']) && !empty($product['price'])) ? $product['price'] : '');

update_post_meta($post_id, '_sale_price', (isset($product['sale-price']) && !empty($product['sale-price']) && ($product['sale-price']<$product['price'])) ? $product['sale-price'] : ((isset($product['retail-price']) && !empty($product['retail-price']) && ($product['retail-price']<$product['price'])) ? $product['retail-price'] : '')); 

update_post_meta($post_id, '_price', (isset($product['sale-price']) && !empty($product['sale-price'])) ? $product['sale-price'] : ((isset($product['retail-price']) && !empty($product['retail-price'])) ? $product['retail-price'] : $product['price']));

Thanks to Frits for the help. :)

Upvotes: 1

Frits
Frits

Reputation: 7624

WooCommerce should be doing this automatically, but in the interest of answering your question, there are some errors in your code, it seems like you are trying to mix a ternery operator with an IF statement.

The correct replacement for line 3 should look like this:

update_post_meta($post_id, '_price', (isset($product['sale-price']) && !empty($product['sale-price'])) ? $product['sale-price'] : $product['price']);

Upvotes: 1

Related Questions