简体   繁体   English

使用 ACF 在 WooCommerce 购物车中为可变产品获取自定义字段

[英]Get a custom field in WooCommerce cart for variable products too using ACF

In WooCommerce, I am using Advanced Custom Fields plugin to display a custom field(image) called 'product_cart_image' which replaces the default image of a product in cart.在 WooCommerce 中,我使用高级自定义字段插件来显示一个名为“product_cart_image”的自定义字段(图像),它替换了购物车中产品的默认图像。 The code is working for simple products but it's not working for variable products .该代码适用于简单产品,但不适用于可变产品 For these I get the default image.对于这些,我得到默认图像。

The following code goes in cart.php template file:以下代码在cart.php模板文件中:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key ); 
    ?>
    <span class="product-thumbnail">
    <?php
    $product_image      = $_product->get_image();
    $product_cart_image = get_field('product_cart_image', $_product->get_id());

    if ( ! empty ( $product_cart_image ) ) {
        $product_image = wp_get_attachment_image( $product_cart_image['ID'] );
    }

    $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $product_image, $cart_item, $cart_item_key );

    if ( ! $product_permalink ) {
        echo $thumbnail;
    } else {
        printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail ); 
    }
    ?>
    </span>
    <?php
}

How can I make it work for variable products too?我怎样才能使它也适用于可变产品?

When a variable product is added to cart, for a cart item cart you need to get the variable product ID instead of the variation ID, so you will replace the following line:将可变产品添加到购物车时,对于购物车商品购物车,您需要获取可变产品 ID 而不是变体 ID,因此您将替换以下行:

$product_cart_image = get_field('product_cart_image', $_product->get_id());

by:经过:

$product_cart_image = get_field('product_cart_image', $cart_item['product_id']);

Now it should work… So in your code:现在它应该可以工作了......所以在你的代码中:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key ); 
    ?>
    <span class="product-thumbnail">
    <?php
    $product_image      = $_product->get_image();
    $product_cart_image = get_field('product_cart_image', $cart_item['product_id']);

    if ( ! empty ( $product_cart_image ) ) {
        $product_image = wp_get_attachment_image( $product_cart_image['ID'] );
    }

    $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $product_image, $cart_item, $cart_item_key );

    if ( ! $product_permalink ) {
        echo $thumbnail;
    } else {
        printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail ); 
    }
    ?>
    </span>
    <?php
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当变量与 WooCommerce 中的 ACF 自定义字段匹配时获取 Json 数据 - Get Json Data when a variable matches with ACF custom field in WooCommerce 从 WooCommerce 购物车中的产品获取 ACF 图像字段的数据 - Get data from product in WooCommerce cart for ACF image field WooCommerce 购物车费用从自定义产品输入字段计算 - WooCommerce cart Fee calculated from Custom products input field WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart Woocommerce可变产品未添加到购物车 - Woocommerce Variable Products not adding to the Cart Woocommerce 产品类别帖子的 ACF 关系字段 - ACF Relationship field by category posts for Woocommerce products 使用 ACF 编号字段作为 WooCommerce 自定义产品类型的价格 - Using ACF number field as price for WooCommerce custom product type 将基于 ACF 字段的自定义“添加到购物车”按钮添加到 WooCommerce 单个产品页面 - Add custom "Add to cart" button based on ACF field to WooCommerce single product page 如果条件满足,自定义 function 重定向到页面(购物车中的有限产品太多) Woocommerce - Custom function to redirect to page if condition met (too many limited products in cart) Woocommerce 在管理区域中更改Woocommerce变量产品的自定义字段的顺序 - Change the order of custom field for Woocommerce variable products in admin area
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM