我正面临这个问题。
致命错误:未捕获错误:在 null 上调用成员函数 is_type()
有人会在这方面帮助我吗?
这是我使用的代码。
add_action( 'woocommerce_before_calculate_totals', 'sione_change_cart_item_name_and_price', 10, 1 );
function sione_change_cart_item_name_and_price( $cart ) {
global $product;
// Get new items names from WC_Session
$session_data = (array) WC()->session->get( 'new_item_names' );
//$sku_data = (array) WC()->session->get( 'product_sku' );
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if($product->is_type( 'simple' )) {
$item_names = $product->get_short_description();
} else {
$item_names = $cart_item['data']->get_name() .", ". $cart_item['data']->get_attribute('color'). "," . $cart_item['data']->get_attribute('gender') . " (Product SKU: " . $cart_item['data']->get_sku() . ")";
}
// If item name doesn't exist in WC_Session for this cart item, we do it
if( ! isset($session_data[$cart_item_key]) ) {
$session_data[$cart_item_key] = $item_names;
WC()->session->set( 'new_item_names', $session_data );
}
}
}
任何帮助将不胜感激。谢谢
慕森王