İçeriğe geç
Ad Maxima
computer screen with word code it 1

WooCommerce Kodları

·7 dk okumaFacebook Reklamları

WordPress kullanıcıları ve geliştiricileri için hazırladığımız WooCommerce kod parçacıklarını aşağıda paylaşıyoruz. Kodları temanızın (tercihen alt temanın) functions.php dosyasına ya da WPCode gibi bir kod parçacığı eklentisine ekleyin. Canlı siteye eklemeden önce yedek alın ve kodu bir test ortamında deneyin.

WooCommerce’e özel para birimi ekleme

Özel bir para birimi eklemek için aşağıdaki kodda ABC kodunu, para birimi adını ve sembolünü kendi para biriminizle değiştirin. Kaydettikten sonra yeni para birimi WooCommerce > Ayarlar > Genel bölümünde seçilebilir olur.

add_filter( 'woocommerce_currencies', 'add_my_currency' );
function add_my_currency( $currencies ) {
$currencies['ABC'] = __( 'Para birimi adı', 'woocommerce' );
return $currencies;
}

add_filter( 'woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2 );
function add_my_currency_symbol( $currency_symbol, $currency ) {
if ( 'ABC' === $currency ) {
$currency_symbol = '$';
}
return $currency_symbol;
}

Tek ürün sayfasında ürün açıklamasını kaldırın

İlk kod ürün açıklaması sekmesini, ikinci kod fiyatın altındaki kısa açıklamayı kaldırır. İhtiyacınız olanı kullanın.

// Ürün açıklaması sekmesini kaldırır
add_filter( 'woocommerce_product_tabs', 'remove_description_tab', 98 );
function remove_description_tab( $tabs ) {
unset( $tabs['description'] );
return $tabs;
}

// Fiyatın altındaki kısa açıklamayı kaldırır
add_action( 'init', 'remove_short_description' );
function remove_short_description() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
}

Ürün fiyatındaki sıfır ondalık sayıları kaldırın

Fiyatlar 100,00 TL yerine 100 TL olarak görünür.

add_filter( 'woocommerce_price_trim_zeros', '__return_true' );

Sepet sayfasında miktarı gizle

add_filter( 'woocommerce_is_sold_individually', 'remove_quantity_column', 10, 2 );
function remove_quantity_column( $sold_individually, $product ) {
if ( is_cart() ) {
return true;
}
return $sold_individually;
}

WooCommerce sipariş notu uzunluğunu sınırlayın

add_filter( 'woocommerce_checkout_fields', 'limit_order_note_length' );
function limit_order_note_length( $fields ) {
$fields['order']['order_comments']['maxlength'] = 200;
return $fields;
}

Özel fatura alanlarını ürün ID’sine göre gösterin

Sepette belirli bir ürün varsa ödeme sayfasına ek bir fatura alanı ekler.

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_field_conditional_logic' );
function custom_checkout_field_conditional_logic( $fields ) {
if ( ! WC()->cart ) {
return $fields;
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
// 2020 yerine kendi ürün ID'nizi yazın
if ( 2020 === $product_id ) {
$fields['billing'][ 'billing_field_' . $product_id ] = array(
'label' => __( 'Ürüne özel alan', 'woocommerce' ),
'placeholder' => __( 'Ürüne özel alan', 'woocommerce' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true,
);
}
}
return $fields;
}

Ücretsiz gönderim varken diğer gönderim yöntemlerini gizleyin

Ücretsiz gönderim mümkün olduğunda onu otomatik olarak tek seçenek yapmak kullanıcı deneyimini iyileştirir; müşteri ücretli bir yöntemi yanlışlıkla seçmez. Aşağıdaki kod mağazadan teslim alma seçeneğini korur.

add_filter( 'woocommerce_package_rates', 'only_show_free_shipping_when_available', 100, 2 );
function only_show_free_shipping_when_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
}
if ( empty( $new_rates ) ) {
return $rates;
}
// Mağazadan teslim alma seçeneği varsa onu da koru
foreach ( $rates as $rate_id => $rate ) {
if ( 'local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
}
return $new_rates;
}

Tek ürün sayfasındaki ek bilgi sekmesini kaldırın

add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 );
function remove_product_tabs( $tabs ) {
unset( $tabs['additional_information'] );
return $tabs;
}

Ülke listesine yeni bir ülke ekleyin

add_filter( 'woocommerce_countries', 'woo_add_my_country' );
function woo_add_my_country( $countries ) {
$countries['AEDUB'] = __( 'Dubai', 'woocommerce' );
return $countries;
}

add_filter( 'woocommerce_continents', 'woo_add_my_country_to_continent' );
function woo_add_my_country_to_continent( $continents ) {
$continents['AS']['countries'][] = 'AEDUB';
return $continents;
}

Breadcrumb (içerik haritası) alanını kaldırın

add_action( 'init', 'remove_wc_breadcrumbs' );
function remove_wc_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
}

Mağaza sayfası başlığını değiştirin

add_filter( 'woocommerce_page_title', 'shop_page_title' );
function shop_page_title( $title ) {
if ( is_shop() ) {
return 'Yeni mağaza başlığı';
}
return $title;
}

Sepete ekledikten sonra ödeme sayfasına yönlendirin

Kodun çalışması için WooCommerce > Ayarlar > Ürünler bölümünde “Arşivlerde AJAX ile sepete ekle” seçeneğini kapatın.

add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirect' );
function add_to_cart_checkout_redirect( $url ) {
return wc_get_checkout_url();
}

Belirli kategorileri mağaza sayfasından gizleyin

add_action( 'woocommerce_product_query', 'remove_categories_shop' );
function remove_categories_shop( $q ) {
if ( ! is_shop() ) {
return;
}
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'kategori-1', 'kategori-2' ), // Gizlenecek kategori slug'ları
'operator' => 'NOT IN',
);
$q->set( 'tax_query', $tax_query );
}

Şirket adı alanını ödeme sayfasından kaldırın

add_filter( 'woocommerce_checkout_fields', 'remove_company_name' );
function remove_company_name( $fields ) {
unset( $fields['billing']['billing_company'] );
return $fields;
}

Ödeme sayfasındaki il (state) alanını kaldırın

add_filter( 'woocommerce_checkout_fields', 'remove_state_field' );
function remove_state_field( $fields ) {
unset( $fields['billing']['billing_state'] );
unset( $fields['shipping']['shipping_state'] );
return $fields;
}

Herhangi bir ifadeyi hızlıca çevirin

Kalıcı ve kapsamlı çeviriler için Loco Translate gibi bir eklenti kullanmanızı öneririz.

add_filter( 'gettext', 'translate_text', 20, 3 );
function translate_text( $translated, $text, $domain ) {
if ( 'woocommerce' === $domain && 'Choose an option' === $text ) {
$translated = 'Seçiniz';
}
return $translated;
}

Bir kategoriyi ürün kategorileri bileşeninden hariç tutun

add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array( 16 ); // Hariç tutulacak kategori ID'si
return $cat_args;
}

“Stokta yok” ifadesini “Satıldı” olarak değiştirin

add_filter( 'woocommerce_get_availability_text', 'availability_filter_func', 10, 2 );
function availability_filter_func( $availability, $product ) {
if ( ! $product->is_in_stock() ) {
$availability = 'Satıldı';
}
return $availability;
}

Ürün sepetteyse “Sepete ekle” yerine “Ürün zaten sepette” yazın

Kod hem tek ürün sayfasında hem de ürün listelerinde çalışır.

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text', 10, 2 );
function woo_custom_cart_button_text( $text, $product ) {
if ( WC()->cart ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( (int) $cart_item['product_id'] === $product->get_id() ) {
return __( 'Ürün zaten sepette', 'woocommerce' );
}
}
}
return $text;
}

Kategori görünümünde ürün sayısını gizleyin

add_filter( 'woocommerce_subcategory_count_html', '__return_empty_string' );

Ödeme sayfasındaki hesap alanlarını zorunlu yapın

add_filter( 'woocommerce_checkout_fields', 'woo_filter_account_checkout_fields' );
function woo_filter_account_checkout_fields( $fields ) {
if ( isset( $fields['account']['account_username'] ) ) {
$fields['account']['account_username']['required'] = true;
}
if ( isset( $fields['account']['account_password'] ) ) {
$fields['account']['account_password']['required'] = true;
}
return $fields;
}

Ürün sekmesini yeniden adlandırın

add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98 );
function woo_rename_tab( $tabs ) {
if ( isset( $tabs['description'] ) ) {
$tabs['description']['title'] = 'Daha fazla bilgi';
}
return $tabs;
}

Ürün varyasyonuna özel alan ekleyin

// Varyasyon düzenleme ekranına alan ekler
add_action( 'woocommerce_product_after_variable_attributes', 'variation_custom_field', 10, 3 );
function variation_custom_field( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'variation_custom_field[' . $loop . ']',
'label' => __( 'Özel alan', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variation_custom_field', true ),
) );
}

// Alanı kaydeder
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_field', 10, 2 );
function save_variation_custom_field( $variation_id, $i ) {
if ( isset( $_POST['variation_custom_field'][ $i ] ) ) {
$value = sanitize_text_field( wp_unslash( $_POST['variation_custom_field'][ $i ] ) );
update_post_meta( $variation_id, '_variation_custom_field', $value );
}
}

WooCommerce ürün kategorilerini listeleyin

Bu kodu kategorileri göstermek istediğiniz tema şablonuna ekleyin.

$product_categories = get_terms( array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
) );

if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) {
echo '<ul>';
foreach ( $product_categories as $product_category ) {
echo '<li><a href="' . esc_url( get_term_link( $product_category ) ) . '">' . esc_html( $product_category->name ) . '</a></li>';
}
echo '</ul>';
}

“Gönderen” adını ve e-posta adresini değiştirin

WordPress’in tüm e-postaları WooCommerce ayarlarındaki gönderen adı ve adresiyle gider. WP Mail SMTP gibi bir eklenti kullanıyorsanız gönderen bilgisini o eklentinin ayarlarından da kontrol edin.

add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );
function woo_custom_wp_mail_from_name( $name ) {
return wp_specialchars_decode( get_option( 'woocommerce_email_from_name' ), ENT_QUOTES );
}

add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );
function woo_custom_wp_mail_from( $email ) {
return sanitize_email( get_option( 'woocommerce_email_from_address' ) );
}

Öne çıkan ürünlerin ID’lerini alın

// WooCommerce'in hazır fonksiyonu (önbellekli)
$featured_ids = wc_get_featured_product_ids();

// Alternatif: yalnızca yayımlanmış öne çıkan ürünler
$featured_ids = wc_get_products( array(
'featured' => true,
'status' => 'publish',
'limit' => -1,
'return' => 'ids',
) );

Minimum sipariş tutarı belirleyin

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 50; // Minimum sipariş tutarı
if ( ! WC()->cart ) {
return;
}
$total = (float) WC()->cart->get_cart_contents_total();
if ( $total < $minimum ) {
$message = sprintf(
'Sipariş verebilmek için minimum tutar %s. Sepet tutarınız: %s.',
wc_price( $minimum ),
wc_price( $total )
);
if ( is_cart() ) {
wc_print_notice( $message, 'error' );
} else {
wc_add_notice( $message, 'error' );
}
}
}

Mağaza sayfasındaki varsayılan sıralamayı değiştirin

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' );
function custom_default_catalog_orderby( $orderby ) {
return 'date'; // Diğer seçenekler: 'price', 'price-desc', 'popularity', 'rating', 'menu_order'
}

Sipariş tamamlandığında e-postaya ek alıcı ekleyin

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2 );
function woo_extra_email_recipient( $recipient, $order ) {
return $recipient . ', ornek@alanadiniz.com';
}

Telefon numarasını zorunlu olmaktan çıkarın

add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}

Diğer Yazılar

ÜCRETSİZ ANALİZ

Markanızı maksimuma taşıyalım.

Hemen Ara WhatsApp