2 min read

☁ Cloud Server Wordpress - Woocommerce Snippet Collection 🥥

Iman Sugirman

☁ Cloud Server Wordpress - Woocommerce Snippet Collection 🥥

Woocommerce adalah Platform Powerfull untuk Ecommerce, cocok untuk dipakai di Indonesia dan sudah banyak yang pakai Woocommerce. Meskipun ini adalah Plugins tetapi sangat komplit untuk kebutuhan Toko Online kita.

Berikut adalah Snippet-Snippet Yang saya tau untuk Memodifikasi Woocommerce anda :

Ubah Tautan Tombol "Kembali ke Belanja" - WooCommerce

add_filter( 'woocommerce_return_to_shop_redirect', 'wpstak_change_return_shop_url' );

function wpstak_change_return_shop_url() {
return home_url();
}

Edit Pengalihan Tautan "Lanjutkan Belanja"

add_filter( 'woocommerce_continue_shopping_redirect', 'wpstak_change_continue_shopping' );

function wpstak_change_continue_shopping() {
   return wc_get_page_permalink( 'shop' );
}

Ganti nama "Tambahkan ke Troli" Tombol jika Produk Sudah @ Keranjang

// Part 1
// Edit Single Product Page Add to Cart

add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpstak_custom_add_cart_button_single_product' );

function wpstak_custom_add_cart_button_single_product( $label ) {

   foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
      $product = $values['data'];
      if( get_the_ID() == $product->get_id() ) {
         $label = __('Already in Cart. Add again?', 'woocommerce');
      }
   }

   return $label;

}

// Part 2
// Edit Loop Pages Add to Cart

add_filter( 'woocommerce_product_add_to_cart_text', 'wpstak_custom_add_cart_button_loop', 99, 2 );

function wpstak_custom_add_cart_button_loop( $label, $product ) {

   if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {

      foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
         $_product = $values['data'];
         if( get_the_ID() == $_product->get_id() ) {
            $label = __('Already in Cart. Add again?', 'woocommerce');
         }
      }

   }

   return $label;

}

Hapus Tambah Keranjang, Tambahkan Lihat Produk @ Halaman Toko

// First, remove Add to Cart Button

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

// Second, add View Product Button

add_action( 'woocommerce_after_shop_loop_item', 'wpstak_view_product_button', 10 );

function wpstak_view_product_button() {
global $product;
$link = $product->get_permalink();
echo '<a href="' . $link . '" class="button addtocartbutton">View Product</a>';
}

Ganti nama Label Menu "Akun Saya" Jika Keluar

add_filter( 'wp_nav_menu_items', 'dynamic_label_change', 10, 2 );

function dynamic_label_change( $items, $args ) {
   if ( ! is_user_logged_in() ) {
      $items = str_replace( "My Account", "Login", $items );
   }
   return $items;
}