我已经为我的产品配置了运输等级.但我想在购物车页面中的每个产品下方显示它们.像这样:
I've configure shipping class for my products. But I want to display them below each product in the shopping cart page. Something like this :
这可以通过编辑 PHP 来完成吗?
Can this be done through editing the PHP?
要在购物车页面中显示产品运输类别名称,有多种方法:
To display the product shipping class name in cart page, there is many ways to do it:
1) 使用挂在 woocommerce_cart_item_name 过滤器钩子中的自定义函数,这样:
1) Using a custom function hooked in woocommerce_cart_item_name filter hook, this way:
add_filter( 'woocommerce_get_item_data', 'shipping_class_in_item_name', 20, 2 );
function shipping_class_in_item_name( $cart_data, $cart_item ) {
$custom_items = array();
$product = $cart_item['data']; // Get the WC_Product object instance
$shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
$label = __( 'Shipping class', 'woocommerce' );
// Checking (To display it in checkout page too, remove below " || is_checkout()" )
if( empty( $shipping_class_id ) || is_checkout() )
return $cart_data; // Return default cart dat (in case of)
// If product or variation description exists we display it
$custom_items[] = array(
'key' => $label,
'display' => $shipping_class_term->name,
);
// Merging shipping class name and product variation attributes + values (if there are some)
if( ! empty( $cart_data ) ) $custom_items = array_merge( $custom_items, $cart_data );
return $custom_items;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.
此代码已经过测试且有效.
This code is tested and works.
2) 使用挂在 woocommerce_cart_item_name 过滤器钩子中的自定义函数,这样:
2) Using a custom function hooked in woocommerce_cart_item_name filter hook, this way:
add_filter( 'woocommerce_cart_item_name', 'shipping_class_in_item_name', 20, 3);
function shipping_class_in_item_name( $item_name, $cart_item, $cart_item_key ) {
// Only in cart page (remove the line below to allow the display in checkout too)
if( ! ( is_cart() || is_checkout() ) ) return $item_name;
$product = $cart_item['data']; // Get the WC_Product object instance
$shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
if( empty( $shipping_class_id ) )
return $item_name; // Return default product title (in case of)
$label = __( 'Shipping class', 'woocommerce' );
return $item_name . '<br>
<p class="item-shipping_class" style="margin:12px 0 0;">
<strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
}
代码位于活动子主题(或活动主题)的 function.php 文件中.
经过测试并有效.
这篇关于在 WooCommerce 购物车页面中的每个产品下方添加运输类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽