[英]get last cart item id woocommerce
I use woocommerce on wordpress. 我在wordpress上使用woocommerce。 This is my code 这是我的代码
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo $_product->ID.',';
}
?>
And this is the result of teh code: 这是代码的结果:
1297,1694,1297,3911,4999,
How can I get just last id '4999' ? 我如何才能获得最后一个ID'4999'?
Storing the ids in an array and using the end()
function would be a solution: 将id存储在数组中并使用end()
函数将是一个解决方案:
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$ids = array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$ids[] = $_product->ID;
}
echo 'Last item = ' . end($ids);
?>
You can do it even simpler that what Akhilesh proposed - instead of iterating through $items
array go to the last item there: 您可以更加简单地执行Akhilesh的建议-而不是遍历$items
数组,而是转到那里的最后一项:
end($items)['data']->post->ID;
I am not sure, but probably you would need PHP 5.3+ for that. 我不确定,但是您可能需要PHP 5.3+。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.