在 Woocommerce 结帐中显示产品缩略图
Display product thumbnail in Woocommerce checkout
我正在使用 Woocommerce,需要在结帐时显示产品缩略图 table。我找到了这个函数:
function isa_woo_cart_attributes($cart_item, $cart_item_key){
global $product;
if (is_cart()){ echo "<style>#checkout_thumbnail{display:none;}</style>"; }
$item_data = $cart_item_key['data'];
$post = get_post($item_data->id);
$thumb = get_the_post_thumbnail($item_data->id, array( 80, 80));
echo '<div id="checkout_thumbnail" style="float: left; padding-right: 10px">' . $thumb . '</div> ' . $post->post_title; } add_filter('woocommerce_cart_item_name', isa_woo_cart_attributes, 10, 2);
这是有效的,但我还需要在购物车和 cehckout table 上显示产品属性(或者产品摘录,但最好是属性)。感谢您的帮助!
更新:您的代码中有一些错误和错误。
In checkout, the cart item quantity comes after the product name… So to display correctly the product description, additional code is required.
请尝试以下操作:
// cart and checkout inline styles
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;}
dt.variation-Description { display: none;}
</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( 80, 80));
$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 description 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 ){
if( $cart_item['data']->get_description() && ( is_cart() || is_checkout() ) ){
$cart_item_data[] = array(
'name' => __("Description"),
'value' => $cart_item['data']->get_description(),
);
}
return $cart_item_data;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
购物车页面:
结帐页面:
我正在使用 Woocommerce,需要在结帐时显示产品缩略图 table。我找到了这个函数:
function isa_woo_cart_attributes($cart_item, $cart_item_key){
global $product;
if (is_cart()){ echo "<style>#checkout_thumbnail{display:none;}</style>"; }
$item_data = $cart_item_key['data'];
$post = get_post($item_data->id);
$thumb = get_the_post_thumbnail($item_data->id, array( 80, 80));
echo '<div id="checkout_thumbnail" style="float: left; padding-right: 10px">' . $thumb . '</div> ' . $post->post_title; } add_filter('woocommerce_cart_item_name', isa_woo_cart_attributes, 10, 2);
这是有效的,但我还需要在购物车和 cehckout table 上显示产品属性(或者产品摘录,但最好是属性)。感谢您的帮助!
更新:您的代码中有一些错误和错误。
In checkout, the cart item quantity comes after the product name… So to display correctly the product description, additional code is required.
请尝试以下操作:
// cart and checkout inline styles
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;}
dt.variation-Description { display: none;}
</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( 80, 80));
$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 description 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 ){
if( $cart_item['data']->get_description() && ( is_cart() || is_checkout() ) ){
$cart_item_data[] = array(
'name' => __("Description"),
'value' => $cart_item['data']->get_description(),
);
}
return $cart_item_data;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
购物车页面:
结帐页面: