簡體   English   中英

僅當購物車項目具有特定元數據時,才從購物車頁面刪除 WooCommerce 購物車數量選擇器

[英]Remove WooCommerce cart quantity selector from cart page only if cart item has specific meta data

我發現這個答案可以根據需要刪除購物車數量選擇器,但是如果購物車項目具有特定的元數據,我只需要這樣做。 因為我需要檢查購物車項目元而不是產品元,所以我不能使用這樣的答案。

我如何更新以下代碼以僅在商品具有購物車元數據“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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM