在 WooCommerce 中,我使用 Advanced Custom Fields 插件来显示名为“product_cart_image”的自定义字段(图像),它替换了购物车中产品的默认图像。该代码适用于简单产品,但不适用于可变产品。对于这些,我得到了默认图像。
以下代码进入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
}
我怎样才能让它也适用于可变产品?
小唯快跑啊