我正在使用自定義函式來更改產品價格的樣式。
這是我的實際功能:
add_filter( 'woocommerce_format_sale_price', function( $price, $regular_price, $sale_price ) {
// stuff happens here
}, 10, 3 );
如果我想要來自產品的一些元欄位,它在產品詳細資訊頁面上作業正常。
如果我使用global $product并從那里開始,我可以獲得這些元欄位。
問題是,global $product如果在購物車/結帳頁面上使用,則會引發錯誤。
所以我正在使用is_product()并is_cart()檢查我在哪里。
但我不知道如何從購物車中的產品中獲取元欄位。
我知道,我可以使用$cart_itemlike $product。但似乎沒有辦法將其與global.
有沒有其他方法可以在上面的函式中獲取購物車專案?
uj5u.com熱心網友回復:
您可以使用WC()->cart來獲取購物車物件。然后你可以回圈它使用$cart->get_cart()get cart_item。試試下面的代碼。
add_filter( 'woocommerce_format_sale_price', function( $price, $regular_price, $sale_price ) {
if ( WC()->cart ) {
$cart = WC()->cart; // Get cart
if ( ! $cart->is_empty() ) {
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
// stuff happens here
}
}
}
}, 10, 3 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331139.html
標籤:php WordPress的 求购 大车 查看
上一篇:角域消毒器和SVG位元組陣列
