Woocommerce Hook for Custom Behaviour for Place Order - wordpress

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

Related

Disable Woocommerce Add to Cart button if cart is not empty

I am trying to hide the Add to Cart button and replace it with a message if the cart is not empty.
Reason: We can only process one order in the cart at a time, regardless of the product type.
Goal: If a customer has already added an item to the cart, they will not see the Add to Cart button on other items. But instead will see a message that states, "Before you can add another product to your cart, please complete your purchase that currently exists in your cart."
I have tested numerous conditional plugins, but none can provide a condition that checks if the cart is empty. I have also viewed various posts in this forum that discuss disabling the Add to Cart button under other conditions, but I have yet to see a code that can hide the Add to Cart button, replace it with a message, if the shopping cart is not empty.
Thanks!
You can achieve this by using a few custom functions.
First we'll make every product non purchasable when something exists in the cart.
add_filter( 'woocommerce_is_purchasable', 'products_no_purchasable_when_other_product_in_cart', 99, 1 );
function products_no_purchasable_when_other_product_in_cart( $is_purchasable ) {
if ( WC()->cart->get_cart_contents_count() > 0) {
$is_purchasable = false;
}
return $is_purchasable;
}
This function alone is enough to prevent users from adding more than one product in the cart. But they would still be able to see and click the "Add to cart" button.
The "Add to cart" button is typically seen in 2 places: on the product page and inside of the product loop, meaning archive pages, shop pages, related products etc.
When the button is displayed inside the loop, we can use filter and replace the the text and the url of the button. In the function below, we replace "Add to cart" text with a long message explaining why a customer can't buy the product and the url is linking to the checkout page.
// On WooCommerce shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_add_to_cart_button_in_a_loop', 10, 3 );
function change_add_to_cart_button_in_a_loop( $sprintf, $product, $args ) {
// When cart NOT empty
if ( WC()->cart->get_cart_contents_count() > 0 ) {
$button_text = 'Before you can add another product to your cart, please complete your purchase that currently exists in your cart.';
// Button URL
$new_button_url = wc_get_page_permalink( 'checkout' );
// New link + text
$sprintf = sprintf(
'%s',
esc_url( $new_button_url ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
esc_html( $button_text )
);
}
return $sprintf;
}
On the product page, the button will be removed automatically when the product is not purchasable and we are making sure it's not purchasable in the first code snippet. Now we only have to fill the void with some message explaining why the "Add to cart" button is not there. We do it using woocommerce_single_product_summary hook. You can use any other hook on the product page to move this message around.
add_action( 'woocommerce_single_product_summary', 'message_when_other_product_already_in_cart', 10 );
function message_when_other_product_already_in_cart() {
if ( WC()->cart->get_cart_contents_count() > 0) {
$message = 'Before you can add another product to your cart, please complete your purchase that currently exists in your cart.';
echo '<p>'.$message.'</p>';
}
}
And finally, most themes will have ajax "add to cart" buttons. This means the page is not reloaded after you add or remove a product from the cart. For example, you are on the shop page and add one product in the cart. The buttons on other products will still be visible. Or when you open a small cart in the header and remove the product from the cart, the shop/product page won't enable "add to cart" buttons automatically.
That's why we add a simple javasctipt that will reload the page when added_to_cart or removed_from_cart javascript events are triggered. We add this code only to WooCommerce pages.
add_action( 'wp_footer', 'custom_footer_scripts' );
function custom_footer_scripts() {
if ( is_woocommerce() ) {
?>
<script>
jQuery( document.body ).on( 'added_to_cart', function() {
window.location = window.location.href;
});
jQuery( document.body ).on( 'removed_from_cart', function() {
window.location = window.location.href;
});
</script>
<?php
}
}
I tested this on the Storefront theme and with WooCommerce 7.2.2

Remove some features of WooCommerce

I'm modifying a theme for WordPress and i can't find a solution for:
   1. How can I delete the WooCommerce product image inside the gallery? Because this image is added automatically and cannot be deleted.
IMAGES: https://imgur.com/a/qd0INEX
   2. How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Greetings to all who comment, I hope these issues can help more people in the future.
Redirect to Checkout when a Product has been added to the Cart
Second question answer:
How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Step 1.
// Disable AJAX add to cart buttons
First of all, we have to do some small configurations in WooCommerce Settings – Uncheck the “Enable AJAX add to cart buttons on archives” checkbox.
Step 2.
// Change text on add to cart buttons
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'nik_add_to_cart_text_1' );
function nik_add_to_cart_text_1( $add_to_cart_html ) {
return str_replace( 'Add to cart', 'Buy now', $add_to_cart_html );
}
/*
* Change button text on product pages
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'nik_add_to_cart_text_2' );
function nik_add_to_cart_text_2( $product ){
return 'Buy now';
}
I decided that str_replace() for this situation is the most simple and easy solution, but if you do not want to use it, you can replace the first part of the code with this one:
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'nik_add_to_cart_text_1', 10, 2 );
function nik_add_to_cart_text_1( $text, $product ){
return $product->is_purchasable() && $product->is_in_stock() ? 'Buy Now' : 'Read more';
}
Step 3.
// Redirect to Checkout Page
add_filter( 'woocommerce_add_to_cart_redirect', 'nik_skip_cart_redirect_checkout' );
function nik_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
Step 4.
// Remove “The product has been added to your cart” message
add_filter( 'wc_add_to_cart_message_html', 'nik_remove_add_to_cart_message' );
function nik_remove_add_to_cart_message( $message ){
return '';
}

Adding extra info to order

I am programmatically adding a product to the cart. Besides this, somehow, I want to store some extra info (an array) to the order. When the client finishes the order, I want to access that info through some WordPress actions. I'll have to do this immediately after adding the product to the cart, because the info may change after that, if the user doesn't finish the order right away. Is there any way I can do it, without putting the database to work?
You should probably use the WooCommerce Cart Item Meta API and the WooCommerce Order Item Meta API.
You use them like this:
// Add to cart item
// This is triggered on add to cart
add_filter('woocommerce_add_cart_item_data', 'my_add_cart_item_data', 10, 2);
function my_add_cart_item_data( $cart_item_meta, $product_id ) {
//Here we can easily filter what values should be added to what products using the $product_id
$cart_item_meta['my_meta_key'] = 'meta value';
return $cart_item_meta;
}
// Add to order item when the cart is converted to an order
// This is triggered when the order is created
add_action('woocommerce_add_order_item_meta', 'my_order_item_meta'), 10, 2);
function my_order_item_meta( $item_id, $values, $cart_item_key ) {
// The value stored in cart above is accessable in $values here
woocommerce_add_order_item_meta( $item_id, 'meta_key', $values['my_meta_key'] );
//Or add what ever you want
$meta_value = 'value';
woocommerce_add_order_item_meta( $item_id, 'meta_key', $meta_value );
}
I hope that helps.

WooCommerce Delete all products from cart and add current product to cart

I am new to WooCommerce and I need to be able to only add one single product in the cart. I want to clear all products and add the current product to the cart when I click the "Add to cart" button.
How can I do that ?
I have got an exact solution for this.
Try following code.
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
The above filter is defined in class-wc-cart.php within function add_to_cart().
http://docs.woothemes.com/wc-apidocs/source-class-WC_Cart.html#774-905
Thus, when add to cart button is pressed, it empties the cart and then add the product.
Try this,
//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);
class file is wp-content/plugins/woocommerce/classes/class-wc-cart.php
Hope its helps..

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources