英文:
how to show the title of a product that is in the cart of Woocommerce
问题
如何在我的Woocommerce商店中调用或显示购物车中的产品标题?
目前在商店中只允许购物车中有一个产品,我想在结算页面上显示这个产品的标题。
我正在自定义结算页面。
我已经尝试了一些插件和代码,但是我无法在我想要的位置显示产品的标题。
英文:
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
答案1
得分: 0
这可能不是最佳方法,但你可以在你的functions.php中添加一个类似这样的短代码:
add_shortcode('cart_item_title_header',function() {
global $woocommerce;
//检查购物车是否为空
if (count(WC()->cart->get_cart()) > 0) {
//循环遍历购物车项目
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$name = $product->get_name();
$title = '<h1 class="product-title">' . $name . '</h1>';
}
ob_start();
echo $title;
}
return ob_get_clean();
});
然后使用[cart_item_title_header]
将短代码添加到顶部的div中。
英文:
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 class="product-title">' . $name . '</h1>';
}
ob_start();
echo $title;
}
return ob_get_clean();
}
);
And then add the shortcode in the top div using [cart_item_title_header]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论