Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从WooCommerce购物车中的产品获取ACF图像字段的数据_Php_Wordpress_Woocommerce_Advanced Custom Fields_Shortcode - Fatal编程技术网

Php 从WooCommerce购物车中的产品获取ACF图像字段的数据

Php 从WooCommerce购物车中的产品获取ACF图像字段的数据,php,wordpress,woocommerce,advanced-custom-fields,shortcode,Php,Wordpress,Woocommerce,Advanced Custom Fields,Shortcode,“我的店铺”中的每个产品都有一个ACF图像字段,该字段显示与产品(活动)关联的t恤图像 现在,我需要客户在结账时看到此图像。我决定创建一个短代码并将其添加到签出页面的某个位置 我不知道如何从购物车中的商品中获取ACF图像字段 到目前为止,我在functions.php中使用了这段代码。有什么想法吗 function wpb_demo_shortcode() { $product_id = $cart_item['product_id']; $image = get_fie

“我的店铺”中的每个产品都有一个ACF图像字段,该字段显示与产品(活动)关联的t恤图像

现在,我需要客户在结账时看到此图像。我决定创建一个短代码并将其添加到签出页面的某个位置

我不知道如何从购物车中的商品中获取ACF图像字段

到目前为止,我在
functions.php
中使用了这段代码。有什么想法吗

function wpb_demo_shortcode() { 
 

    $product_id = $cart_item['product_id'];
    $image = get_field('tenue', $product_id);
    $size = 'full'; // (thumbnail, medium, large, full or custom size)
        if( $image ) {
            return wp_get_attachment_image( $image, $size );
        }
} 
// register shortcode
add_shortcode('tenue-shortcode', 'wpb_demo_shortcode');


注意:我们运行一个设置,即购物车中只能有一个产品。

要从购物车中获取一个产品ID,您可以在
foreach
循环中使用
WC()->cart->get\u cart()
break

所以你得到:

函数wpb\u demo\u shortcode(){
//厕所推车
如果(WC()->cart){
//循环浏览购物车项目
foreach(WC()->cart->get_cart()作为$cart_项目){
$product_id=$cart_项目['product_id'];
打破
}
//伊塞特
if(isset($product_id)){
$image=get_字段('tenue',$product_id);
$size='full';//(缩略图、中、大、全尺寸或自定义尺寸)
如果($image){
返回wp_get_attachment_image($image,$size);
}
}
}
} 
//寄存器短码
添加_短代码(“tenue短代码”、“wpb_演示_短代码”);

不知怎的,它没有显示出来。代码不需要一些$。。。在函数名?@tristan中,请了解短代码是如何工作的,以及如何正确应用它们。