更新
下面会在产品名称旁边显示产品图片,下面是产品属性:
在结帐时,购物车商品数量在产品名称之后,然后在产品名称之后不能添加任何内容。产品属性可以添加为自定义购物车项目数据,使用另一个挂钩。
// cart and checkout inline styles (To be removed and added in your theme's styles.css file)
add_action( 'wp_head', 'custom_inline_styles', 900 );
function custom_inline_styles(){
if ( is_checkout() || is_cart() ){
?><style>
.product-item-thumbnail { float:left; padding-right:10px;}
.product-item-thumbnail img { margin: 0 !important;}
</style><?php
}
}
// Product thumbnail in checkout
add_filter( 'woocommerce_cart_item_name', 'product_thumbnail_in_checkout', 20, 3 );
function product_thumbnail_in_checkout( $product_name, $cart_item, $cart_item_key ){
if ( is_checkout() ) {
$thumbnail = $cart_item['data']->get_image(array( 70, 70));
$image_html = '<div class="product-item-thumbnail">'.$thumbnail.'</div> ';
$product_name = $image_html . $product_name;
}
return $product_name;
}
// Cart item qquantity in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_checkout_cart_item_quantity', 20, 3 );
function filter_checkout_cart_item_quantity( $quantity_html, $cart_item, $cart_item_key ){
return ' <strong class="product-quantity">' . sprintf( '× %s', $cart_item['quantity'] ) . '</strong><br clear="all">';
}
// Product attribute in cart and checkout
add_filter( 'woocommerce_get_item_data', 'product_descrition_to_cart_items', 20, 2 );
function product_descrition_to_cart_items( $cart_item_data, $cart_item ){
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
$taxonomy = 'pa_delivery';
$value = $product->get_attribute($taxonomy);
if ($product->get_attribute($taxonomy)) {
$cart_item_data[] = array(
'name' => get_taxonomy($taxonomy)->labels->singular_name,
'value' => $product->get_attribute($taxonomy),
);
}
return $cart_item_data;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
如果需要,您应该需要为显示的产品属性设置样式,具体取决于您在主题中和您自己进行的现有 CSS 自定义。
代码基于此相关答案:
Displaying product thumbnail and attribute in Woocommerce cart and checkout