如何显示在Woocommerce购物车中的产品标题

huangapple go评论81阅读模式
英文:

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(&#39;cart_item_title_header&#39;,function() {
global $woocommerce;
	//Check if cart is empty or not
	if (count(WC()-&gt;cart-&gt;get_cart()) &gt; 0) {
		//Loop trough cart items
		foreach (WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item) {
			$product 	= $cart_item[&#39;data&#39;];
			$name		= $product-&gt;get_name();
			$title		= &#39;&lt;h1 class=&quot;product-title&quot;&gt;&#39; . $name . &#39;&lt;/h1&gt;&#39;;
		}
		ob_start();
		echo $title;
	}
	return ob_get_clean();
}
);

And then add the shortcode in the top div using [cart_item_title_header]

huangapple
  • 本文由 发表于 2023年1月9日 06:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定