>您想在客戶結帳之前在WooCommerce商店中設置某種最低要求。接下來是有關如何設置這些要求和限制的指南,而無需完全使用任何插件:
>我們將使用WooCommerce_Check_cart_items操作由WooCommerce提供的操作來運行我們的功能並執行我們的代碼。訪問以下鏈接以獲取WooCommerce操作和過濾器的完整列表,也稱為掛鉤。
>通常限制客戶完成結帳過程而不滿足最小權重要求是有用的。減小的重量要求可以幫助您的運輸成本更易於管理,並且運輸過程更加精簡。不要忘記將最小的重量要求更改為最適合您的一切,並記住,在WooCommerce下設置的任何重量單元 - >設置 - > Products-> Products。
<span>// Set a minimum weight requirement before checking out </span><span>add_action( 'woocommerce_check_cart_items', 'spyr_set_weight_requirements' ); </span><span>function spyr_set_weight_requirements() { </span> <span>// Only run in the Cart or Checkout pages </span> <span>if( is_cart() || is_checkout() ) { </span> <span>global $woocommerce; </span> <span>// Set the minimum weight before checking out </span> <span>$minimum_weight = 25; </span> <span>// Get the Cart's content total weight </span> <span>$cart_contents_weight = WC()->cart->cart_contents_weight; </span> <span>// Compare values and add an error is Cart's total weight </span> <span>// happens to be less than the minimum required before checking out. </span> <span>// Will display a message along the lines of </span> <span>// A Minimum Weight of 25kg is required before checking out. (Cont. below) </span> <span>// Current cart weight: 12.5kg </span> <span>if( $cart_contents_weight < $minimum_weight ) { </span> <span>// Display our error message </span> <span>wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>' </span> <span>. '<br />Current cart weight: %s%s', </span> <span>$minimum_weight, </span> <span>get_option( 'woocommerce_weight_unit' ), </span> <span>$cart_contents_weight, </span> <span>get_option( 'woocommerce_weight_unit' ), </span> <span>get_permalink( wc_get_page_id( 'shop' ) ) </span> <span>), </span> <span>'error' ); </span> <span>} </span> <span>} </span><span>}</span>
<span>// Set a minimum number of products requirement before checking out </span><span>add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' ); </span><span>function spyr_set_min_num_products() { </span> <span>// Only run in the Cart or Checkout pages </span> <span>if( is_cart() || is_checkout() ) { </span> <span>global $woocommerce; </span> <span>// Set the minimum number of products before checking out </span> <span>$minimum_num_products = 20; </span> <span>// Get the Cart's total number of products </span> <span>$cart_num_products = WC()->cart->cart_contents_count; </span> <span>// Compare values and add an error is Cart's total number of products </span> <span>// happens to be less than the minimum required before checking out. </span> <span>// Will display a message along the lines of </span> <span>// A Minimum of 20 products is required before checking out. (Cont. below) </span> <span>// Current number of items in the cart: 6 </span> <span>if( $cart_num_products < $minimum_num_products ) { </span> <span>// Display our error message </span> <span>wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' </span> <span>. '<br />Current number of items in the cart: %s.', </span> <span>$minimum_num_products, </span> <span>$cart_num_products ), </span> <span>'error' ); </span> <span>} </span> <span>} </span><span>}</span>
要設置這些限制,您需要創建一個數組,該數組將您的規則/限制保存在另一個數組中。編輯此數組時要小心,並確保准確輸入所有代碼,以防止錯誤和意外結果。您需要使用的格式如下:
>
<span>// Product Id and Min. Quantities per Product </span><span>// id = Product ID </span><span>// min = Minimum quantity </span><span>$product_min_qty = array( </span> <span>array( 'id' => 47, 'min' => 100 ), </span> <span>array( 'id' => 37, 'min' => 100 ), </span> <span>array( 'id' => 34, 'min' => 100 ), </span> <span>array( 'id' => 31, 'min' => 100 ), </span><span>);</span>
>
<span>// Set minimum quantity per product before checking out </span><span>add_action( 'woocommerce_check_cart_items', 'spyr_set_min_qty_per_product' ); </span><span>function spyr_set_min_qty_per_product() { </span> <span>// Only run in the Cart or Checkout pages </span> <span>if( is_cart() || is_checkout() ) { </span> <span>global $woocommerce; </span> <span>// Product Id and Min. Quantities per Product </span> <span>$product_min_qty = array( </span> <span>array( 'id' => 47, 'min' => 100 ), </span> <span>array( 'id' => 37, 'min' => 100 ), </span> <span>array( 'id' => 34, 'min' => 100 ), </span> <span>array( 'id' => 31, 'min' => 100 ), </span> <span>); </span> <span>// Will increment </span> <span>$i = 0; </span> <span>// Will hold information about products that have not </span> <span>// met the minimum order quantity </span> <span>$bad_products = array(); </span> <span>// Loop through the products in the Cart </span> <span>foreach( $woocommerce->cart->cart_contents as $product_in_cart ) { </span> <span>// Loop through our minimum order quantities per product </span> <span>foreach( $product_min_qty as $product_to_test ) { </span> <span>// If we can match the product ID to the ID set on the minimum required array </span> <span>if( $product_to_test['id'] == $product_in_cart['product_id'] ) { </span> <span>// If the quantity required is less than than the quantity in the cart now </span> <span>if( $product_in_cart['quantity'] < $product_to_test['min'] ) { </span> <span>// Get the product ID </span> <span>$bad_products[$i]['id'] = $product_in_cart['product_id']; </span> <span>// Get the Product quantity already in the cart for this product </span> <span>$bad_products[$i]['in_cart'] = $product_in_cart['quantity']; </span> <span>// Get the minimum required for this product </span> <span>$bad_products[$i]['min_req'] = $product_to_test['min']; </span> <span>} </span> <span>} </span> <span>} </span> <span>// Increment $i </span> <span>$i++; </span> <span>} </span> <span>// Time to build our error message to inform the customer </span> <span>// About the minimum quantity per order. </span> <span>if( is_array( $bad_products) && count( $bad_products ) > 1 ) { </span> <span>// Lets begin building our message </span> <span>$message = '<strong>A minimum quantity per product has not been met.</strong><br />'; </span> <span>foreach( $bad_products as $bad_product ) { </span> <span>// Append to the current message </span> <span>$message .= get_the_title( $bad_product['id'] ) .' requires a minimum quantity of ' </span> <span>. $bad_product['min_req'] </span> <span>.'. You currently have: '. $bad_product['in_cart'] .'.<br />'; </span> <span>} </span> <span>wc_add_notice( $message, 'error' ); </span> <span>} </span> <span>} </span><span>}</span>
結論
<span>// Set a minimum dollar amount per order </span><span>add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' ); </span><span>function spyr_set_min_total() { </span> <span>// Only run in the Cart or Checkout pages </span> <span>if( is_cart() || is_checkout() ) { </span> <span>global $woocommerce; </span> <span>// Set minimum cart total </span> <span>$minimum_cart_total = 10; </span> <span>// Total we are going to be using for the Math </span> <span>// This is before taxes and shipping charges </span> <span>$total = WC()->cart->subtotal; </span> <span>// Compare values and add an error is Cart's total </span> <span>// happens to be less than the minimum required before checking out. </span> <span>// Will display a message along the lines of </span> <span>// A Minimum of 10 USD is required before checking out. (Cont. below) </span> <span>// Current cart total: 6 USD </span> <span>if( $total <= $minimum_cart_total ) { </span> <span>// Display our error message </span> <span>wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>' </span> <span>.'<br />Current cart\'s total: %s %s', </span> <span>$minimum_cart_total, </span> <span>get_option( 'woocommerce_currency'), </span> <span>$total, </span> <span>get_option( 'woocommerce_currency') ), </span> <span>'error' ); </span> <span>} </span> <span>} </span><span>}</span>
感謝您的閱讀,評論或有關如何改進代碼的建議。如果您對特定的WooCommerce修改有想法,請不要害羞,並在評論中發布鏈接。
wooCommerce中有關最低結帳要求的常見問題>
我可以在WooCommerce中設置最大數量嗎?用於WooCommerce中的產品。類似於設置最小數量,您可以從“庫存”選項卡下的“產品數據”部分中執行此操作。在那裡,您將找到設置“最大數量”的選項。輸入所需的號碼並保存您的更改。這將限制客戶可以單訂單購買的每種產品的數量。
可以為產品的變化設置最小和最大數量嗎?
>是的,可以為WooCommerce中產品的變化設置最小和最大數量。這可以從產品數據的變體部分完成。對於每種變體,您都可以設置“最小數量”和“最大數量”。 >如何在產品頁面上顯示最小和最大數量要求?產品頁面上的數量要求可以使用諸如“ WooCommerce Min/Max Nutities”之類的插件來完成。此插件在產品頁面上添加了一個通知,該頁面顯示了產品的最小和最大數量要求。>我可以為特定類別的產品設置最小和最大數量嗎?為WooCommerce中的特定類別產品設置最小和最大數量。這可以通過使用“ WooCommerce Min/Max Wentities”之類的插件來完成。安裝和激活後,您可以從插件的設置中為每個產品類別設置最小和最大數量。
以上是在WooCommerce中設定最低結帳要求的詳細內容。更多資訊請關注PHP中文網其他相關文章!