Created
December 17, 2024 09:15
-
-
Save zakirsajib/b2eb6b3f000b723fd67956db53e679f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Change the "Read more" button text to "Learn more" in the Related Products section. | |
* | |
* This function modifies the add-to-cart button text for non-purchasable products | |
* when viewing a single product page. | |
* | |
* @param string $text The current button text. | |
* @param WC_Product $product The WooCommerce product object. | |
* @return string Modified button text. | |
*/ | |
function change_read_more_text_related_products( $text, $product ) { | |
// Check if the current product is in the Related Products section | |
if ( ! $product->is_purchasable() && is_product() ) { | |
$text = __( 'Learn more', 'woocommerce' ); | |
} | |
return $text; | |
} | |
add_filter( 'woocommerce_product_add_to_cart_text', 'change_read_more_text_related_products', 10, 2 ); | |
/** | |
* Change "Select Options" button text to "View Map" for variable products. | |
* | |
* This function replaces the button text for variable products on WooCommerce shop pages. | |
* | |
* @param string $text The current button text. | |
* @param WC_Product $product The WooCommerce product object. | |
* @return string Modified button text. | |
*/ | |
function change_select_options_to_view_map( $text, $product ) { | |
// Check if the product is a variable product | |
if ( $product->is_type( 'variable' ) ) { | |
$text = __( 'View Map', 'woocommerce' ); | |
} | |
return $text; | |
} | |
add_filter( 'woocommerce_product_add_to_cart_text', 'change_select_options_to_view_map', 10, 2 ); | |
/** | |
* Change the placeholder text in WooCommerce variation dropdowns. | |
* | |
* This function updates the default placeholder text in the dropdowns | |
* used for product variations. | |
* | |
* @param array $args The arguments for the dropdown variation attribute options. | |
* @return array Modified arguments with updated placeholder text. | |
*/ | |
function change_dropdown_placeholder_text( $args ) { | |
// Replace the default placeholder text | |
$args['placeholder'] = __( '-- Select --', 'woocommerce' ); | |
$args['show_option_none'] = __( '-- Select --', 'woocommerce' ); | |
return $args; | |
} | |
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'change_dropdown_placeholder_text' ); | |
/** | |
* Replace the labels for WooCommerce variation dropdowns dynamically. | |
* | |
* This function uses JavaScript to replace the labels for variation dropdowns: | |
* - First dropdown: "Area" | |
* - Second dropdown: "Map type" | |
* | |
* This script is added to the footer. | |
*/ | |
function custom_replace_dropdown_labels() { | |
?> | |
<script type="text/javascript"> | |
document.addEventListener("DOMContentLoaded", function() { | |
// Target the labels for the dropdowns | |
let labels = document.querySelectorAll('.variations th label'); | |
// Replace labels dynamically | |
if (labels.length > 0) { | |
labels[0].innerText = "Area"; // First dropdown label | |
labels[1].innerText = "Map type"; // Second dropdown label | |
} | |
}); | |
</script> | |
<?php | |
} | |
add_action('wp_footer', 'custom_replace_dropdown_labels'); | |
/** | |
* Ensure "-- Select --" is set as the default option for WooCommerce variation dropdowns. | |
* | |
* This function uses JavaScript to set the first dropdown option as default | |
* when the product page loads. | |
* | |
* This script is added to the footer. | |
*/ | |
function custom_set_default_dropdown_option() { | |
?> | |
<script type="text/javascript"> | |
document.addEventListener("DOMContentLoaded", function() { | |
// Find all dropdowns in the variations table | |
let variationDropdowns = document.querySelectorAll('.variations select'); | |
variationDropdowns.forEach(function(dropdown) { | |
// Check if the placeholder "-- Select --" exists and set it as default | |
if (dropdown.options.length > 0) { | |
dropdown.selectedIndex = 0; // Ensure the first option is selected | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action('wp_footer', 'custom_set_default_dropdown_option'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment