این کد امکان ایجاد فرم پرداخت مبلغ دلخواه در ووکامرس را فراهم میکند. با استفاده از یک شورتکد، یک دکمه «پرداخت مبلغ» نمایش داده میشود که با کلیک روی آن، فرم پرداخت در یک پنجره پاپآپ باز خواهد شد.
کاربر میتواند مبلغ پرداخت، عنوان پرداخت، شماره فاکتور، شماره موبایل و توضیحات را وارد کند. پس از ثبت فرم، مبلغ بهصورت خودکار به سبد خرید منتقل شده و کاربر به صفحه پرداخت ووکامرس هدایت میشود.
این کد همچنین یک محصول مجازی مخصوص پرداخت دلخواه ایجاد میکند و اطلاعات واردشده را در سفارش ووکامرس ذخیره و نمایش میدهد.
برای نمایش دکمه پرداخت، از شورتکد زیر استفاده کنید:
[custom_pay]
<?php
add_shortcode('custom_pay', 'dk_custom_pay_modal');
function dk_custom_pay_modal(){
ob_start();
?>
<style>
.dkpay-wrapper{
font-family:iranyekan,sans-serif;
display:inline-block;
margin:0;
max-width:none;
}
.dkpay-overlay{
position:fixed;
inset:0;
width:100%;
height:100%;
background:rgba(0,0,0,0.45);
backdrop-filter:blur(6px);
-webkit-backdrop-filter:blur(6px);
opacity:0;
visibility:hidden;
pointer-events:none;
transition:.3s;
z-index:999998;
}
.dkpay-modal{
position:fixed;
top:50%;
left:50%;
transform:translate(-50%,-50%) scale(.8);
width:92%;
max-width:450px;
background:#fff;
border-radius:10px;
padding:15px;
box-shadow:0 8px 30px rgba(0,0,0,0.25);
opacity:0;
visibility:hidden;
pointer-events:none;
transition:.3s;
z-index:999999;
box-sizing:border-box;
}
.dkpay-wrapper.dkpay-show .dkpay-overlay{
opacity:1;
visibility:visible;
pointer-events:auto;
}
.dkpay-wrapper.dkpay-show .dkpay-modal{
opacity:1;
visibility:visible;
pointer-events:auto;
transform:translate(-50%,-50%) scale(1);
}
.dk-field{
margin-bottom:15px;
}
.dk-field label{
font-weight:bold;
margin-bottom:5px;
display:block;
font-size:14px;
}
.dk-input{
width:100%;
padding:10px;
border:1px solid #ddd;
border-radius:5px;
font-size:14px;
box-sizing:border-box;
font-family:inherit;
}
.dk-input:focus{
outline:none;
border-color:#0088cc;
}
.dk-close{
position:absolute;
right:15px;
top:12px;
font-size:30px;
cursor:pointer;
color:#666;
background:transparent;
border:0;
line-height:1;
padding:0;
z-index:2;
font-family:Arial, sans-serif;
}
.dk-close:hover{
color:#000;
}
.dkpay-modal-title{
font-size:20px;
text-align:center;
margin:0 0 20px;
padding:0 25px;
}
@media(max-width:480px){
.dkpay-modal{
padding:20px 15px;
}
.dkpay-modal-title{
font-size:16px;
}
}
</style>
<div class="dkpay-wrapper">
<button class="dkpay-open-btn">
پرداخت مبلغ
</button>
<div class="dkpay-overlay"></div>
<div class="dkpay-modal">
<button type="button" class="dk-close" aria-label="بستن">
×
</button>
<h2 class="dkpay-modal-title"> فرم پرداخت مبلغ دلخواه </h2>
<form method="post">
<?php wp_nonce_field('dk_custom_pay_action', 'dk_custom_pay_nonce'); ?>
<div class="dk-field">
<label>مبلغ تومان</label>
<input
class="dk-input"
type="number"
name="dk_amount"
min="1000"
required
>
</div>
<div class="dk-field">
<label>عنوان پرداخت</label>
<input
class="dk-input"
type="text"
name="dk_title"
placeholder="مثلاً پرداخت قسط"
required
>
</div>
<div class="dk-field">
<label>شماره فاکتور</label>
<input
class="dk-input"
type="text"
name="dk_invoice"
placeholder="مثلاً 4587"
>
</div>
<div class="dk-field">
<label>موبایل</label>
<input
class="dk-input"
type="text"
name="dk_mobile"
placeholder="مثلاً 09121234567"
>
</div>
<div class="dk-field">
<label>توضیحات</label>
<textarea
class="dk-input"
name="dk_desc"
rows="4"
></textarea>
</div>
<button type="submit" name="dk_pay_btn">
ادامه و پرداخت
</button>
</form>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function(){
document.querySelectorAll(".dkpay-wrapper").forEach(function(wrapper){
const openBtn = wrapper.querySelector(".dkpay-open-btn");
const closeBtn = wrapper.querySelector(".dk-close");
const overlay = wrapper.querySelector(".dkpay-overlay");
const modal = wrapper.querySelector(".dkpay-modal");
if(!openBtn || !closeBtn || !overlay || !modal){
return;
}
function openModal(e){
if(e){
e.preventDefault();
}
wrapper.classList.add("dkpay-show");
}
function closeModal(e){
if(e){
e.preventDefault();
}
wrapper.classList.remove("dkpay-show");
}
openBtn.addEventListener("click", openModal);
closeBtn.addEventListener("click", closeModal);
overlay.addEventListener("click", closeModal);
modal.addEventListener("click", function(e){
e.stopPropagation();
});
});
document.addEventListener("keydown", function(e){
if(e.key === "Escape"){
document.querySelectorAll(".dkpay-wrapper.dkpay-show").forEach(function(wrapper){
wrapper.classList.remove("dkpay-show");
});
}
});
});
</script>
<?php
return ob_get_clean();
}
add_action('template_redirect', 'dk_custom_pay_process');
function dk_custom_pay_process(){
if( ! isset($_POST['dk_pay_btn']) ){
return;
}
if(
! isset($_POST['dk_custom_pay_nonce']) ||
! wp_verify_nonce($_POST['dk_custom_pay_nonce'], 'dk_custom_pay_action')
){
wc_add_notice('درخواست نامعتبر است. لطفاً دوباره تلاش کنید.', 'error');
return;
}
if( ! function_exists('WC') || ! WC()->cart ){
return;
}
$amount = isset($_POST['dk_amount']) ? intval($_POST['dk_amount']) : 0;
$title = isset($_POST['dk_title']) ? sanitize_text_field($_POST['dk_title']) : '';
$invoice = isset($_POST['dk_invoice']) ? sanitize_text_field($_POST['dk_invoice']) : '';
$mobile = isset($_POST['dk_mobile']) ? sanitize_text_field($_POST['dk_mobile']) : '';
$desc = isset($_POST['dk_desc']) ? sanitize_textarea_field($_POST['dk_desc']) : '';
if($amount < 1000){
wc_add_notice('مبلغ وارد شده معتبر نیست.', 'error');
return;
}
if(empty($title)){
wc_add_notice('عنوان پرداخت الزامی است.', 'error');
return;
}
$product_id = dk_get_custom_pay_product();
if( ! $product_id ){
wc_add_notice('محصول پرداخت دلخواه ایجاد نشد.', 'error');
return;
}
WC()->cart->empty_cart();
WC()->cart->add_to_cart($product_id, 1, 0, array(), array(
'dk_amount' => $amount,
'dk_title' => $title,
'dk_invoice' => $invoice,
'dk_mobile' => $mobile,
'dk_desc' => $desc,
));
wp_safe_redirect(wc_get_checkout_url());
exit;
}
function dk_get_custom_pay_product(){
$product_slug = 'custom-pay';
$product_name = 'پرداخت مبلغ دلخواه';
$args = array(
'post_type' => 'product',
'post_status' => array('publish', 'private'),
'name' => $product_slug,
'posts_per_page' => 1,
'fields' => 'ids',
);
$products = get_posts($args);
if( ! empty($products) ){
$product_id = intval($products[0]);
update_post_meta($product_id, '_price', '0');
update_post_meta($product_id, '_regular_price', '0');
update_post_meta($product_id, '_virtual', 'yes');
update_post_meta($product_id, '_sold_individually', 'yes');
return $product_id;
}
$product_id = wp_insert_post(array(
'post_title' => $product_name,
'post_name' => $product_slug,
'post_type' => 'product',
'post_status' => 'publish',
'meta_input' => array(
'_virtual' => 'yes',
'_price' => '0',
'_regular_price' => '0',
'_sold_individually' => 'yes',
),
));
if( is_wp_error($product_id) || ! $product_id ){
return 0;
}
wp_set_object_terms($product_id, 'simple', 'product_type');
update_option('dk_custom_pay_product_id', $product_id);
return $product_id;
}
add_action('woocommerce_before_calculate_totals', 'dk_set_custom_price', 20);
function dk_set_custom_price($cart){
if(is_admin() && ! defined('DOING_AJAX')){
return;
}
if( ! $cart ){
return;
}
static $custom_product_id = null;
if($custom_product_id === null){
$custom_product_id = dk_get_custom_pay_product();
}
foreach($cart->get_cart() as $cart_item){
if(
isset($cart_item['product_id']) &&
intval($cart_item['product_id']) === intval($custom_product_id) &&
isset($cart_item['dk_amount'])
){
$cart_item['data']->set_price(floatval($cart_item['dk_amount']));
}
}
}
add_filter('woocommerce_get_item_data', 'dk_show_custom_pay_meta', 10, 2);
function dk_show_custom_pay_meta($item_data, $cart_item){
static $custom_product_id = null;
if($custom_product_id === null){
$custom_product_id = dk_get_custom_pay_product();
}
if(
! isset($cart_item['product_id']) ||
intval($cart_item['product_id']) !== intval($custom_product_id)
){
return $item_data;
}
$fields = array(
'dk_title' => 'عنوان پرداخت',
'dk_invoice' => 'شماره فاکتور',
'dk_mobile' => 'موبایل',
'dk_desc' => 'توضیحات',
);
foreach($fields as $key => $label){
if(isset($cart_item[$key]) && $cart_item[$key] !== ''){
$item_data[] = array(
'name' => $label,
'value' => wc_clean($cart_item[$key]),
);
}
}
return $item_data;
}
add_action('woocommerce_checkout_create_order_line_item', 'dk_save_custom_pay_order', 10, 4);
function dk_save_custom_pay_order($item, $cart_item_key, $values, $order){
static $custom_product_id = null;
if($custom_product_id === null){
$custom_product_id = dk_get_custom_pay_product();
}
if(
! isset($values['product_id']) ||
intval($values['product_id']) !== intval($custom_product_id)
){
return;
}
$fields = array(
'dk_title' => 'عنوان پرداخت',
'dk_invoice' => 'شماره فاکتور',
'dk_mobile' => 'موبایل',
'dk_desc' => 'توضیحات',
);
foreach($fields as $key => $label){
if(isset($values[$key]) && $values[$key] !== ''){
$item->add_meta_data($label, $values[$key], true);
}
}
}