php - 在 Woocommerce 购物车和结帐中显示产品缩略图和属性
问题描述
我正在我的 Woocommerce 结帐表中显示产品属性,请参阅我之前的问题:在电子邮件中显示 Woocommerce 分类
我还想显示产品图片,所以我的理想是:产品图片在左侧,产品名称旁边,下一行产品数量,然后是属性。如果我使用这个功能
add_filter( 'woocommerce_cart_item_name', add_thumbnail_to_cart_items, 10, 3 );
function add_thumbnail_to_cart_items( $product_name, $cart_item, $cart_item_key ){
if ( ! is_checkout() ) return $product_name;
$product = $cart_item['data'];
$thumbnail = $product->get_image(array( 70, 70));
$opening_div = '<div id="checkout_thumbnail" style="float:left; padding-right:15px;">';
return $opening_div . $thumbnail . '</div> ' . $product->get_name();
}
该属性不再显示。如何同时显示两者?
解决方案
更新
以下将在其名称旁边显示产品图像,并在下方显示产品属性:
在结帐时,购物车商品数量在产品名称之后,然后在产品名称之后不能添加任何内容。产品属性可以添加为自定义购物车项目数据,使用另一个挂钩。
// 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
推荐阅读
- python - Scrapy不解析项目
- java - 忽略通知作为 AppLock 服务中的当前前台包
- angular - angular-cli 的最新 Angular 应用程序未成功编译包 json
- go - GO 应用程序在管道中时如何提示用户输入
- rust - 如何使用类型参数将变量装箱为静态方法引用?
- java - 如何从 arraylist 更改为 class [ ]?
- kubernetes - 使用 JSON 密钥文件的身份验证方法:未授权:GCR 登录失败
- fiware - Fiware JSON IoT 代理是否期望设备提供答案?
- ruby-on-rails - 为什么我无法使用 Excel 软件打开打开的 XML(在 Ruby on Rails 中生成)?
- java - 使用 Stripe 时出现 Maven 编译错误:找不到符号 APIException、APIConnectionException