【发布时间】:2017-06-01 10:45:48
【问题描述】:
简单的问题如何在购物车woocomerce中显示产品属性:例如颜色:红色,不确定是否有一些代码要添加,如钩子或一些代码到fundctions.php或者可以通过woocomerce设置完成,还没有找到任何有用的在线信息,任何帮助表示赞赏。
【问题讨论】:
标签: php wordpress woocommerce
简单的问题如何在购物车woocomerce中显示产品属性:例如颜色:红色,不确定是否有一些代码要添加,如钩子或一些代码到fundctions.php或者可以通过woocomerce设置完成,还没有找到任何有用的在线信息,任何帮助表示赞赏。
【问题讨论】:
标签: php wordpress woocommerce
只需做如下简单的事情,你就会把所有东西都放在你的cart_item中-
add_filter('woocommerce_cart_item_name', 'add_variations_in_cart', 10, 3);
function add_variations_in_cart($name, $cart_item, $item_key){
$product_variation = '';
if(!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0 ){
if(is_array($cart_item['variation']) && !empty($cart_item['variation'])){
foreach ($cart_item['variation'] as $key => $value) {
$product_variation .= '<br>'.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value);
}
}
}
echo $name.$product_variation;
}
就这么简单。谢谢。
【讨论】:
这个插件https://wordpress.org/plugins/woocommerce-show-attributes/
或
WooCommerce:显示购物车页面上每个项目下方列出的所有产品属性
在function.php中添加以下代码
/**
* WooCommerce: show all product attributes listed below each item on Cart page
*/
function sm_woo_cart_attributes($cart_item, $cart_item_key){
$item_data = $cart_item_key['data'];
$attributes = $item_data->get_attributes();
if ( ! $attributes ) {
return $cart_item;
}
$out = $cart_item . '<br />';
foreach ( $attributes as $attribute ) {
if ( $attribute['is_taxonomy'] ) {
// skip variations
if ( $attribute['is_variation'] ) {
continue;
}
// backwards compatibility for attributes which are registered as taxonomies
$product_id = $item_data->id;
$terms = wp_get_post_terms( $product_id, $attribute['name'], 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ($tax_object->labels->name) ) {
$tax_label = $tax_object->labels->name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
}
foreach ( $terms as $term ) {
$out .= $tax_label . ': ';
$out .= $term->name . '<br />';
}
} else {
// not a taxonomy
$out .= $attribute['name'] . ': ';
$out .= $attribute['value'] . '<br />';
}
}
echo $out;
}
add_filter( 'woocommerce_cart_item_name', sm_woo_cart_attributes, 10, 2 );