I would like to set a message to specific shipping class in Woocommerce checkout page.
I tried using:
add_action( 'woocommerce_review_order_before_order_total', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
$shipping_class_id = 14; // Your shipping class Id
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// Check cart items for specific shipping class, displaying a notice
//if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
var_dump($cart_item['data']->get_shipping_class_id());
echo "Do poznámky k objednávce napište adresu Zásilkovny nebo ZBoxu, kam chcete objednávku doručit.";
//break;
//}
}
}
But when I try to var_dump selected shipping class, it always returns me int(0).
I have used something similar to: Cart Message for a Specific Shipping Class in WooCommerce, because It did not work for me.
The Cart Message for a Specific Shipping Class in WooCommerce answer still works on last woocommerce version.
Your question code works and display the shipping class Id… So there is something else that is making trouble.
Be sure that a cart item has the required shipping class set for it.
Try following to display a message in checkout based for specific shipping class slug:
add_action( 'woocommerce_review_order_before_order_total', 'checkout_shipping_class_message' );
function checkout_shipping_class_message(){
// Here your shipping class slugs in the array
$shipping_classes = array('truck');
// Here your shipping message
$message = __("Please add some shipping details in order notes field.", "woocommerce");
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
$shipping_class = $cart_item['data']->get_shipping_class();
// echo '<pre>' . print_r($shipping_class, true) . '</pre>'; // Uncomment for testing
// Check cart items for specific shipping class, displaying a message
if( in_array($shipping_class, $shipping_classes ) ){
echo '<tr class="shipping-note">
<td colspan="2"><strong>'.__("Note", "woocommerce").':</strong> '.$message.'</td>
</tr>';
break;
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related
I am running a WooCommerce store with WC Marketplace. What I am trying to achieve with the hook below is to prevent a new item being added to a basket if there is already a product in the basket from a different vendor. e.g. If a shopper adds product x from vendor y to his basket, if they were to add product a from vendor b, then the item will not be added and the user will be informed of the error.
I have 2 questions:
- firstly when does a hook run, is it before the main function fired or after? I have a hook for the function woocommerce_add_to_cart. So I want to know will the hook fire after the function woocommerce_add_to_cart runs or before.
- My second question is, I have attached the hook below, in your opinion would this work?
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
$same_vendor = 1;
$empty = WC_Cart::is_empty();
//If there is an item in the cart then,
if (!$empty) {
//Get the VendorId of the product being added to the cart.
$vendor = get_wcmp_product_vendors($product_id);
$vendor_id = $vendor->id;
foreach( WC()->cart->get_cart() as $cart_item ) {
//Get the vendor Id of the item
$cart_product_id = $cart_item['product_id'];
$cart_vendor = get_wcmp_product_vendors($product_id);
$cart_vendor_id = $cart_vendor->id;
//If two products do not have the same Vendor then set $same_vendor to 0
if($vendor_id !== $cart_vendor_id) {
$same_vendor = 0;
}
}
if ($same_vendor === 0) {
WC()->cart->remove_cart_item( $cart_item_key );
//How do I show a message to tell the customer.
}
}
}
Regards
Here below are the hooks involved in WC_Cart add_to_cart() method:
A) Before an item is added to cart:
Validation filter hook woocommerce_add_to_cart_validation
Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
and some others related to "sold individually" products (see here)
A) After an item is added to cart:
Change cart Item filter hook woocommerce_add_cart_item
Add an event, action hook woocommerce_add_to_cart
To be clear in your case:
As you can see now woocommerce_add_to_cart is not a function but only an action hook.
Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
What is the purpose: To execute some custom code when this method is executed (event).
Regarding your code:
It should be better to use the dedicated filter hook woocommerce_add_to_cart_validation that will stop customer that want to add a new item to the cart if there is already a product in cart from a different vendor, displaying optionally a custom message:
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) {
if ( WC()->cart->is_empty() ) return $passed;
// Get the VendorId of the product being added to the cart.
$current_vendor = get_wcmp_product_vendors($product_id);
foreach( WC()->cart->get_cart() as $cart_item ) {
// Get the vendor Id of the item
$cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);
// If two products do not have the same Vendor
if( $current_vendor->id != $cart_vendor->id ) {
// We set 'passed' argument to false
$passed = false ;
// Displaying a custom message
$message = __( "This is your custom message", "woocommerce" );
wc_add_notice( $message, 'error' );
// We stop the loop
break;
}
}
return $passed;
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
Tested and works.
I am working on a woocommerce website and I am trying to restrict a product to be purchased only if a coupon is applied for it, so it should not be processed without adding a coupon code. User must enter a coupon code to be able to order this specific product (not on all other products).
Any help is appreciated.
For defined products, the following code will not allow checkout if a coupon is not applied, displaying an error message:
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
$targeted_ids = array(37); // The targeted product ids (in this array)
$coupon_code = 'summer2'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Check cart item for defined product Ids and applied coupon
if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
And in checkout:
Here's what I want to do:
If customer selects Canada as shipping country and there's a specific product in cart, checkout should not happen and an error message should generate informing customer of why checkout didn't happen.
My research:
Add or remove woocommerce error messages has code to generate WooCommerce error messages. I asked a question yesterday that gave me codes to check if certain product is in cart and if shipping country is set to Canada Remove Canada from country list when specific products are in cart on Woocommerce I am not sure which filter/action I must hook my code to so it runs whenever "Place Order" is clicked on checkout page.
Update:
So I tried combining the two codes I have listed in my research but it didn't work. Any help if I need to approach this differently will be really appreciated
Product IDs are 15631 & 12616
This can be done using the action hook woocommerce_check_cart_items through this custom function:
add_action( 'woocommerce_check_cart_items', 'products_not_shipable_in_canada' );
function products_not_shipable_in_canada() {
// Only on checkout page (allowing customer to change the country in cart shipping calculator)
if( ! is_checkout() ) return;
// Set your products
$products = array(15631, 12616);
// Get customer country
$country = WC()->session->get('customer')['shipping_country'];
if( empty($country) ){
$country = WC()->session->get('customer')['billing_country'];
}
// For CANADA
if( $country == 'CA' ){
// Loop through cart items
foreach( WC()->cart->get_cart() as $item ){
// IF product is in cart
if( in_array( $item['product_id'], $products ) ){
// Avoid checkout and display an error notice
wc_add_notice( sprintf(
__("The product %s can't be shipped to Canada, sorry.", "woocommerce" ),
'"' . $item['data']->get_name() . '"'
), 'error' );
break; // Stop the loop
}
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related: Set minimum allowed weight for a specific country in WooCommerce
I want to change the shipping method title displayed in the checkout of my store based on the shipping class the product has.
e.g.
Shipping method title is currently Flat Rate and I have 2 products:
If product A is being purchased I need it to have "Fragile shipping"
If product B is being purchased I need it to have "Standard shipping"
Sadly I have to do my shipping using classes so alternative methods won't work.
Any help would be appreciated.
The following code will rename your shipping flat rate based on your "Fragile" shipping class:
You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.
The code:
add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2);
function change_shipping_method_name_based_on_shipping_class($rates, $package){
// HERE set the shipping class for "Fragile"
$shipping_class_id = 64;
$found = false;
// Check for the "Fragile" shipping class in cart items
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$found = true;
break;
}
}
// Loop through shipping methods
foreach ( $rates as $rate_key => $rate ) {
// Change "Flat rate" Shipping method label name
if ( 'flat_rate' === $rate->method_id ) {
if( $found )
$rates[$rate_key]->label = __( 'Fragile shipping', 'woocommerce' );
else
$rates[$rate_key]->label = __( 'Standard shipping', 'woocommerce' );
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Don't forget to re-enable "Enable debug mode" option in shipping settings.
i think this plugin may help you and also check this one
I am running a WooCommerce store with WC Marketplace. What I am trying to achieve with the hook below is to prevent a new item being added to a basket if there is already a product in the basket from a different vendor. e.g. If a shopper adds product x from vendor y to his basket, if they were to add product a from vendor b, then the item will not be added and the user will be informed of the error.
I have 2 questions:
- firstly when does a hook run, is it before the main function fired or after? I have a hook for the function woocommerce_add_to_cart. So I want to know will the hook fire after the function woocommerce_add_to_cart runs or before.
- My second question is, I have attached the hook below, in your opinion would this work?
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
$same_vendor = 1;
$empty = WC_Cart::is_empty();
//If there is an item in the cart then,
if (!$empty) {
//Get the VendorId of the product being added to the cart.
$vendor = get_wcmp_product_vendors($product_id);
$vendor_id = $vendor->id;
foreach( WC()->cart->get_cart() as $cart_item ) {
//Get the vendor Id of the item
$cart_product_id = $cart_item['product_id'];
$cart_vendor = get_wcmp_product_vendors($product_id);
$cart_vendor_id = $cart_vendor->id;
//If two products do not have the same Vendor then set $same_vendor to 0
if($vendor_id !== $cart_vendor_id) {
$same_vendor = 0;
}
}
if ($same_vendor === 0) {
WC()->cart->remove_cart_item( $cart_item_key );
//How do I show a message to tell the customer.
}
}
}
Regards
Here below are the hooks involved in WC_Cart add_to_cart() method:
A) Before an item is added to cart:
Validation filter hook woocommerce_add_to_cart_validation
Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
and some others related to "sold individually" products (see here)
A) After an item is added to cart:
Change cart Item filter hook woocommerce_add_cart_item
Add an event, action hook woocommerce_add_to_cart
To be clear in your case:
As you can see now woocommerce_add_to_cart is not a function but only an action hook.
Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
What is the purpose: To execute some custom code when this method is executed (event).
Regarding your code:
It should be better to use the dedicated filter hook woocommerce_add_to_cart_validation that will stop customer that want to add a new item to the cart if there is already a product in cart from a different vendor, displaying optionally a custom message:
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) {
if ( WC()->cart->is_empty() ) return $passed;
// Get the VendorId of the product being added to the cart.
$current_vendor = get_wcmp_product_vendors($product_id);
foreach( WC()->cart->get_cart() as $cart_item ) {
// Get the vendor Id of the item
$cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);
// If two products do not have the same Vendor
if( $current_vendor->id != $cart_vendor->id ) {
// We set 'passed' argument to false
$passed = false ;
// Displaying a custom message
$message = __( "This is your custom message", "woocommerce" );
wc_add_notice( $message, 'error' );
// We stop the loop
break;
}
}
return $passed;
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
Tested and works.