I have this code to display field order total amount on the order edit page:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
$method = get_post_meta( $order->id, '_payment_method', true );
if($method != 'pec_gateway_check')
return;
echo date('Y/m/d', strtotime("+40 days")) . " تاریخ چک صیادی";
}
I want add below code to show total amount in this page:
How to do that?
global $woocommerce;
$carttotal = $woocommerce->cart->total;
$total35 = $carttotal*0.35;
$total65 = $carttotal*0.65;
echo number_format($total35) . " ریال پرداخت شده";
echo number_format($total65) . " ریال مبلغ چک پرداختی";
From your question, it sounds like you just need to add the code you've written to the function you already have. So something like this:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
global $woocommerce;
$method = get_post_meta( $order->id, '_payment_method', true );
if($method != 'pec_gateway_check')
return;
echo date('Y/m/d', strtotime("+40 days")) . " تاریخ چک صیادی";
$carttotal = $woocommerce->cart->total;
$total35 = $carttotal*0.35;
$total65 = $carttotal*0.65;
echo number_format($total35) . " ریال پرداخت شده";
echo number_format($total65) . " ریال مبلغ چک پرداختی";
}
Related
I am trying the update total stock of the product when stock for any variation changes. I want to keep the total stock of all the variations updated.
Here is the code I am using but it's not working. What could be the reason.
add_action( 'woocommerce_product_set_stock', 'wc_get_variable_product_stock_quantity' );
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
set_stock_quantity( $stock_quantity );
}
}
I have this code to display the price.
function edit_selected_variation_price( $data, $product, $variation ) {
$price = $variation->price;
$price_incl_tax = $price + round($price * ( 23 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '<div><span class="price">';
$display_price .= '<span>Cena netto</span><span class="amount">' . $price .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</div><div>';
$display_price .= '<span>Cena brutto</span><span class="amount">' . $price_incl_tax .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</span></div>';
$data['price_html'] = $display_price;
return $data;
}
add_filter( 'woocommerce_available_variation', 'edit_selected_variation_price', 10, 3);
How to get the maximum price displayed without choosing any variants?
I do not fully understand your question, you want the max price from what prices (incl tax and ...)?
However, when you have different prices in an array (or as loose variables), consider using the PHP max() function:
https://www.php.net/manual/en/function.max.php
That will return the highest value (Hope that's what you're after)
I would like to replace the name of the product in the shopping cart with two selected variants. Ideally in this format:
Variant 1 - Variant 2
With the following code it works, but only shows the name of the first variant.
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
$new_name = wc_gzd_get_product( $product )->get_attribute('pa_attribute3';
if( method_exists( $product, 'set_name' ) )
$product->set_name( $new_name );
else
$product->post->post_title = $new_name;
}
}
Does anyone find the bug or have a way how it works?
Thanks a lot!
I've solved it.
I take the code above to use attribute 3 as the item name and add attribute 1 and 2 with this code:
function show_things_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$att1 = $product->get_attribute('pa_attribute1');
$att2 = $product->get_attribute('pa_attribute2');
{
$item_name = '<class="product-model">' . $att1 . __( " ", "woocommerce") . $att2 . __( " - ", "woocommerce") . $item_name;
}
return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'show_things_in_cart_items', 99, 3 );
Thanks to User LoicTheAztec for the base of this code, i just modified it for my needs.
I found it here: https://wordpress.stackexchange.com/questions/348631/adding-product-sku-before-cart-item-name-in-woocommerce
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 );
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…