仅当购物车项目具有特定元数据时,才从购物车页面中删除 WooCommerce 购物车数量选择器
Remove WooCommerce cart quantity selector from cart page only if cart item has specific meta data
我找到了 which can remove the cart quantity selector as needed, however I only need to do this if a cart item has specific meta data. As I need to check for cart item meta rather than product meta, I can't use answers like this one。
如何更新以下代码以仅在商品具有购物车元数据时应用 'Ticketnumber'?
add_filter( 'woocommerce_cart_item_quantity', 'wc_cart_item_quantity', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
if( is_cart() ){
$product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
}
return $product_quantity;
}
我想我可以使用 中的一些代码来检索购物车项目元数据,但我想不通。
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
$_product = $cart_item['data'];
$meta = $_product->get_meta( 'product_card_beschrijving', true ); // THIS PART
if( $meta ) {
$title .= '<span class="meta">' . $meta . '</span>';
}
return $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
您可以使用$cart_item['data']->get_meta()
所以你得到:
function filter_woocommerce_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ) {
// Get meta
$ticketnumber = $cart_item['data']->get_meta( 'Ticketnumber', true );
if ( $ticketnumber ) {
$product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'filter_woocommerce_cart_item_quantity', 10, 3 );
我找到了
如何更新以下代码以仅在商品具有购物车元数据时应用 'Ticketnumber'?
add_filter( 'woocommerce_cart_item_quantity', 'wc_cart_item_quantity', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
if( is_cart() ){
$product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
}
return $product_quantity;
}
我想我可以使用
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
$_product = $cart_item['data'];
$meta = $_product->get_meta( 'product_card_beschrijving', true ); // THIS PART
if( $meta ) {
$title .= '<span class="meta">' . $meta . '</span>';
}
return $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
您可以使用$cart_item['data']->get_meta()
所以你得到:
function filter_woocommerce_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ) {
// Get meta
$ticketnumber = $cart_item['data']->get_meta( 'Ticketnumber', true );
if ( $ticketnumber ) {
$product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'filter_woocommerce_cart_item_quantity', 10, 3 );