I am currently trying to make an if else check in the checkout of a WooCommerce site.
I need to know if the total is greater or less than 100, so that it will say "You need to call to negotiate a shipping fee".
The code:
$woocommerce->cart->get_cart_total()
shows the value, but adds HTML content.
I need only the value itself.
I kind of found out how to do this the hard way.
I searched google for over 2 hours and found this page: https://woocommerce.wp-a2z.org/oik_api/wc_cartget_cart_subtotal/
global $woocommerce;
$subtotal = $woocommerce->cart->get_subtotal();
$subtax = $woocommerce->cart->get_subtotal_tax();
$subtotaltax = $subtotal+$subtax;
echo with $subtotaltax shows the value with the tax added.
Related
Does anyone have (or know of) a PHP code snippet solution to add automation say a checkbox to increase stock by quote list or bring back the original increase stock button in the order page; before sending out a quote ?
I have searched many days and hours for a code solution or a Plugin => Nothing.
I believe WooCommerce once used to have this button in the orders page but it is now not there, I am on Woo5.6.0, WP5.8 and PHP7.4
Thanks in advance.
So.. After re-posting my question again and reducing it to simplify and add focus, after I think a robot response closed my original question.
I looked at the suggestions that came up and "would you believe it" the post below was in the suggestion list.. I had not seen this before.
Increase stock (not decrease) after completed WooCommerce orders?
I had to test and change the call adapt the code a little to fit my store model..
I now have a working safe way of targeting only the stock from a quote list and only elevating the stock by the quote list amount (quantity) when I send out a quote.
The only downside to this is if another client comes along and sees the commission in stock they could place it in their basket and then it would not match the original quote sent out.
"Code to develop another time to find a way to reserve that elevated and allocated stock".
I did this after thinking about how to only trigger an action for a sent quote.
So a deep dive into Yiths Request a quote code to find the class where it changed the WooCommerce order status and came up with this.
Thanks to #loictheaztec he was responsible for this inspiration.
// Increase stock on pending quote order status - An experiment "ywraq-pending"
add_action( 'woocommerce_order_status_ywraq-pending', 'action_on_quote_sent' , 10, 1 );
function action_on_quote_sent( $order_id )
{
// Get an instance of the order object
$order = wc_get_order( $order_id );
// Iterating though each order items
foreach ( $order->get_items() as $item_id => $item_values ) {
// Item quantity
$item_qty = $item_values['qty'];
// getting the product ID (Simple and variable products)
$product_id = $item_values['variation_id'];
if( $product_id == 0 || empty($product_id) ) $product_id = $item_values['product_id'];
// Get an instance of the product object
$product = wc_get_product( $product_id );
// Get the stock quantity of the product
$product_stock = $product->get_stock_quantity();
// Increase back the stock quantity
wc_update_product_stock( $product, $item_qty, 'increase' );
}
}
Ill explain my necessity:
I use wp all import to add product to my store, so some items price's is < 20 euro, in this case ill pay 3 euro for single order as shipping fee.
I need a simply if statements like this
[IF({price[.>20]})]{
price[1]}
[ELSE]{
price[1]}+3
[ENDIF]
That add 3 in case product price is less than 20...but when i try prices becomes not 3+3=6 but 3+000 = 3000
any ideas about?
Your wording is a little confusing. So I'm going to say what I think you are trying to do.
You have a store and you can add products.
When you are shipping products, if the product's price is less than 20 euros, then add 3 euros as a shipping fee.
In that case, the if-state should be something like:
If you a product object with a property price...
$cost = $product->price; //Get product price
if($product->price < 20){ //If shipping fee needs to be applied
$cost += 3; //Applied shipping fee
}
Although this is an old question, I will post the answer in case someone need this is in the future.
When you use Wp All Import you can use php functions.
In your case you need a function that check the original price and if it is lower than 20 it will add 3, else it will leave it as is.
So that function will look like this:
function price_change($originalprice) {
if ($originalprice < "20"):
$x = abs($originalprice + 3);
else:
$x = originalprice;
endif;
return $x;
}
And then in the price field you should use this:
[price_change({price[1]})]
It works! I have tested it on my woocommerce store
In Woocommerce, I would like my customers to buy either 18 or 36 products but not between the two. I wanna remove the possibility to checkout from 19 to 35.
You told me in comment that you don't know php well. Most of the time we don't offer php code here, but I found it interesting to do. And it may helps some people in the future.
As I said, one possibility is to do a redirection to the cart page on the checkout page if the customer doesn't have 18 or 36 products. After the redirection on the cart page, you must display a message for the customer to see that he must have 18 or 36 products.
Summary
add_action on template_redirect to check the product count and handle the redirection. If we it redirects, add an arg like invalid-cart-product-count in the url with value equal to the product count.
add_action on woocommerce_before_cart to check if we get the invalid-cart-product-count so we display a message.
Working code (tested)
There is the code that you need to place in your functions.php file of you theme or child theme (preferred option).
/**
* Check on template redirect the cart product count
*/
add_action('template_redirect','product_count_template_redirect');
function product_count_template_redirect() {
// If we are on checkout and the cart contents count is not equal to 18 or 36
if(is_checkout() && !in_array(WC()->cart->get_cart_contents_count(), array(18, 36))) {
// Redirect on the cart page and add a query arg to the url so we can check for it and add a message
wp_redirect(esc_url(add_query_arg('invalid-cart-product-count', WC()->cart->get_cart_contents_count(), wc_get_cart_url())));
exit;
}
}
/**
* Handle the message on the cart page if we have the 'invalid-cart-product-count' arg in url
*/
add_action('woocommerce_before_cart', 'cart_page_message');
function cart_page_message() {
// If its set and not empty
if(!empty($_GET['invalid-cart-product-count'])) {
// Display the message with the current cart count and the count that user need to have
$message = "<div class='woocommerce-error'>";
$message .= sprintf(
__('You currently have <strong>%s</strong> products in your cart. You must have <strong>18</strong> or <strong>36</strong> products in your cart to be able to checkout', 'your-text-domain'),
// Force the arg to be an int, if someone malicious change it to anything else
(int) $_GET['invalid-cart-product-count']
);
$message .= "</div>";
echo $message;
}
}
Tell me if you don't understand or if this not exactly what you need.
Completely new to Woocommerce/wordpress here. On the cart page mydomain.local/cart What filter should I use to show/hide a flat rate shipping method at certain times. From admin I managed to add an extra flat rate method and named it "Next day". Now I would like to show that flat rate method only before 4PM. I tried in functions.php
add_filter( 'woocommerce_package_rates', 'custom_change_shipping', 10);
function custom_change_shipping($rates) {
var_dump($rates);
}
Nothing seems to change and also I cant seem to debug the $rates variable as nothing is outputted when var_dump($rates);. I tried it both anonymous and as an admin but nothing seems to work.
Thanks #LoicTheAztec for confirming whether I was using the right filter. My other problem was that I was not able to output var_dump on the /cart page.
I found out that I need to clear my "cart cache" by going to woocommerce->settings->shipping and in the region, disable then enable again and click on save changes.
By doing this I could see my output from var_dump.
Filter posted earlier by #LoicTheAztec (as requested by #robert)
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping', 100 );
function custom_hide_shipping( $rates ) {
$current_time = date_i18n(('H'), current_time('timestamp')); //Uses Timezone Settings > General
$maximum_time = array (
'time' =>'16'); //4PM
if ($current_time >= $maximum['time']){
unset( $rates['flat_rate:X'] ); // change X to your rate id
}
return $rates;
}
I am trying to get shipping and handling amount. I was trying the snippet code below but it's not working. I am successfully sending some data to third party API after the place order .
echo $item->getShippingAmount();
but if I tried with.
echo $order->getShippingAmount();
It works but it shows total of shipping amount of all orders, but conversely I want each single amount to be shown.
item ordered * quantity
Can anybody tell me how to do that?
Maybe you can use
$order->getShippingIncTax()
Try with below code :
for magento2 :
foreach ($order->getAllItems() as $item) {
echo $item->getShippingAmount();
}