1 回答
TA贡献1863条经验 获得超2个赞
将可变产品添加到购物车时,对于购物车项目 cart,您需要获取可变产品 ID 而不是变体 ID,因此您将替换以下行:
$product_cart_image = get_field('product_cart_image', $_product->get_id());
经过:
$product_cart_image = get_field('product_cart_image', $cart_item['product_id']);
现在它应该可以工作了……所以在你的代码中:
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
}
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报