WooCommerce, WordPress'e e-ticaret yetenekleri ekleyen eklentidir. Ürün verisi hem WordPress'in post tablosunda hem de kendi meta tablolarında tutulur.
Ürün verisi nereden gelir?
wp_posts → urun basligi, icerik, durum
wp_postmeta → fiyat, stok, sku
woocommerce_product_meta → ek alanlar
wp_terms → kategori, etiket
woocommerce_order_items → siparis kalemleri
Ürün meta verisine erişim
<?php
$urunId = 42;
$ad = get_the_title($urunId);
$sk = get_post_meta($urunId, '_sku', true);
$fiyat = (float) get_post_meta($urunId, '_price', true);
$stok = (int) get_post_meta($urunId, '_stock', true);
if ($fiyat > 0) {
$fiyat = wc_get_price_to_display($urunId);
}
Özel alan ekleme
<?php
// Urun kaydedildiginde
add_action('woocommerce_process_product_meta', 'ozel_alan_kaydet');
function ozel_alan_kaydet(int $urunId): void
{
$garanti = isset($_POST['garanti_yil'])
? absint($_POST['garanti_yil'])
: 0;
// Yonlendirme tabanli guvenli yazim
if (isset($_POST['garanti_yil'])) {
update_post_meta(
$urunId,
'_garanti_yil',
wc_sanitize_textarea_field(wp_unslash($_POST['garanti_yil']))
);
}
}
// Okuma
$garanti = (int) get_post_meta($urunId, '_garanti_yil', true);
Ürün tablosunda alan gösterme
<?php
// Yeni sutun
add_filter('manage_edit-product_columns',
function (array $sutunlar): array {
$yeni = [];
foreach ($sutunlar as $anahtar => $baslik) {
$yeni[$anahtar] = $baslik;
if ($anahtar === 'price') {
$yeni['garanti'] = 'Garanti';
}
}
return $yeni;
});
add_action('manage_product_posts_custom_column',
function ($sutun, $anahtar) {
if ($anahtar === 'garanti') {
$yil = (int) get_post_meta($sutun, '_garanti_yil', true);
echo $yil ? esc_html($yil . ' yıl') : '—';
}
}, 10, 2);
Sipariş işleme
<?php
add_action('woocommerce_checkout_order_processed', 'siparis_hazirla', 10, 3);
function siparis_hazirla(int $siparisId, array $gonderim, ?string $odemeYontemi): void
{
$siparis = wc_get_order($siparisId);
foreach ($siparis->get_items() as $kalem) {
$urunId = $kalem->get_product_id();
$adet = $kalem->get_quantity();
// Stok azalt
wc_reduce_stock_levels($urunId);
// Indirim kodu kullanildi mi
$kod = $siparis->get_coupon_codes();
if ($kod) {
error_log('Indirim kodu kullanildi: ' . implode(', ', $kod));
}
}
}
Katalog sorguları
<?php
// Ucuz sorgu: WC_Product_Query
$sorgu = new WC_Product_Query([
'limit' => 12,
'status' => 'publish',
'category' => ['php'],
'orderby' => 'date',
'order' => 'DESC',
'return' => 'objects',
]);
$urunler = $sorgu->get_products();
$toplam = $sorgu->get_total();
// N+1 sorgudan kacinin
wp_cache_set('kategori_urunleri_' . $kategoriId, $sonuclar, 'woocommerce', 3600);
Webhook ile bildirim
<?php
// Siparis olustugunda webhook tetikle
add_action('woocommerce_new_order', 'siparis_webhook', 20, 1);
function siparis_webhook(int $siparisId): void
{
$siparis = wc_get_order($siparisId);
$veri = [
'siparis_no' => $siparis->get_order_number(),
'tutar' => $siparis->get_total(),
'urunler' => array_map(
fn($k) => $k->get_name() . ' x' . $k->get_quantity(),
iterator_to_array($siparis->get_items())
),
];
wp_remote_post('https://app.site.com/api/webhook/siparis', [
'headers' => ['Content-Type' => 'application/json'],
'body' => wp_json_encode($veri),
'timeout' => 5,
]);
}
Performans uyarıları
- Ürün döngüsünde
get_post_meta çağırmak yerine toplu sorgu kullanın.
wc_get_products yerine WC_Product_Query tercih edin (eski API).
- Ağır sorguları geçici olarak (transient) önbelleğe alın.
- Her sayfa yüklemesinde
get_post_meta çağırmayın; meta sorgusunu bir kez çalıştırın.
Sonuç
WooCommerce'de doğru yaklaşım, veriyi meta anahtarlarından okumak ve özel alanları wc_sanitize_* fonksiyonlarıyla temizlemektir. Ürün döngüleri en sık performans sorununun kaynağıdır.
Arivo'da PHP scriptler, temalar ve eklentiler bulun. Kurulum desteği dahil.
Mağazaya Git