/*
 Theme Name:   Woodmart Child
 Description:  Woodmart Child Theme
 Author:       XTemos
 Author URI:   http://xtemos.com
 Template:     woodmart
 Version:      1.0.0
 Text Domain:  woodmart
*/

<?php
/**
 * Enqueue script and styles for child theme
 */
function woodmart_child_enqueue_styles() {
	wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'woodmart-style' ), woodmart_get_theme_info( 'Version' ) );
}
add_action( 'wp_enqueue_scripts', 'woodmart_child_enqueue_styles', 10010 );

//Include custom JS file//
function zm_child_scripts() {
    wp_enqueue_script(
        'my-great-script',
        get_stylesheet_directory_uri() . '/custom_js/antler.js',
        array( 'jquery' ),
        '1.0.0',
        true
    );
}
add_action( 'wp_enqueue_scripts', 'zm_child_scripts' );


//hide catagory names in single product//
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );



/**
 * Disable payment gateway based on category.
 * https://aceplugins.com/disable-payment-gateways-by-product-category/
 */
function ace_disable_payment_gateway_category( $gateways ) {
    // Categories that'll disable the payment gateway 
	$category_slugs = array( 'jewelery' );
	$category_ids = get_terms( array( 'taxonomy' => 'product_cat', 'slug' => $category_slugs, 'fields' => 'ids' ) );


    if(method_exists(WC()->cart,'get_cart')){ //this must be added to bypass the call to null get_cart() function error
    
    	// Check each cart item for given category
    	foreach ( WC()->cart->get_cart() as $item ) {
            $product = $item['data'];
    	$product = wc_get_product( $product->get_parent_id() ?: $product );
    
    		if ( $product && array_intersect( $category_ids, $product->get_category_ids() ) ) {
    			unset( $gateways['cod'] ); // Disable payment gateway 'cod' when product has one of the categories
                break;
    		}
    	}
    
    	return $gateways;
    }
}
add_filter( 'woocommerce_available_payment_gateways', 'ace_disable_payment_gateway_category' );


//change shipping to delivery in order page//
//Delivery
add_filter( 'woocommerce_shipping_package_name', 'bbloomer_new_shipping_title' );
 
function bbloomer_new_shipping_title() {
   return "Delivery";
}

//delivery (simple case)
add_filter('gettext', 'translate_reply');
add_filter('ngettext', 'translate_reply');

function translate_reply($translated) {
   $translated = str_ireplace('shipping', 'Delivery', $translated);
   return $translated;
}
///change shipping to delivery in order page//


/**
 * @snippet       Translate "Ship to a Different Address?" @ Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */
 
add_filter( 'gettext', 'bbloomer_translate_shippingtodiffaddr', 9999, 3 );
    
function bbloomer_translate_shippingtodiffaddr( $translated, $untranslated, $domain ) {
   if ( ! is_admin() && 'woocommerce' === $domain ) {
      switch ( $untranslated ) {
         case 'Ship to a different address?':
            $translated = 'Delivery Details';
            break;
      }
   }   
   return $translated;
}

/* 
 * Hide delivery charge option when entitled for free delivery in cart page 
 * 
 */
 add_filter( 'woocommerce_package_rates', function( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}, 100 );


