Echo product name WooCommerce
-
Hello everyone,
I am trying to finish my webshop in WooCommerce. There are some products which cannot be sent. Therefore, I have used the code below which checks whether the products in the cart are of a specific category (not sendable). If this is true, then a notice is printed in the cart informing my customer that it contains a product that cannot be send.
So i try to echo the name of the product of the “not sendable” category in this notice. Can someone please help me out?
Thanks,
Robin
<?php
// Exit if accessed directly
if ( !defined( ‘ABSPATH’ ) ) exit;// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED – Do not modify or remove comment markers above or below:if ( !function_exists( ‘chld_thm_cfg_parent_css’ ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( ‘chld_thm_cfg_parent’, trailingslashit( get_template_directory_uri() ) . ‘style.css’ );
}
endif;
add_action( ‘wp_enqueue_scripts’, ‘chld_thm_cfg_parent_css’ );// END ENQUEUE PARENT ACTION
// a function to check if the cart has product from organge and it’s sub category id
function cart_has_product_with_orange_cats() {
//Check to see if user has product in cart
global $woocommerce;
//assigns a default negative value
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[‘data’];
$terms = get_the_terms( $_product->id, ‘product_cat’ );
// second level loop search, in case some items have several categories
if($terms){
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if (( $_categoryid === 40 )) {
//category is in cart!
$product_in_cart = true;
}
}
}
}return $product_in_cart;
}// add filter and function to hide method
add_filter( ‘woocommerce_available_shipping_methods’, ‘custom_shipping_methods’ , 10, 1 );
function custom_shipping_methods( $available_methods ){
if ( cart_has_product_with_orange_cats() ) {
foreach($available_methods as $key => $method){
if($key == ‘local_pickup’){
continue;
}
unset($available_methods[$key]);}
// remove the rate you want
}// return the available methods without the one you unset.
return $available_methods;
}add_action( ‘woocommerce_before_checkout_form’ , ‘orange_product_notices’ );
add_action(‘woocommerce_before_cart_table’,’orange_product_notices’);
function orange_product_notices() {
global $woocommerce;
if(cart_has_product_with_orange_cats()){
wc_print_notice( “There is a product in your shoppingcart that can’t be delivered.
The product that can’t be delivered is:
“, $notice_type = ‘notice’ );}
}
?>
- The topic ‘Echo product name WooCommerce’ is closed to new replies.