How can I call or show the title of the product that is in the cart in my Woocommerce store?
Currently in the store only one product is allowed in the cart and I would like to show the title of this product on the checkout page.
I am customizing the checkout page.
I have searched with some plugins and codes but I have not been able to show the title of the product where I want
CodePudding user response:
This might not be optimal but what you could do is add a shortcode like this in your functions.php:
add_shortcode('cart_item_title_header',function() {
global $woocommerce;
//Check if cart is empty or not
if (count(WC()->cart->get_cart()) > 0) {
//Loop trough cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$name = $product->get_name();
$title = '<h1 >' . $name . '</h1>';
}
ob_start();
echo $title;
}
return ob_get_clean();
}
);
And then add the shortcode in the top div using [cart_item_title_header]
CodePudding user response:
Here is how you could do it through code :
global $woocommerce;
// Get the product id of the product in the cart
$product_id = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$product_id = $values['product_id'];
break;
}
// Get the product object
$product = wc_get_product( $product_id );
// Get the product title
$product_title = $product->get_title();
echo 'Product in the cart: ' . $product_title;
If you want to avoid code you can try the woolentor plugin which works on element or sites , if you are using that.