I create a custom badge/bubble "New!" based on time published:
add_action( 'woocommerce_before_shop_loop_item', function() {
#add_action( 'woocommerce_single_product_image_html', function() {
$postdate = get_the_time( 'Y-m-d' ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}, 20 );
But i want to add conditional to show if "on sale" is not set.
How should look like?
thanks!
WC_Product::is_on_sale() – Returns whether or not the product is on sale.
function my_callback_function() {
global $product;
$postdate = get_the_date( 'Y-m-d', $product->get_id() ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
$onsale = $product->is_on_sale();
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp && !$onsale ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'my_callback_function', 10, 0 );
Related
I like to display unit prices at variable products. I like it to do based on a product attribute whic contains the following: 5 kg, 2 l etc. So, I get the attribute, extract the numbers from it and do some math. The problem is, that all of the code displays the multipier number of 1. Here is my code:
function product_variation_unit_price( $data, $product, $variation ) {
$price = wc_get_price_to_display( $variation );
$unit = array_shift( wc_get_product_terms( $product->id, 'pa_unit', array( 'fields' => 'names' ) ) );
$unit_number_extracted = preg_replace('/[^0-9]/', '', $unit);
if ( (!empty($unit) ) AND $unit_number_extracted == 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price * $unit_number_extracted) );
} else if( (!empty($unit) ) AND $unit_number_extracted > 1 ) {
$unit_price = sprintf( __( 'Gross unit price: %s', 'loremipsum' ), wc_price($price / $unit_number_extracted) );
}
$data['price_html'] .= '<span> ' . $unit_price . '</span>';
return $data;
}
add_filter( 'woocommerce_available_variation', 'product_variation_unit_price', 10, 3 );
I guess I should foreach the variations, but I don't know how to do that in this situation.
Thanks if you can help!
I can't display crossed price for 0 priced variable products.
That is my code. Where I'm going wrong ?
add_filter( 'woocommerce_get_price_including_tax_html', 'woocommerce_get_regular_price_html', 'woocommerce_get_price_html', 'price_free_zero_empty', 9998, 2 );
function price_free_zero_empty( $price, $product ){
if ( '' === $product->get_price_including_tax() || 0 == $product->get_price_including_tax() ) {
$regular_price = $product->get_price_including_tax();
$price = '<del>' . wc_price( $regular_price ) . '</del> '. '<span class="woocommerce-Price-amount amount">'.__("Free", "woocommerce").'</span>';
}
return $price;
}
function wpglorify_price_free_zero_empty( $price, $product ) {
if ( $product->get_price() == 0 ) {
if ( $product->is_on_sale() && $product->get_regular_price() ) {
$regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
$price = wc_format_price_range( '<del>' . wc_price($regular_price) . '</del>', __( 'Omaggio!', 'woocommerce' ) );
} else {
$price = '<span class="amount">' . __( 'Omaggio!', 'woocommerce' ) . '</span>';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wpglorify_price_free_zero_empty', 10, 2 );
UPD
Solved:
function wc_discount_total_30() {
global $woocommerce;
$discount_total = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ( $_product->is_on_sale() ) {
$regular_price = $_product->get_regular_price();
$sale_price = $_product->get_sale_price();
$discount = ($regular_price - $sale_price) * $values['quantity'];
$discount_total += $discount;
}
}
if ( $woocommerce->cart->subtotal + $woocommerce->cart->shipping_total > $woocommerce->cart->total + $woocommerce->cart->shipping_total || $discount_total >0 ) {
echo '<tr class="cart-discount">
<th>'. __( 'Saved:', 'woocommerce' ) .'</th>
<td data-title=" '. __( 'Saved', 'woocommerce' ) .' ">'
. wc_price( $woocommerce->cart->subtotal - $woocommerce->cart->total + $woocommerce->cart->shipping_total + $discount_total ) .'</td>
</tr>';
}
}
// Hook our values to the Basket and Checkout pages
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total_30', 99);
--------------------------------------------------------------------
ORIGINAL QUESTION:
I want to calculate and display «Total Savings» in Cart and Checkout page (Woocommerce). I've tried to use this code, but it only works if I have at least one discounted item in my cart:
function wc_discount_total_30() {
global $woocommerce;
$discount_total = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ( $_product->is_on_sale() ) {
$regular_price = $_product->get_regular_price();
$sale_price = $_product->get_sale_price();
$discount = ($regular_price - $sale_price) * $values['quantity'];
$discount_total += $discount;
}
}
if ( $discount_total > 0 ) {
echo '<tr class="cart-discount">
<th>'. __( 'Saved', 'tsavedis' ) .'</th>
<td data-title=" '. __( 'Saved', 'tsavedis' ) .' ">'
. wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
</tr>';
}
}
// Hook our values to the Basket and Checkout pages
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total_30', 99);
But I need to calculate the discount amount considering the coupon and the amount of items in the cart (whether there are discounted items in the cart or not). Here is my code for displaying discount based on the number of items in shopping cart:
function woo_discount_total(WC_Cart $cart) {
$woo_count_item = $cart->get_cart_contents_count(); // Number of items in the shopping cart
if($woo_count_item >= 3 && $woo_count_item <= 4) {
$discount = $cart->subtotal * 0.10; // 0.10 — 10%
$cart->add_fee("10% discount for 3-4 items in the shopping cart", -$discount);
} elseif($woo_count_item >= 5 && $woo_count_item <= 100) {
$discount = $cart->subtotal * 0.20; // 0.20 — 20%
$cart->add_fee("20% discount for 5 and more items in the shopping cart", -$discount);
}
}
add_action("woocommerce_cart_calculate_fees" , "woo_discount_total");
What I want to display:
Cart Subtotal: $1000
Coupon: -$50
10% discount for 3 items in Cart: -$100
Order Total: $850
Saved: $150
I think the easiest way to do that is to subtract «order total» from «cart subtotal». Is there any way to do this?
This question already has answers here:
Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4
(1 answer)
Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"
(8 answers)
Closed 3 years ago.
Hello I was using woocommerce_add_order_item_meta to add some additional information for order item. After woocommerce update I got error on ordar pay page. I researched that why it happens. I found this article. and I understood why I'm getting error. I changed action hook name but still get errors. What should I do ?
function addBizDays($item_id, $cart_item ,$start, $add){
$start =current_time( 'mysql' );
$add = get_post_meta( $cart_item[ 'product_id' ], 'dp_kargolanma_suresi', true );
$d = new DateTime($start );
$t = $d->getTimestamp();
// loop for X days
for($i=0; $i<$add; $i++){
// add 1 day to timestamp
$addDay = 86400;
// get what day it is next day
$nextDay = date('w', ($t+$addDay));
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t+$addDay;
}
$d->setTimestamp($t);
$teslim_tarihi = $d->format( 'd-m-Y' ). "\n";
$desiveyakg = ( $cart_item['quantity'] * $cart_item['data']->get_weight());
$kargosinifi = $cart_item['data']->get_shipping_class();
wc_update_order_item_meta( $item_id, 'En Gec Kargolanma Tarihi', $teslim_tarihi );
wc_update_order_item_meta( $item_id, '_desi_kg', $desiveyakg );
wc_update_order_item_meta( $item_id, '_alici_onay_durumu', 'Onay Bekliyor' );
if($cart_item['quantity'] >= 100)
{
wc_update_order_item_meta( $item_id, '_kargo_sinifi', 'ucretsiz-kargo' );
}
else{
wc_update_order_item_meta( $item_id, '_kargo_sinifi', $kargosinifi );
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'addBizDays', 10, 5 );
You need to change more than just the hook… The hooked function arguments are wrong in your code and the way to updated data has changed too. Try the following:
add_action( 'woocommerce_checkout_create_order_line_item', 'add_business_days', 10, 4 );
function add_business_days( $order_item, $cart_item_key, $cart_item, $order ){
$now = current_time( 'mysql' );
$add = $cart_item['data']->get_meta( 'dp_kargolanma_suresi' );
$d = new DateTime( $now );
$t = $d->getTimestamp();
$oneDay = 86400;
$nextDay = date('w', ($t + $oneDay));
// loop for X days
for( $i = 0; $i < $add; $i++ ) {
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t + $oneDay;
}
$d->setTimestamp($t);
$teslim_tarihi = $d->format( 'd-m-Y' ). "\n";
$desiveyakg = ( $cart_item['quantity'] * $cart_item['data']->get_weight() );
$kargosinifi = $cart_item['data']->get_shipping_class();
$order_item->update_meta_data( 'En Gec Kargolanma Tarihi', $teslim_tarihi );
$order_item->update_meta_data( '_desi_kg', $desiveyakg );
$order_item->update_meta_data( '_alici_onay_durumu', 'Onay Bekliyor' );
if($cart_item['quantity'] >= 100) {
$order_item->update_meta_data( '_kargo_sinifi', 'ucretsiz-kargo' );
} else {
$order_item->update_meta_data( '_kargo_sinifi', $kargosinifi );
}
}
It should work…
In product page to get input from customer as number of words. To calculate the amount from products add-ons fixed amount to additional 10$ for each above 500 words.
ex: 1. Number of Words: 530
Normal Price + additional price: 180$ + 10$
Total: 190$
Number of Words: 1040
Normal Price + additional price: 180$ + 20$
Total: 200$
This process as to create dynamic input form customer to calculate amount the price and total amount.
`$extra_amount = (int)'10';
$amount = (int)'180'; // how to get amount from woocommerce data
if(isset($_POST['wordnumber'])){ // how to get this paramater form woocommerce post values
$test =$_POST['wordnumber'];
$original_amount = '';
if($test <= (int)'500'){
$original_amount = $amount;
}
elseif($test > (int)'500'){
$div_amount = $test/(int)'500';
$round = floor($div_amount);
//echo '<br/>';
$total_extra = $round*$extra_amount;
$original_amount = $amount+$total_extra;
}
echo $original_amount;
}`
I installed WC Fields Factory plugin then i got value from one field using key. in that i written a function and i overrided the price value.
function calculate_gift_wrap_fee( $cart_object ) {
/* Gift wrap price */
$additionalPrice = 10;
foreach ( $cart_object->cart_contents as $key => $value ) {
$test = $value['custom field'];
if( $test <= 100 ) {
$quantity = floatval( $value['quantity'] );
$orgPrice = floatval( $value['data']->price );
$value['data']->price = ( $orgPrice );
}
elseif( $test > 100 ) {
$div_amount = floatval($test/500);
$round = floor($div_amount);
$total_extra = floatval($round * $additionalPrice);
$quantity = floatval( $value['quantity'] );
$orgPrice = floatval( $value['data']->price );
$pp = ($value['price']);
$total = floatval($pp + $total_extra);
$value['data']->price = ( $orgPrice + $total );
}
}}
add_action( 'woocommerce_before_calculate_totals', calculate_gift_wrap_fee', 1, 1 );