[英]Set Max Purchase Totals in WooCommerce
我正在嘗試在我的 WooCommerce 購物車中設置最大購買總額。 (背景:我想禁止在這種情況下使用 100% 優惠券的人將更多產品添加到他們的購物車。)
這是我從這里改編的代碼:
// Weitere Einkäufe verhindern
add_action( 'woocommerce_check_cart_items', 'cldws_set_max_total');
function cldws_set_max_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set maximum cart total
$max_cart_total = 1;
// A maximum of 1 € is required before checking out.
$total = WC()->cart->totals;
// Compare values and add an error is Cart's total
if( $total >= $max_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Bitte lege nicht mehr Stränge in den Warenkorb als du beim Crowdfunding bestellt hast.</strong> Die genaue Anzahl findest du in der E-Mail mit dem Gutscheincode. '),
'error' );
}
}
}
這是它應該做的:
檢查這僅在購物車和結帳頁面上運行並設置最大值后。 總計值為 1 歐元,我查詢購物車的總計並將它們與此最大值進行比較。 如果總數大於最大值。 允許的總數,它會顯示錯誤消息。
我懷疑我用“totals”查詢了錯誤的變量,但是那個變量應該是折扣后購物車總價值的變量。 我也嘗試過“get_cart_total()”,但這沒有幫助。 我也看過這個,但不明白為什么用 $woocommerce 替換 WC() 會有所幫助。
非常感謝任何幫助,謝謝!
結果替換
WC()->cart->get_cart_total();
和
$woocommerce->cart->total;
實際上可以解決問題。 但我不明白為什么。 :-D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.