Worpdress kullanıcıları ya da WordPress geliştiricileri için hazırladığımız kodları kullanmanız için sizlerle paylaşıyoruz.
WooCommerce’e Özel Para Birimi Ekleme
Varsayılan olarak WooCommerce WooCommerce 2.0+ sürümünde özel bir para birimi eklemek için, bu kodu kopyalayıp tema function.php dosyanıza yapıştırın ve para birimi kodunu ve sembolünü kendi para biriminizle değiştirin. Değişiklikleri kaydettikten sonra WooCommerce ayarlarınızda mevcut olmalıdır.
add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );
function add_my_currency( $currencies ) {
$currencies[‘ABC’] = __( ‘Currency name’, ‘woocommerce’ );
return $currencies;
}add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);
function add_my_currency_symbol( $currency_symbol, $currency )
{
switch( $currency ) {
case ‘ABC’:
$currency_symbol = ‘$’;
break;
}
return $currency_symbol;
}
Tek ürün sayfasında ürün açıklamasını kaldırın
remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_meta’, 40 );
Ürün fiyatındaki sıfır ondalık sayıları kaldırın
add_filter( ‘woocommerce_price_trim_zeros’, ‘__return_true’ );
Sepet sayfasında miktarı gizle
function remove_quantity_column( $return, $product ) {
if ( is_cart() ) return true;
}
add_filter( ‘woocommerce_is_sold_individually’, ‘remove_quantity_column’, 10, 2 );
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 faturalandırma ödeme alanlarını ürün kimliğine göre göster
add_action( 'woocommerce_checkout_fields', 'hqhowdotcom_cutom_checkout_field_conditional_logic' );function hqhowdotcom_cutom_checkout_field_conditional_logic( $fields ) {foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];//change 2020 to your product id
if( $product_id == 2020 ) {
$fields['billing']['billing_field_' . $product_id] = array(
'label' => __('Custom Field on Checkout for ' . $product_id, 'woocommerce'),
'placeholder' => _x('Custom Field on Checkout for ' . $product_id, 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
}}// Return checkout fields.
return $fields;}
Tüm gönderim yöntemlerini gizle ancak ücretsiz gönderim
Kullanıcı deneyiminde, mümkün olduğunda ücretsiz gönderim yöntemini otomatik olarak uygulamalısınız; bu, müşterilerin satın alma işleminizde daha rahat hissetmelerine yardımcı olur.
Aşağıdaki kod parçacığı bunu yapmanıza yardımcı olacaktır:
function only_show_free_shipping_when_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Only modify rates if free_shipping is present.
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}if ( ! empty( $new_rates ) ) {
//Save local pickup if it's present.
foreach ( $rates as $rate_id => $rate ) {
if ('local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
return $new_rates;
}return $rates;
}add_filter( 'woocommerce_package_rates', 'only_show_free_shipping_when_available', 10, 2 );
Tek ürün sayfasındaki ürün sekmesini kaldırın
add_filter( ‘woocommerce_product_tabs’, ‘remove_product_tabs’, 98 );function remove_product_tabs( $tabs ) {
// Remove the additional information tab
unset( $tabs[‘additional_information’] );
return $tabs;
}
Ülke listesine yeni bir ülke ekle
function woo_add_my_country( $country ) {
$country[“AEDUB”] = ‘Dubai’;
return $country;
}add_filter( ‘woocommerce_countries’, ‘woo_add_my_country’, 10, 1 );
Breadcrumbs kısmını kaldır
add_action( ‘init’, ‘remove_wc_breadcrumbs’ );function remove_wc_breadcrumbs() { remove_action( ‘woocommerce_before_main_content’, ‘woocommerce_breadcrumb’, 20, 0 );}
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 “My new title”;
}
return $title;
}
Sepete ekledikten sonra ödeme sayfasına yönlendir
add_action( ‘add_to_cart_redirect’, ‘add_to_cart_checkout_redirect’, 16 );
function add_to_cart_checkout_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
Ürün kategorilerini mağaza sayfasından kaldırın
add_action( ‘pre_get_posts’, ‘remove_categories_shop’ );function remove_categories_shop( $q ) {if ( ! $q->is_main_query() ) return;if ( ! $q->is_post_type_archive() ) return;if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {$q->set( ‘tax_query’, array(array(‘taxonomy’ => ‘product_cat’,‘field’ => ‘slug’,// Don’t display products in these categories on the shop page
‘terms’ => array( ‘color’, ‘flavor’, ‘spices’, ‘vanilla’ ),‘operator’ => ‘NOT IN’)));
}remove_action( ‘pre_get_posts’, ‘remove_categories_shop’ );}
Şirket adını WooCommerce ödeme sayfasından kaldırma
add_filter( ‘woocommerce_checkout_fields’ , ‘remove_company_name’ ); function remove_company_name( $fields ) {
unset($fields[‘billing’][‘billing_company’]);
return $fields;
}
WooCommerce Checkout’taki durum alanını kaldırın
function remove_state_field( $fields ) {
unset( $fields[‘state’] );
return $fields;
}
add_filter( ‘woocommerce_default_address_fields’, ‘remove_state_field’ );
Herhangi bir kelimeyi hızla çevirin
add_filter(‘gettext’, ‘translate_text’);
add_filter(‘ngettext’, ‘translate_text’);
function translate_text($translated) {
$translated = str_ireplace(‘Choose and option’, ‘Select’, $translated);
return $translated;
}
Bir kategoriyi WooCommerce kategori bileşenlerden 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’);
return $cat_args;
}
“Stokta yok” ifadesini “satıldı” olarak değiştirin
add_filter(‘woocommerce_get_availability’, ‘availability_filter_func’);
function availability_filter_func($availability)
{
$availability[‘availability’] = str_ireplace(‘Out of stock’, ‘Sold’, $availability[‘availability’]);
return $availability;
}
“Sepete ekle” düğmesi yerine “ürün zaten sepette” ifadesini görüntüle
/**
* Change the add to cart text on single product pages
*/
add_filter( ‘woocommerce_product_single_add_to_cart_text’, ‘woo_custom_cart_button_text’ );
function woo_custom_cart_button_text() {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[‘data’];
if( get_the_ID() == $_product->id ) {
return __(‘Already in cart – Add Again?’, ‘woocommerce’);
}
}
return __(‘Add to cart’, ‘woocommerce’);
}
/**
* Change the add to cart text on product archives
*/
add_filter( ‘add_to_cart_text’, ‘woo_archive_custom_cart_button_text’ );
function woo_archive_custom_cart_button_text() {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[‘data’];
if( get_the_ID() == $_product->id ) {
return __(‘Already in cart’, ‘woocommerce’);
}
}
return __(‘Add to cart’, ‘woocommerce’);
}
Kategori görünümünde ürün sayısını gizle
add_filter( ‘woocommerce_subcategory_count_html’, ‘woo_remove_category_products_count’ );
function woo_remove_category_products_count() {
return;
}
Hesap ödeme alanlarını gerekli kılın
add_filter( ‘woocommerce_checkout_fields’, ‘woo_filter_account_checkout_fields’ );
function woo_filter_account_checkout_fields( $fields ) {
$fields[‘account’][‘account_username’][‘required’] = true;
$fields[‘account’][‘account_password’][‘required’] = true;
$fields[‘account’][‘account_password-2’][‘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) {
$tabs[‘description’][‘title’] = ‘More info’;
return $tabs;
}
Ürün varyasyonuna özel alan ekleme
//Display Fields
add_action( ‘woocommerce_product_after_variable_attributes’, ‘variable_fields’, 10, 2 );
//JS to add fields for new variations
add_action( ‘woocommerce_product_after_variable_attributes_js’, ‘variable_fields_js’ );
//Save variation fields
add_action( ‘woocommerce_process_product_meta_variable’, ‘variable_fields_process’, 10, 1 );
function variable_fields( $loop, $variation_data ) { ?>
<tr>
<td>
<div>
<label></label>
<input type=”text” size=”5″ name=”my_custom_field[]” value=””/>
</div>
</td>
</tr>
<tr>
<td>
<div>
<label></label>
</div>
</td>
</tr>
<?php }
function variable_fields_process( $post_id ) {
if (isset( $_POST[‘variable_sku’] ) ) :
$variable_sku = $_POST[‘variable_sku’];
$variable_post_id = $_POST[‘variable_post_id’];
$variable_custom_field = $_POST[‘my_custom_field’];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, ‘_my_custom_field’, stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
WooCommerce ürün kategorilerini listeleyin
$args = array(
‘number’ => $number,
‘orderby’ => $orderby,
‘order’ => $order,
‘hide_empty’ => $hide_empty,
‘include’ => $ids
);
$product_categories = get_terms( ‘product_cat’, $args );
$count = count($product_categories);
if ( $count > 0 ){
echo “<ul>”;
foreach ( $product_categories as $product_category ) {
echo ‘<li><a href=”‘ . get_term_link( $product_category ) . ‘”>’ . $product_category->name . ‘</li>’;
}
echo “</ul>”;
}
“Gönderen” e-posta adresini değiştir
function woo_custom_wp_mail_from_name() {
global $woocommerce;
return html_entity_decode( get_option( ‘woocommerce_email_from_name’ ) );
}
add_filter( ‘wp_mail_from_name’, ‘woo_custom_wp_mail_from_name’, 99 );
function woo_custom_wp_mail_from() {
global $woocommerce;
return html_entity_decode( get_option( ‘woocommerce_email_from’ ) );
}
add_filter( ‘wp_mail_from_name’, ‘woo_custom_wp_mail_from_name’, 99 );
Öne çıkan ürün kimliklerini döndür
function woo_get_featured_product_ids() {
// Load from cache
$featured_product_ids = get_transient( ‘wc_featured_products’ );
// Valid cache found
if ( false !== $featured_product_ids )
return $featured_product_ids;
$featured = get_posts( array(
‘post_type’ => array( ‘product’, ‘product_variation’ ),
‘posts_per_page’ => -1,
‘post_status’ => ‘publish’,
‘meta_query’ => array(
array(
‘key’ => ‘_visibility’,
‘value’ => array(‘catalog’, ‘visible’),
‘compare’ => ‘IN’
),
array(
‘key’ => ‘_featured’,
‘value’ => ‘yes’
)
),
‘fields’ => ‘id=>parent’
) );
$product_ids = array_keys( $featured );
$parent_ids = array_values( $featured );
$featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) );
set_transient( ‘wc_featured_products’, $featured_product_ids );
return $featured_product_ids;
}
Minimum sipariş tutarını belirleyin
add_action( ‘woocommerce_checkout_process’, ‘wc_minimum_order_amount’ );function wc_minimum_order_amount() { global $woocommerce; $minimum = 50; if ( $woocommerce->cart->get_cart_total(); < $minimum ) { $woocommerce->add_error( sprintf( ‘You must have an order with a minimum of %s to place your order.’ , $minimum ) ); }}
Mağaza sayfasında fiyata, tarihe veya başlığa göre sıralayın
add_filter(‘woocommerce_default_catalog_orderby’, ‘custom_default_catalog_orderby’);
function custom_default_catalog_orderby() {
return ‘date’; // Can also use title and price
}
Sipariş tamamlandığında e-posta alıcısını ekle
function woo_extra_email_recipient($recipient, $object) {
$recipient = $recipient . ‘, your@email.com’;
return $recipient;
}
add_filter( ‘woocommerce_email_recipient_customer_completed_order’, ‘woo_extra_email_recipient’, 10, 2);
Telefon numarasının gerekli olmamasını sağlayı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;
}