در بخش سوم آموزش و معرفی ترفندهای ووکامرس، توسعه و شخصی سازی با شما همراهان هستیم.
اگر قسمتهای قبلی این آموزش سریالی را از دست دادهاید، نگران نباشید در آرشیو تم فور سرچ کنید.
در این قسمت، به چند ترفند جذاب و کاربردی از ترفندهای ووکامرس میپردازیم که فروشگاه شما را متفاوت و منحصرفرد میکند.
با قطعه کد زیر میتوانید شناسه یا همان 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' );
}
این کد، تنها متن افزودن به سبد خرید صفحۀ محصول را تغییر میدهد و متن سایر دکمهها تغییر نمییابد.
این قطعه کد از ترفندهای ووکامرس، توسعه از پرسرچترین موارد در موتورهای جستجو است.
با این ترفند کاربردی، میتوانید قیمت محصولات را از کاربرانی که عضو سایت نیستند مخفی کنید.
قیمت محصول از صفحۀ محصول و صفحات فروشگاه مخفی و یک پیام جایگزین قیمت خواهد شد.

/**
* @snippet Hide Price & Add to Cart for Logged Out Users
* @post WooCommerce Customization & Development Tricks - Part 3
* @author themefour.com
* @testedwith WooCommerce 6.2
* @website https://themefour.com/
*/
add_filter( 'woocommerce_get_price_html', 'themefour_hide_price_addcart_not_logged_in', 9999, 2 );
function themefour_hide_price_addcart_not_logged_in( $price, $product ) {
if ( ! is_user_logged_in() ) {
$price = '';
echo '';
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
return $price;
}

در این قطعه کد، شما میتوانید استایلهای 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 همان نقش است که نقش مشتریان ووکامرس است که میتوانید به دلخواه تغییر بدهید.

همۀ این کدها مثال و نمونه هستند و جهت آشنایی شما عزیزان با نحوۀ ایجاد تغییرات در ووکامرس است.
عملاً با این ترفندهای ووکامرس، توسعه و شخصی سازی شما در حال اعمال تغییراتی در هسته آن هستید.
پایان بخش سوم آموزش، ترفندهای ووکامرس، توسعه و شخصی سازی
شخصیسازی و تغییر این قطعه کدها به عهده خودتان و براساس نیازتان از آنها در وردپرس استفاده کنید.
امیدواریم این قسمت از ترفندهای ووکامرس، توسعه و شخصی سازی نیازهایتان را برطرف کرده باشد.
این مقاله چقدر براتون مفید بود؟
از ۱ تا ۵ امتیاز بدید
میانگین رتبه ۵ / ۵. تعداد رای: ۴