


Bagaimanakah saya boleh menghalang pembelian produk tertentu yang tidak dibenarkan dalam WooCommerce dengan menghendaki pelanggan mempunyai bukti pembelian terdahulu?
Reaksi terhadap Mencegah Pembelian Tanpa Kebenaran dalam WooCommerce
Masalah:
Peniaga menghadapi cabaran biasa untuk menghalang pembelian tanpa kebenaran untuk tertentu produk. Dalam kes ini, pelanggan mesti mempunyai bukti pembelian terdahulu untuk produk "a" atau "b" untuk mendapatkan akses kepada produk "c," "d" dan "e."
Penyelesaian:
Dalam panduan komprehensif ini, kami akan melengkapkan anda dengan fungsi tersuai yang menentukan sama ada pelanggan telah membeli produk prasyarat sebelum membolehkan akses kepada terhad item.
function has_bought_items() { $bought = false; // Replace the numbers with your specific target product IDs $prod_arr = array( '21', '67' ); // Gather all customer orders $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => get_current_user_id(), 'post_type' => 'shop_order', // WooCommerce orders post type 'post_status' => 'wc-completed' // Only orders with "completed" status ) ); // Process each customer order foreach ( $customer_orders as $customer_order ) { // Handle WooCommerce version compatibility $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id; $order = wc_get_order( $customer_order ); // Iterate through products bought in the order foreach ($order->get_items() as $item) { // Product ID retrieval based on WooCommerce version if ( version_compare( WC_VERSION, '3.0', '<' ) ) $product_id = $item['product_id']; else $product_id = $item->get_product_id(); // Condition: Check if required product ID exists in the array of purchased products if ( in_array( $product_id, $prod_arr ) ) $bought = true; } } // Return true if the specific products have been purchased by the customer return $bought; }
Penggunaan:
Laksanakan fungsi dalam templat WooCommerce anda yang memanipulasi butang tambah ke troli produk, seperti:
- Gelung/add-to-cart.php untuk kedai halaman
- Folder produk tunggal/tambah ke troli untuk halaman produk individu
Sebagai contoh, dalam templat butang tambah ke troli untuk halaman kedai (gelung/tambah -to-cart.php):
// Replace numbers with restricted product IDs $restricted_products = array( '20', '32', '75' ); // WooCommerce compatibility adjustment $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Restricted product, inactive add-to-cart button if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) { // Display an inactive add-to-cart button with a custom message // Non-restricted product or allowed product after specific purchase } else { // Regular add-to-cart button code }
Contoh ini melumpuhkan butang tambah ke troli secara dinamik untuk produk tertentu sehingga pelanggan mempunyai pembelian yang terbukti sebelum item yang diperlukan.
Atas ialah kandungan terperinci Bagaimanakah saya boleh menghalang pembelian produk tertentu yang tidak dibenarkan dalam WooCommerce dengan menghendaki pelanggan mempunyai bukti pembelian terdahulu?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undress AI Tool
Gambar buka pakaian secara percuma

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Stock Market GPT
Penyelidikan pelaburan dikuasakan AI untuk keputusan yang lebih bijak

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



Phparrayshandledatacollectionsefficientlyusingindexedorassociativestructures; theareCreatedWithArray () atau [], AccessedViAkeys, ModifiedByAssignment, iteratedWithForeach, danManipulatedingingingingFuntionsLikeCount ()

Theobserverdesignpatternenablesautomaticnotificationofdependentobjectswhenasubject'sstatechanges.1) itdefinaone-to-manydependencybetweenobjects; 2) thesubjectmaintainsalistofobserversandnotifemmoninterfoninterface;

$ _Cookieisaphpsuperglobalforaccessingcookiessentbrowser; cookiesareSetusingsetCookie () sebelum ini, readvia $ _cooKie ['name'], updatedbyresendingwithnewvalues, anddeletedbysettingexpadexpadexpoadexpadexpadexpadexpadexpadexpoadtimestamp, denganSecureStorsiredTimeShamp, denganSecureStyCoREdingB, denganSsecureStoStoStAb

Useinterfacestodefinecontractsforunrelatedclasses,ensuringtheyimplementspecificmethods;2.Useabstractclassestosharecommonlogicamongrelatedclasseswhileenforcinginheritance;3.Usetraitstoreuseutilitycodeacrossunrelatedclasseswithoutinheritance,promotingD

B-treeindexesareBestFormostPhpapplications, astheysupportequalityandrangequeries, sorting, andareidealforcolumnsusedinwhere, gabungan, ororderbyclauses;

Ahli awam boleh diakses mengikut kehendak; 2. Ahli swasta hanya boleh diakses dalam kelas; 3. Ahli yang dilindungi boleh diakses dalam kelas dan subkelas; 4. Penggunaan rasional dapat meningkatkan keselamatan kod dan penyelenggaraan.

Digunakan ('y-m-dh: i: s') withdate_default_timeezone_set () togetcurrentdateandtimeinphp, memastikancurateresultsbysettingthedesiredtimezonelike'america/new_york'beforecallingdate ().

USETAteTimeFordatesInphp: createWitHnewDateTime (), formatWithFormat (), ModifyViaAdd () Ormodify (), setTimeZonesWithDateTimeZone, andCompareUsingOperatorsOrsOrsOrtersOrvals.
