Woocommerce: the shipping method is changed on checkout progress - woocommerce

I did hide the shipping method from the cart checkout. The user will select the shipping method on cart page.
In the cart page I selected fedex:15:GROUND_HOME_DELIVERY as the shipping method.
On the checkout page, I can see the shipping rate.
When I press "Place order" button on the checkout page, it shown the Pickup as shipping method.
I checked the log file on
woocommerce_checkout_process:
2020-10-21 01:13:23:
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_methods = fedex:15:GROUND_HOME_DELIVERY
but
woocommerce_checkout_update_order_meta:
2020-10-21 01:13:24
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_methods = local_pickup
It is very strange.
and the shipping rate on the order page is 0 too.
please advice
Thanks

Related

Add a Woocommerce fee based on shipping class and item quantity

In Woocommerce I am trying to add a shipping fee if a cart item has a specific shipping class assigned to the related product. I would like this shipping fee to be multiplied by the cart item quantity…
I have this working when a product is added to the cart and the quantity is increased and the additional shipping fee is increased also. However if I add another product with the same shipping class and increase the quantity the additional fee does not increase.
This is my code:
// Add additional fees based on shipping class
function woocommerce_fee_based_on_shipping_class( $cart_object ) {
global $woocommerce;
// Setup an array of shipping classes which correspond to those created in Woocommerce
$shippingclass_dry_ice_array = array( 'dry-ice-shipping' );
$dry_ice_shipping_fee = 70;
// then we loop through the cart, checking the shipping classes
foreach ( $cart_object->cart_contents as $key => $value ) {
$shipping_class = get_the_terms( $value['product_id'], 'product_shipping_class' );
$quantity = $value['quantity'];
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_dry_ice_array ) ) {
$woocommerce->cart->add_fee( __('Dry Ice Shipping Fee', 'woocommerce'), $quantity * $dry_ice_shipping_fee ); // each of these adds the appropriate fee
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_fee_based_on_shipping_class' ); // make it all happen when Woocommerce tallies up the fees
How can I make it work for additional cart items too?
Your code is a bit outdated and there is some mistakes. To add a fee based on product shipping class and cart item quantity use the following:
// Add a fee based on shipping class and cart item quantity
add_action( 'woocommerce_cart_calculate_fees', 'shipping_class_and_item_quantity_fee', 10, 1 );
function shipping_class_and_item_quantity_fee( $cart ) {
## -------------- YOUR SETTINGS BELOW ------------ ##
$shipping_class = 'dry-ice-shipping'; // Targeted Shipping class slug
$base_fee_rate = 70; // Base rate for the fee
## ----------------------------------------------- ##
$total_quantity = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
// Get the instance of the WC_Product Object
$product = $cart_item['data'];
// Check for product shipping class
if( $product->get_shipping_class() == $shipping_class ) {
$total_quantity += $cart_item['quantity']; // Add item quantity
}
}
if ( $total_quantity > 0 ) {
$fee_text = __('Dry Ice Shipping Fee', 'woocommerce');
$fee_amount = $base_fee_rate * $total_quantity; // Calculate fee amount
// Add the fee
$cart->add_fee( $fee_text, $fee_amount );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If there is one shipping class in cart there should be no fees, but if there is more than one shipping class, then should be additional 1 Eur handling fee and increase for every another shipping class.
Situation 1: Product with shipping class (Warehouse 1) added to cart = No extra fees
Situation 2: Product with shipping class (Warehouse 1) added to cart, Product with another shipping class (Warehouse 2) added to cart = 1x Handling fee added to cart
Subtotals:
Products 2x - 10 Eur
Shipping - Free
Handling fee 1x - 1Eur
Total - 11 Eur
Situation 3: Product with shipping class (Warehouse 1) added to cart, Product with shipping class (Warehouse 2) added to cart, Product with shipping class (Warehouse 3) added to cart = 2x Handling fee added to cart
Subtotals:
Products 3x - 15 Eur
Shipping - Free
Handling fee 2x - 1Eur
Total - 12 Eur

How to customize a woocommerce a new page between shopping cart and payment?

I am setting up a woocommerce shop and I want to add a new page between the shopping cart and the final payment. I currently have my cart page (cart. php), and for example the finalize purchase button, be "next", on this new page I want to add a number of functions and the finalize purchase button to make the final payment.
By default the configuration is: product page -> shopping cart -> payment_final. My idea is to add one more page to this cycle: product page -> shopping cart -> My_page_with_other_options -> final payment.
What files would I have to touch to modify the purchase cycle?
A greeting and thank you in advance.
Try like this
add_action( 'woocommerce_widget_shopping_cart_buttons', function(){
// Removing Buttons
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
// Adding customized Buttons
add_action( 'woocommerce_widget_shopping_cart_buttons', 'custom_widget_shopping_cart_proceed_to_checkout', 20 );
}, 1 );
// Custom Checkout button
function custom_widget_shopping_cart_proceed_to_checkout() {
$custom_link = 'your page url';
echo '' . esc_html__( 'Checkout', 'woocommerce' ) . '';
}
Change cart and checkout button links on WooCommerce mini cart widget

Prevent user to add same product twice in a cart of woocommerce

I want to prevent user to add same product twice in cart of Woocommerce in a same transaction, means once transaction is complete user can add that product into cart but at a same time user can add only one unit of that product into cart.
Unfortunately there is no 'action' hook before WooCommerce adds an item to the cart. But they have a 'filter' hook before adding to cart. That is how I use it:
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}

Woocommerce get total on checkout page

How to get Woo-commerce order total on cart page.
Use $cart to access the cart object.
Use $cart_total to access the cart total after all the calculations.
add_action("woocommerce_cart_contents", "get_cart");
function get_cart()
{
global $woocommerce;
// Will get you cart object
$cart = $woocommerce->cart;
// Will get you cart object
$cart_total = $woocommerce->cart->get_cart_total();
}
The cart total echoed here will be displayed above Cart table.
Refer to the WC_Cart class documentation for more help.
As order total in checkout is variable, i.e. is sum of added tax or shipping costs, you can get it after the order is set. You can access it when place order button is clicked. By woocommerce_checkout_create_order action hook.
According to LoicTheAztec
add_action( 'woocommerce_checkout_create_order', 'get_total_cost', 20, 1 );
function get_total_cost($order){
$total = $order->get_total();
}

Woocommerce Hook for Custom Behaviour for Place Order

I wonder if there's a hook for changing Place Order Button behaviour upon click. I am trying to replace/change a product upon placing order as the product selection (all available products) are also in the Checkout Page.
So far I have been trying to manipulate woocommerce_checkout_order_review hook & failed.
add_action('woocommerce_checkout_order_review', 'remove_woocommerce_product');
function remove_woocommerce_product(){
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
}
}
The hook above is not triggered upon Place Order. Maybe there's something wrong with my code or maybe what I suspected is wrong hook applied. Any ideas?
Nevermind... found the answer...
add_action('woocommerce_checkout_process', 'change_product_upon_submission');
function change_product_upon_submission() {
if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) ) {
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->empty_cart(); //Empty the cart
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the cart
}
}
For more explanation, see Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

Resources