در این مقاله به ترفندهای ووکامرس که مجموعهای از کاربردیترین کدهای ووکامرس هستند را برای شما آماده کردهایم، افزونه ووکامرس از بهترین فروشگاه سازهای وردپرس است و از توسعه و انعطاف بالایی برخوردار است.
سر فصلهای این مقاله:
در صورتی که هنوز به ووکامرس مسلط نیستید، توصیه میکنیم آموزش ووکامرس که به صورت جامع و پیشرفته تولید شده است مشاهده و بررسی کنید که به مباحث مختلفی پرداختیم.
فیلد دلخواه در باکس ویرایش سریع محصول
فیلدهای دلخواه از پراستفادهترین سفارش سازیها در طراحی وب سایت خصوصاً وردپرس به شمار میرود.
با افزودن یک فیلد دلخواه، میتوانید از آن برای کاربردهای مختلفی در ووکامرس خود استفاده کنید.
قطعه کد زیر را در انتها یا ابتدای فایل functions.php قالب یا چایلد تم قالب خود قرار بدید و آن را ذخیره کنید.
مهم و ضروری! قبل از افزودن کدها به فایل فانکشن قالب، یک کپی از این فایل بعنوان بک آپ بگیرید.
/**
* @snippet Add Custom Field @ WooCommerce Quick Edit Product
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_action( 'woocommerce_product_quick_edit_start', 'themefour_show_custom_field_quick_edit' );
function themefour_show_custom_field_quick_edit() {
global $post;
?>
فیلد دلخواه: ' . esc_html( get_post_meta( $post_id, '_custom_field', true ) ) . '
اجباری کردن نمایش تب نظرات محصولات
با استفاده از قطعه کد زیر میتوانید تب نظرات را همیشه و به صورت اجباری در محصولات نمایش دهید.
حتی اگر در صفحۀ ویرایش محصول، گزینه فعال سازی نقد و بررسی تیک نخورده باشد، باز نظرات را خواهید داشت.
در این حالت ممکن است به هر دلیلی فعال سازی نقد و بررسی را فراموش کرده باشید.
اینجاست که این قطعه کد به شما کمک میکند که به صورت سراسری همیشه نظرات محصولات باشند.
/**
* @snippet Override & Force Enable Reviews @ WooCommerce Products
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'comments_open', 'themefour_force_enable_reviews', 9999, 2 );
function themefour_force_enable_reviews( $enable, $post_id ) {
if ( 'product' === get_post_type( $post_id ) ) {
$enable = true;
}
return $enable;
}
اجباری کردن فروش تکی برای همه محصولات
محصولاتی دارید که تنها باید یکبار فروش بروند؟ محصولات دانلودی دارید؟ این قطعه کد برای شماست.
اگر نمیخواهید روی تک تک محصولات گزینه فروش تکی را فعال کنید و تعداد محصولاتی زیادی دارید.
/**
* @snippet Override & Force Sold Individually @ WooCommerce Products
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_is_sold_individually', '__return_true' );
با این قطعه کد، هر محصول توسط مشتری تنها میتواند یکبار به سبد خرید اضافه شود.
مخفی کردن قیمت محصولات ناموجود
با این قطعه کد کاربردی میتوانید قیمت کالاهایی که دیگر در انبار شما موجود نیستند را مخفی کنید.
/**
* @snippet Hide Price If Out of Stock @ WooCommerce Frontend
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_get_price_html', 'themefour_hide_price_if_out_stock_frontend', 9999, 2 );
function themefour_hide_price_if_out_stock_frontend( $price, $product ) {
if ( is_admin() ) return $price; // BAIL IF BACKEND
if ( ! $product->is_in_stock() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $product );
}
return $price;
}
ووکامرس به صورت پیشفرض حتی اگر محصول ناموجود باشد قیمت آن را نمایش میدهد.
نمایش موجودی کالا در ایمیل سفارش
به این فکر کردید که میتوانیم تعداد موجودی کالاها را در ایمیل سفارش مشتری قید کنیم؟.
قطعه کد زیر، هر محصولی که موجودی آن را رو به کاهش باشد، موجودی آن را فاکتور نمایش داده میشود.
/**
* @snippet Remaining Stock @ WooCommerce New Order Email
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
// ----------
// STEP 1: DECLARE EMAIL ID GLOBAL
add_action( 'woocommerce_email_before_order_table', 'themefour_email_id_as_global', 9999, 4 );
function themefour_email_id_as_global( $order, $sent_to_admin, $plain_text, $email ){
$GLOBALS['email_id'] = $email->id;
}
// ----------
// STEP 2: IF NEW ORDER EMAIL, ADD REMAINING STOCK QUANTITY
add_filter( 'woocommerce_email_order_item_quantity', 'themefour_item_remaining_stock', 9999, 2 );
function themefour_item_remaining_stock( $qty_display, $item ) {
$email_id = $GLOBALS['email_id'];
if ( empty( $email_id ) ) return;
if ( 'new_order' !== $email_id ) return;
$product = $item->get_product();
if ( is_object( $product ) && $product->managing_stock() ) {
$qty_display .= ' (' . $product->get_stock_quantity() . ' عدد باقی مانده)';
}
return $qty_display;
}
نمونه تستی که از این قطعه قرار دادیم گویای کار این کد کاربردی برای فروشگاه ساز ووکامرس است.
افزودن یک پیشوند و پسوند به موجودی کالا
اگر میخواهید به ابتدا و انتهای عدد موجودی کالا در صفحه محصول یک متن دلخواه اضافه کنید.
این قطعه کد برای همین منظور است و میتوانید متن دلخواه خود را مانند تصویر زیر قرار بدهید.
/**
* @snippet Stock Quantity Suffix @ WooCommerce Single Product
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_format_stock_quantity', 'themefour_stock_quantity_suffix', 9999, 2 );
function themefour_stock_quantity_suffix( $stock_quantity, $product ) {
$stock_quantity .= ' عددموجودی';
return $stock_quantity;
}
متن به فارسی در کد نوشته شده و کاملاً روند اجرای این قطعه کد قابل تشخیص است.
نمایش کالاها براساس موجودی و توقف فروش
این ترفند مورد بسیار کاربردی و مورد علاقه، میتوانید مدیریت بهتری برای محصولات شما فراهم میکند.
با این کد، همۀ محصولات در محیط پیشخوان وردپرس، براساس موجودی آنها مرتب میشوند.
مثلاً محصولاتی که ناموجود هستند به انتهای صفحه میروند و محصولات موجود بالاتر قرار میگیرند.
در افزونه ووکامرس این حالت براساس تاریخ انتشار محصولات است و ممکن است این را دوست نداشته باشید.
کافیست قطعه کد زیر را در فایل فانکشن قالب (functions.php) یا چایلد تم خود قرار دهید و ذخیره کنید.
بعد از این کار به منوی محصولات ووکامرس در پیشخوان ووکامرس خود برگردید تا تغییرات را ببینید.
خواهید دید که کالاها یا محصولات ناموجود به انتهای صفحه یا لیست محصولات شما رفتهاند.
/**
* @snippet Product Sorted By Stock @ WordPress Dashboard
* @post WooCommerce Customization & Development Tricks - Part 1
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_action( 'pre_get_posts', 'themefour_sort_products_by_stock_status_admin' );
function themefour_sort_products_by_stock_status_admin( $query ){
global $typenow;
if ( is_admin() && $query->is_main_query() && $typenow == 'product' ) {
if ( ! isset( $_GET['orderby'] ) ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', '_stock_status' );
$query->set( 'order', 'ASC' );
}
}
}
ایجاد و ساخت یک محصول خودکار
عجیب نیست! به لطف انعطاف ووکامرس و وردپرس، چنین کارهای باحالی هم قابل پیاده سازی است.
با قطعه کدی که در زیر قرار دادیم میتوان با آپلود هر تصویری در وردپرس یک محصول جدید ایجاد کرد.
فرآیند ایجاد این محصول خودکار بوده و با استفاده از کدهای تعریف شده صورت میگیرد.
شما میتوانید با استفاده از دانش کدنویسی خودتان، این قطعه کد را شخصیسازی و از آن استفاده کنید.
این مورد از جذابترین ترفندهای ووکامرس به شمار میرود و با خیال راحت آن را تست کنید.
/**
* @snippet Auto Create Product @ WP Admin
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_action( 'add_attachment', 'themefour_create_product_automatically', 9999 );
function themefour_create_product_automatically( $image_id ) {
$product = new WC_Product_Simple();
$product->set_name( 'محصول ' . get_the_title( $image_id ) );
$product->set_status( 'publish' );
$product->set_catalog_visibility( 'visible' );
$product->set_price( 50000 );
$product->set_regular_price( 47500 );
$product->set_sold_individually( true );
$product->set_image_id( $image_id );
$product->set_downloadable( true );
$product->set_virtual( true );
$src_img = wp_get_attachment_image_src( $image_id, 'full' );
$file_url = reset( $src_img );
$file_md5 = md5( $file_url );
$download = new WC_Product_Download();
$download->set_name( get_the_title( $image_id ) );
$download->set_id( $file_md5 );
$download->set_file( $file_url );
$downloads[$file_md5] = $download;
$product->set_downloads( $downloads );
$product->save();
}
اجازه آپلود فایلهای متنی txt در ووکامرس
وردپرس به صورت پیشفرض اجازه آپلود همۀ فرمتها و فایلها را به دلایل امنیتی نمیدهد.
اما با وجود قطعه کدهایی میتوانید هر نوع فایلی را در وردپرس بدون مشکل آپلود و از آنها استفاده کنید.
اگر میخواهید از فایلهای متنی با فرمت txt در هنگام آپلود فایل در ووکامرس استفاده کنید.
از قطعه کد زیر برای این کار میتوانید بهره بگیرید و در فایل فانکشن قالب خود آن را قرار دهید.
/**
* @snippet Fix "Sorry File Type Not Permitted" @ WooCommerce Downloadable Product + WordPress Media Library
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'upload_mimes', 'themefour_allow_custom_mime_types' );
function themefour_allow_custom_mime_types( $mimes ) {
if ( current_user_can( 'manage_woocommerce' ) ) {
$mimes['txt'] = 'text/plain';
$mimes['json'] = 'text/plain';
}
return $mimes;
}
add_filter( 'wp_check_filetype_and_ext', 'themefour_correct_filetypes', 10, 5 );
function themefour_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) {
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
return $data;
}
$wp_file_type = wp_check_filetype( $filename, $mimes );
if ( 'json' === $wp_file_type['ext'] ) {
$data['ext'] = 'json';
$data['type'] = 'text/plain';
} elseif ( 'txt' === $wp_file_type['ext'] ) {
$data['ext'] = 'txt';
$data['type'] = 'text/plain';
}
return $data;
}
راهنمای وارد کردن تلفن هنگام ثبت سفارش
با دهمین از ترفندهای ووکامرس به معرفی یکی از شخصیسازی صفحۀ تسویه حساب فروشگاه میپردازیم.
با کد زیر میتوانید یک Placeholder یا Mask روی فیلد تلفن صورت حساب ووکامرس قرار دهید.
این میتواند راهنمای خوبی برای نحوه صحیح وارد کردن شماره موبایل برای مشتریان باشد.
/**
* @snippet Phone Mask @ WooCommerce Checkout
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_checkout_fields', 'themefour_checkout_phone_us_format' );
function themefour_checkout_phone_us_format( $fields ) {
$fields['billing']['billing_phone']['placeholder'] = '09140000';
$fields['billing']['billing_phone']['maxlength'] = 12; // حداکثر تعداد کاراکتر فیلد
return $fields;
}
add_action( 'woocommerce_after_checkout_form', 'bbloomer_phone_mask_us' );
function bbloomer_phone_mask_us() {
wc_enqueue_js( "
$('#billing_phone')
.keydown(function(e) {
var key = e.which || e.charCode || e.keyCode || 0;
var phone = $(this);
if (key !== 8 && key !== 9) {
if (phone.val().length === 3) {
phone.val(phone.val() + '-'); // add dash after char #3
}
if (phone.val().length === 7) {
phone.val(phone.val() + '-'); // add dash after char #7
}
}
return (key == 8 ||
key == 9 ||
key == 46 ||
(key >= ۴۸ && key <= 57) ||
(key >= ۹۶ && key <= 105));
});
" );
}
تغییرنام متن عدم موجودی در فروشگاه
اگر میخواهید متن عدم موجودی یا ناموجود یا out in stock را در صفحههای فروشگاه تغییر دهید.
از این قطعه کد میتوانید برای این کار استفاده کنید و به راحتی این متن را به دلخواه تغییر بدید.
/**
* @snippet Rename Out of stock @ WooCommerce Shop
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'themefour_archive_custom_cart_button_text' );
function themefour_archive_custom_cart_button_text( $text ) {
global $product;
if ( $product && ! $product->is_in_stock() ) {
return 'توقف فروش!';
}
return $text;
}
مشخص سازی برچسب محصول
کاربرد این کد برای نمایش یک پیام روی هر بلوک محصول جهت نمایش برچسب استفاده شده آن است.
در کد، برچسب را مشخص کردیم (همان تست) و هر محصولی که این برچسب را داشته باشد پیام چاپ میشود.
/**
* @snippet Show Tag Name in WooCommerce Shop
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_action( 'woocommerce_after_shop_loop_item', 'themefour_print_message_if_product_belongs' );
function themefour_print_message_if_product_belongs() {
global $product;
$product_id = $product->get_id();
if ( has_term( 'تست', 'product_tag', $product_id ) ) { // نام برچسب: تست
echo 'این محصول با برچسب تست است'; // نمایش پیام در صفحههای دسته بندی و فروشگاه
}
}
نام فیلدهای داخل input فیلدهای پرداخت
به صورت پیشفرض در ووکامرس، در صفحه پرداخت نام فیلدها بالای هر فیلد نمایش داده میشود.
اما اگر میخواهید این نامها در داخل input فیلدها و به جای placeholder جایگزین شوند.
از این کد ساده اما کاربردی برای این اعمال چنین تغییری استفاده کنید. ابتدا صفحه پرداخت خود را ببینید.
با بررسی صفحه پرداخت قبل از درج این کد، میتوانید بهتر کار این قطعه را درک کنید.
/**
* @snippet Move Labels Inside Inputs - WooCommerce Checkout
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_checkout_fields', 'themefour_move_labels_inside_checkout_fields', 9999 );
function themefour_move_labels_inside_checkout_fields( $fields ) {
foreach ( $fields as $section => $section_fields ) {
foreach ( $section_fields as $section_field => $section_field_settings ) {
$fields[$section][$section_field]['placeholder'] = $fields[$section][$section_field]['label'];
$fields[$section][$section_field]['label'] = '';
}
}
return $fields;
}
نمایش تصویر عکس در صفحه پرداخت
با این ترفند فوق العاده کاربردی، میتوان در کنار نام محصول در صفحه پرداخت، تصویر آن را نمایش دهید.
نمایش تصویر محصول در صفحۀ تسویه حساب (پرداخت) به بهبود رابط کاربری فروشگاه شما کمک کند.
/**
* @snippet Product Images @ Woo Checkout
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
add_filter( 'woocommerce_cart_item_name', 'themefour_product_image_review_order_checkout', 9999, 3 );
function themefour_product_image_review_order_checkout( $name, $cart_item, $cart_item_key ) {
if ( ! is_checkout() ) return $name;
$product = $cart_item['data'];
$thumbnail = $product->get_image( array( '50', '50' ), array( 'class' => 'alignleft' ) );
return $thumbnail . $name;
}
ایجاد یک وضعیت دلخواه برای سفارشات
دوست دارید وضعیت دلخواه خود را برای سفارشات ایجاد کنید؟ این کد برای همین نوشته شده است.
شما میتوانید با استفاده از این کد یک وضعیت سفارشی برای محصولات خودتان ایجاد کنید.
/**
* @snippet Set Custom Order Status during Checkout
* @post WooCommerce Customization & Development Tricks - Part 2
* @developer themefour
* @testedwith WooCommerce 6.1
* @website https://themefour.com/
*/
// ---------------------
// ۱. Register Order Status
add_filter( 'woocommerce_register_shop_order_post_statuses', 'themefour_register_custom_order_status' );
function themefour_register_custom_order_status( $order_statuses ){
// Status must start with "wc-"
$order_statuses['wc-custom-status'] = array(
'label' => _x( 'وضعیت دلخواه', 'Order status', 'woocommerce' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'وضعیت تم فور (%s)', 'وضعیت تم فور (%s)', 'woocommerce' ),
);
return $order_statuses;
}
// ---------------------
// ۲. Show Order Status in the Dropdown @ Single Order and "Bulk Actions" @ Orders
add_filter( 'wc_order_statuses', 'themefour_show_custom_order_status' );
function themefour_show_custom_order_status( $order_statuses ) {
$order_statuses['wc-custom-status'] = _x( 'وضعیت تم فور', 'Order status', 'woocommerce' );
return $order_statuses;
}
add_filter( 'bulk_actions-edit-shop_order', 'themefour_get_custom_order_status_bulk' );
function themefour_get_custom_order_status_bulk( $bulk_actions ) {
// Note: "mark_" must be there instead of "wc"
$bulk_actions['mark_custom-status'] = 'تغییر وضعیت به تم فور';
return $bulk_actions;
}
// ---------------------
// ۳. Set Custom Order Status @ WooCommerce Checkout Process
add_action( 'woocommerce_thankyou', 'themefour_thankyou_change_order_status' );
function themefour_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;
$order = wc_get_order( $order_id );
// Status without the "wc-" prefix
$order->update_status( 'custom-status' );
}
نام وضعیت را به دلخواه خودتان داخل سورس کد تغییر بدهید و همۀ شخصی در اختیار شماست.
این آخرین مورد از ترفندهای ووکامرس بود اما افزونههای مختلفی برای ایجاد تغییر وضعیت نیز موجود هستند.
نمایش شناسه محصولهای متغیر
با قطعه کد زیر میتوانید شناسه یا همان SKU محصول را در لیست محصولات مدیریت مشاهده کنید.
/**
* @snippet Display Variation SKUs @ WooCommerce Product Admin
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_filter( 'woocommerce_product_get_sku', 'themefour_variable_product_skus_admin', 9999, 2 );
function themefour_variable_product_skus_admin( $sku, $product ) {
if ( ! is_admin() ) return $sku;
global $post_type, $pagenow;
if ( 'edit.php' === $pagenow && 'product' === $post_type ) {
if ( $product->is_type('variable') ) {
$sku = '';
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
if ( $variation && $variation->exists() ) $sku .= '(' . $variation->get_sku() . ') ';
}
}
}
return $sku;
}
توجه کنید که حتماً در هنگام افزودن متغیر، نیاز است که کد SKU را هرکدام از متغیرها وارد کرده باشید.
اگر محصولات متغیر شما دارای کد محصول یا SKU باشند در لیست محصولات پیشخوان نمایش داده میشوند.
ست کردن فیلد نام و نام خانوادگی کاربر
اگر میخواهید همان نام و نام خانوادگی که کاربر هنگام ثبت سفارش برای پروفایل او در نظر گرفته شود.
میتوانید از قطعه کد زیر برای این کار استفاده کنید تا فیلدهای نام و نام خانوادگی ادغام شوند.
/**
* @snippet Copy Billing to User @ WP User Edit Page
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_filter( 'pre_user_first_name', 'themefour_sync_user_edit_profile_edit_billing_firtname' );
function themefour_sync_user_edit_profile_edit_billing_firtname( $first_name ) {
if ( isset( $_POST['billing_first_name'] ) ) {
$first_name = $_POST['billing_first_name'];
}
return $first_name;
}
add_filter( 'pre_user_last_name', 'themefour_sync_user_edit_profile_edit_billing_last_name' );
function themefour_sync_user_edit_profile_edit_billing_last_name( $last_name ) {
if ( isset( $_POST['billing_last_name'] ) ) {
$last_name = $_POST['billing_last_name'];
}
return $last_name;
}
فیلد نام و نام خانوادگی صورت حساب ووکامرس برای اطلاعات حساب کاربری ست و تنظیم خواهد شد.
نمایش صفحه تشکر هر سفارش
با این قطعه کد، میتوان در هنگام مشاهده سفارش مشتری، صفحه تشکر و جزئیات سفارش را دید.
این گزینه میتواند به مدیریت برای بررسی صفحۀ تشکری که برای مشتری نمایش داده شده کمک کند.
/**
* @snippet View Thank You Page @ Edit Order Admin
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_filter( 'woocommerce_order_actions', 'themefour_show_thank_you_page_order_admin_actions', 9999, 2 );
function themefour_show_thank_you_page_order_admin_actions( $actions, $order ) {
if ( $order->has_status( wc_get_is_paid_statuses() ) ) {
$actions['view_thankyou'] = 'نمایش صفحۀ تشکر این سفارش';
}
return $actions;
}
add_action( 'woocommerce_order_action_view_thankyou', 'themefour_redirect_thank_you_page_order_admin_actions' );
function themefour_redirect_thank_you_page_order_admin_actions( $order ) {
$url = $order->get_checkout_order_received_url();
add_filter( 'redirect_post_location', function() use ( $url ) {
return $url;
});
}
نمایش تعداد فروشهای هر محصول
این ترفند، ترفندی پرطرفدار و جذاب است و بیشتر افراد دنبال نمایش تعداد فروشهای محصولات هستند.
با استفاده از قطعه کد زیر، میتوانید تعداد فروشهای هر محصول را در صفحه محصول نمایش دهید.
/**
* @snippet Show Total Sales @ WooCommerce Single Product
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_action( 'woocommerce_single_product_summary', 'themefour_product_sold_count', 11 );
function themefour_product_sold_count() {
global $product;
$total_sold = get_post_meta( $product->id, 'total_sales', true );
if ( $total_sold ) echo '
' . sprintf( __( '%s', 'woocommerce' ), $total_sold ) . ' فروش
';
}
توجه کنید که اگر محصول فروشی نداشته باشد، باکس تعداد فروش نمایش داده نخواهد شد.
همچنین همانطور که در قطعه کد مشخص است، میتوانید استایل CSS باکس را نیز کاستوم کنید.
تغییر متن دکمه افزودن به سبد خرید
اگر میخواهید متن پیشفرض افزودن به سبد خرید صفحۀ محصول را تغییر دهید این قطعه برای همین کار است.
توسط کد زیر، میتوانید به راحتی متن دلخواه خود را جایگزین متن افزودن به سبد خرید صفحۀ محصول کنید.
/**
* @snippet Change Add to Cart Button txt @ WooCommerce Single Product
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'themefour_custom_single_add_to_cart_text' );
function themefour_custom_single_add_to_cart_text() {
return __( 'پرداخت و دانلود سریع', 'woocommerce' );
}
این کد، تنها متن افزودن به سبد خرید صفحۀ محصول را تغییر میدهد و متن سایر دکمهها تغییر نمییابد.
این قطعه کد از ترفندهای ووکامرس، توسعه از پرسرچترین موارد در موتورهای جستجو است.
مخفی کردن قیمت محصولات از مهمانان
با این ترفند کاربردی، میتوانید قیمت محصولات را از کاربرانی که عضو سایت نیستند مخفی کنید.
قیمت محصول از صفحۀ محصول و صفحات فروشگاه مخفی و یک پیام جایگزین قیمت خواهد شد.
برای دیدن این کد، لطفاً در تم فور ثبت نام کنید یا وارد حساب کاربری خود شوید!
در این قطعه کد، شما میتوانید استایلهای CSS پیام و متن پیام را به دلخواه خودتان تغییر دهید.
کاربرانی که عضو سایت باشند و لاگین کرده باشند، میتوانند قیمتها را به روال سابق مشاهده کنند.
غیرفعال کردن درگاه پرداخت برای نقش خاص
اگر میخواهید برای یک نقش خاص، درگاه پرداخت مثلاً زرین پال را غیرفعال کنید، این کد برای شماست.
شما میتوانید با استفاده از کد زیر، درگاه پرداخت زرین پال و pay را به سادگی براساس نقش شرطی کنید.
/**
* @snippet Disable Payment Gateway for a Specific User Role | WooCommerce
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_filter( 'woocommerce_available_payment_gateways', 'themefour_gateway_disable_by_role' );
function themefour_gateway_disable_by_role( $available_gateways ) {
// غیرفعال کردن درگاه زرین پال برای کاربران با نقش مشتری
if ( isset( $available_gateways['wc_zpal'] ) && current_user_can( 'customer' ) ) {
unset( $available_gateways['wc_zpal'] );
}
// غیرفعال کردن درگاه پی دات آی آر برای کاربران با نقش مشتری
if ( isset( $available_gateways['payir'] ) && current_user_can( 'customer' ) ) {
unset( $available_gateways['payir'] );
}
return $available_gateways;
}
متن customer همان نقش است که نقش مشتریان ووکامرس است که میتوانید به دلخواه تغییر بدهید.
همۀ این کدها مثال و نمونه هستند و جهت آشنایی شما عزیزان با نحوۀ ایجاد تغییرات در ووکامرس است.
عملاً با این ترفندهای ووکامرس، توسعه و شخصی سازی شما در حال اعمال تغییراتی در هسته آن هستید.
جمع بندی آموزش ترفندهای ووکامرس
شخصیسازی و تغییر این قطعه کدها به عهده خودتان و براساس نیازتان از آنها در وردپرس استفاده کنید و امیدواریم مقالۀ ترفندهای ووکامرس، توسعه و شخصی سازی نیازهایتان را برطرف کرده باشد.
این مقاله چقدر براتون مفید بود؟
از ۱ تا ۵ امتیاز بدید
میانگین رتبه ۵ / ۵. تعداد رای: ۶