• 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.

    In this notice, i try to echo the name of the product of the “not sendable” category.

    For example:
    There is a product in your cart that cannot be delivered:
    Title of product 1 (which cant be send)
    Title of product 2 (which cant be send)
    etc.

    Can someone please help me out?

    Thanks!

    Robin

    https://www.ads-software.com/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Thread Starter robinjoshua

    (@robinjoshua)

    Whoops. The code is here:

    <?php

    // 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_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’ );

    }
    }
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘Echo product name WooCommerce’ is closed to new replies.