Woocommerce Change "Select option" text in variable product page - woocommerce

On product page 'dropdown) i'd like to modify the SELECT OPTIONS text and i assume this code could to this but nothing change in my page even if i empty cache.
Thanks for your help
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'variable':
return __( 'Options', 'woocommerce' );
break;
}
}

Use the following code --
add_filter('woocommerce_dropdown_variation_attribute_options_args', 'custom_woocommerce_product_add_to_cart_text', 10, 2);
function custom_woocommerce_product_add_to_cart_text($args){
$args['show_option_none'] = __( 'Options', 'woocommerce' );
return $args;
}

I'm little late but below code can also be used .
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'variable':
return __( 'Options', 'woocommerce' );
break;
}
case 'simple':
return __( 'Purchase', 'woocommerce' );
break;
}
}
Reference : https://www.raadeen.com/change-select-options-text-woocommerce/

Related

Create a custom hidden field in backend product pages and use value in WooCommerce products sort by

I am making an e-commerce website using WooCommerce. With the help of this forum, I was able to code some useful snippets. However, I am stuck at a certain code now.
I have pieced together codes from various sections, but I am unable to get it to work.
What I want to do - create a custom hidden field programmatically for discount percent & then use the field to allow sorting by discount.
But it looks like the meta does not save in the backend.
When I sort by discount, all I get is this 'No products were found matching your selection.' even though there are some products on sale.
This is my code:
//add_action ('woocommerce_process_product_meta', 'mycode_product_save_discount');
add_action ('woocommerce_admin_process_product_object', 'mycode_product_save_discount');
function mycode_product_save_discount ($product)
{
$product = wc_get_product ($product);
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
if (!empty($sale_price))
{
$discount_percent = round (100 - ($sale_price / $regular_price * 100), 4);
$discount_percent = $_POST['_discount_percent'][$product];
//update_post_meta ($post_id, '_discount_percent', $discount_percent);
$product->update_meta_data ('discount_percent', $discount_percent);
$product->save();
}
}
add_filter ('woocommerce_get_catalog_ordering_args', 'mycode_add_discount_to_ordering_args');
function mycode_add_discount_to_ordering_args ($args)
{
$orderby_value = isset ($_GET['orderby']) ? wc_clean ($_GET['orderby']) : apply_filters ('woocommerce_default_catalog_orderby', get_option ('woocommerce_default_catalog_orderby'));
if (!is_post_type_archive ('product') && !is_shop() && isset($args['orderby']))
{
$orderby_value = $sort_args['orderby'];
}
if ('discount' == $orderby_value)
{
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = '_discount_percent';
}
return $args;
}
add_filter ('woocommerce_default_catalog_orderby_options', 'mycode_add_discount_to_orderby');
add_filter ('woocommerce_catalog_orderby', 'mycode_add_discount_to_orderby');
function mycode_add_discount_to_orderby ($sortby)
{
$sortby['discount'] = 'Sort by discount';
return $sortby;
}
What do I need to change to make it work?
You are close, but your code contains some minor mistakes.
This should suffice:
/*** For debugging purposes, remove this action hook if everything works!! ***/
function action_woocommerce_product_options_general_product_data() {
// Text field
woocommerce_wp_text_input( array(
'id' => '_discount_percent',
'label' => __( 'Discount percent', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'For debugging purposes, remove this action hook if everything works', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => array( 'readonly' => true ),
));
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
/*** From here on the code below is needed so that everything would work smoothly ***/
// Save value
function action_woocommerce_admin_process_product_object( $product ) {
// Getters
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
// NOT empty
if ( ! empty( $sale_price ) ) {
// Calculate
$discount_percent = round( 100 - ( $sale_price / $regular_price * 100 ), 4 );
// Update
$product->update_meta_data( '_discount_percent', $discount_percent );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// Ordering products based on the selected values
function filter_woocommerce_get_catalog_ordering_args( $args, $orderby, $order ) {
switch( $orderby ) {
case 'discount':
$args['orderby'] = 'meta_value';
$args['order'] = 'ASC';
$args['meta_key'] = '_discount_percent';
break;
}
return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'filter_woocommerce_get_catalog_ordering_args', 10, 3 );
// Orderby setting
function filter_orderby( $orderby ) {
$orderby['discount'] = __( 'Sort by discount', 'woocommerce' );
return $orderby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'filter_orderby', 10, 1 );
add_filter( 'woocommerce_catalog_orderby', 'filter_orderby', 10, 1 );
This shows the value after it has been saved. This is not actually necessary, but can be useful for debug purposes.

Can't Change WooCommerce Coupon Label In Cart Totals

This has proven to be a challenge I can't figure out. I handled the renaming of "Coupon" to "Promo" pretty much everywhere else. However, the renaming of the WC LABEL that calls the name of the coupon and the LABEL is a lot harder.
Here's a screenshot of what I'm talking about exactly:
I've pretty much renamed all the uses of "coupon" to "promo" site-wide. This one is making my hair fall out. Has anyone ever successfully changed this? What am I missing?
Rename "Coupon" to "Promo" code below
// rename the coupon field on the cart page
function woocommerce_rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
// bail if not modifying frontend woocommerce text
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon code:' === $text ) {
$translated_text = 'Promo Code:';
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_cart', 10, 3 );
// rename the "Have a Coupon?" message on the checkout page
function woocommerce_rename_coupon_message_on_checkout() {
return 'Have a Customer Code?' . ' <a href="#" class="showcoupon">' . __(
'Click here to enter your code', 'woocommerce' ) . '</a>';
}
add_filter( 'woocommerce_checkout_coupon_message',
'woocommerce_rename_coupon_message_on_checkout' );
// rename the coupon field on the checkout page
function woocommerce_rename_coupon_field_on_checkout( $translated_text, $text, $text_domain ) {
// bail if not modifying frontend woocommerce text
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon code' === $text ) {
$translated_text = 'Promo Code';
} elseif ( 'Apply coupon' === $text ) {
$translated_text = 'Apply Code';
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_checkout', 10, 3 );
WC Coupon Notices/Message code below
function woocommerce_coupon_message( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Coupon' :
$translated_text = __( 'Promo', 'woocommerce' );
break;
case 'promo' :
$translated_text = __( 'promo', 'woocommerce' );
break;
case 'Please enter a coupon code.' :
$translated_text = __( 'Please enter a Promo code.', 'woocommerce' );
break;
case 'Coupon code already applied!' :
$translated_text = __( 'Promo code already applied!', 'woocommerce' );
break;
case 'Coupon has been removed.' :
$translated_text = __( 'Promo has been removed', 'woocommerce' );
break;
case 'Coupon code applied successfully.' :
$translated_text = __( 'Promo code applied successfully.', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_coupon_message', 20, 3 );
// Hide 'Coupon: CODE' in cart totals and instead return generic 'Promo Code Applied'
add_filter( 'woocommerce_cart_totals_coupon_label', 'woocommerce_change_coupon_label' );
function woocommerce_change_coupon_label() {
echo 'Promo Code Applied';
}
add_filter( 'woocommerce_cart_totals_coupon_label', 'woocommerce_change_coupon_label' );
function woocommerce_change_coupon_label($sprintf, $coupon) {
return 'Promo ' . $coupon->code;
}
I found a solutions for all needs.
Works with WooCommerce v7.
function custom_cart_totals_coupon_label($sprintf, $coupon)
{
$coupon_code = $coupon->get_code();
$sprintf = '<span class="discount_label">'.__('Coupon', 'veganwebagency').':</span> <span class="discount_code">'.$coupon_code.'</span>';
return $sprintf;
}
add_filter('woocommerce_cart_totals_coupon_label', 'custom_cart_totals_coupon_label', 10, 2);

Adding New Stock Status to Products in Woocommerce [duplicate]

I am using the following code to add new stock statuses in WooCommerce 4+
The new statuses are:
Preorder
Contact us
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'Available', 'woocommerce' ), //changed the name
'outofstock' => __( 'Sold out', 'woocommerce' ), //changed the name
'onbackorder' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), //changed the name
'contact' => __( 'Contact us for Availability', 'woocommerce' ), //added new one
'preorder' => __( 'On Preorder: Pending Distributor release', 'woocommerce' ), //added new one
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
function woo_add_custom_general_fields_save_two( $post_id ){
// Select
$woocommerce_select = $_POST['_stock_status'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_stock_status', esc_attr( $woocommerce_select ) );
else
update_post_meta( $post_id, '_stock_status', '' );
}
function woocommerce_get_custom_availability( $data, $product ) {
switch( $product->stock_status ) {
case 'instock':
$data = array( 'availability' => __( 'Available', 'woocommerce' ), 'class' => 'in-stock' ); //changed name
break;
case 'outofstock':
$data = array( 'availability' => __( 'Sold Out', 'woocommerce' ), 'class' => 'out-of-stock' ); //changed name
break;
case 'onbackorder':
$data = array( 'availability' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'onbackorder' ); //changed name
break;
case 'contact':
$data = array( 'availability' => __( 'Contact us for Availability', 'woocommerce' ), 'class' => 'contact' ); //added new one
break;
case 'preorder':
$data = array( 'availability' => __( 'On Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'preorder' ); //added new one
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 4);
Works:
Backend: The new status is added in the dropdown menu, I can select the status I want
Does not work:
Front end: on single product page is not showing the correct status
Backend: Display the new status on the admin products list table
Someone who can assist me with this?
Last update: 04/22 - Tested in WordPress 5.9.2 & WooCommerce 6.3.1
Code goes in functions.php file of the active child theme (or active theme).
Use woocommerce_product_stock_status_options
instead of woocommerce_product_options_stock_status.
This way you can immediately add a status instead of replace the existing dropdown
Also use woocommerce_get_availability_text & woocommerce_get_availability_class
opposite woocommerce_get_availability.
This way you don't have to add the existing statuses again
// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['pre_order'] = __( 'Pre order', 'woocommerce' );
$status['contact_us'] = __( 'Contact us', 'woocommerce' );
return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'contact_us':
$availability = __( 'Contact us', 'woocommerce' );
break;
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$class = 'pre-order';
break;
case 'contact_us':
$class = 'contact-us';
break;
}
return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
Use woocommerce_admin_stock_html to display the new stock status on the admin products list table
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Simple
if ( $product->is_type( 'simple' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );
// Get stock status
$product_stock_status = $variation->get_stock_status();
/*
Currently the status of the last variant in the loop will be displayed.
So from here you need to add your own logic, depending on what you expect from your custom stock status.
By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
- Product should be in stock if a child is in stock.
- Product should be out of stock if all children are out of stock.
- Product should be on backorder if all children are on backorder.
- Product should be on backorder if at least one child is on backorder and the rest are out of stock.
*/
}
}
// Stock status
switch( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'contact_us':
$stock_html = '<mark class="contact-us" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>';
break;
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
Optional: if desired, the custom stock status can be used in hooks where you already have access to the $product object or you can use global $product.
1) No access to the $product object, use global $product as is the case for example with the woocommerce_shop_loop_item_title or the woocommerce_single_product_summary hook
function woocommerce_my_callback() {
// An example based on global $product
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Use this line during testing, delete afterwards!
echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
// Compare
if ( $product_stock_status == 'My custom stock status' ) {
// Etc..
}
}
}
add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_my_callback', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_my_callback', 10 );
2) Access to the $product object, because it's passed by default to the callback function. As is the case for example with the woocommerce_get_price_html hook
function filter_woocommerce_get_price_html( $price, $product ) {
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Use this line during testing, delete afterwards!
echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
// Compare
if ( $product_stock_status == 'My custom stock status' ) {
// Etc..
// $price .= ' my text';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
In addition to filters provided by 7uc1f3r woocommerce_product_export_product_column_stock_status filter is required to display custom stock status in exported products CSV file:
function add_custom_stock_csv_data( $_, $product ) {
$status = $product->get_stock_status( 'edit' );
switch( $status ) {
case 'pre_order':
case 'contact_us':
return $status;
case 'onbackorder':
return 'backorder';
case 'instock':
return 1;
default:
return 0;
}
}
add_filter( 'woocommerce_product_export_product_column_stock_status', 'add_custom_stock_csv_data', 10, 2 );

Hide / Change WooCommerce admin notice

Hi WooCommerce ninjas!
I'm developing WooCommerce plugin and every time when I submit form (Save Changes button) on top shows update notice with 'Your settings have been saved.'. How I can hide or change this notice, I'm use woocommerce_show_admin_notice filter but don't work in my plugin class. Below is part of code from my plugin. Any ideas for hook who will be usefull?
Great thanks!
<?php
class name_of_plugin extends WC_Payment_Gateway {
public $error = '';
// Setup our Gateway's id, description and other values
function __construct() {
$this->id = "name_of_plugin";
$this->method_title = __( "Name", 'domain' );
$this->method_description = __( "Gateway Plug-in for WooCommerce", 'domain' );
$this->title = __( "TFS VPOS", 'domain' );
$this->icon = null;
$this->has_fields = false;
$this->init_settings();
$this->init_form_fields();
$this->title = $this->get_option( 'title' );
$this->testurl = 'https://example.com/payment/api';
$this->liveurl = 'https://example.com/payment/api';
// Save settings
if ( is_admin()) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}
// Disable Admin Notice
add_filter( 'woocommerce_show_admin_notice', array( $this, 'shapeSpace_custom_admin_notice' ), 10, 2 );
// add the filter
add_filter( 'woocommerce_add_error', array( $this, 'filter_woocommerce_add_notice_type' ), 10, 1 );
} // End __construct()
// display custom admin notice
public function filter_woocommerce_add_notice_type($true, $notice ) {
// magic happen here...
return $true;
}
I can't see any proper hook that can be used to remove this.
But these 2 seems to work.
My first thought is to reload the page so the settings text will be gone at the time. Something like this.
add_action( 'woocommerce_sections_checkout', 'woocommerce_sections_checkout' );
function woocommerce_sections_checkout() {
if ( isset( $_GET['section'] ) && isset( $_REQUEST['_wpnonce'] ) && ( $_GET['section'] === 'paypal' )
&& wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
WC_Admin_Settings::add_message( __( 'Your settings have been saved!', 'woocommerce' ) );
wp_safe_redirect( wp_get_raw_referer() );
exit();
}
}
Then my second option is if you wanted to change the text, we can use the filter gettext.
add_filter( 'gettext', 'woocommerce_save_settings_text', 20, 3 );
function woocommerce_save_settings_text( $translated_text, $text, $domain ) {
if ( ( $domain == 'woocommerce' ) && isset( $_GET['section'] ) && ( $_GET['section'] === 'paypal' ) ) {
switch ( $translated_text ) {
case 'Your settings have been saved.' :
$translated_text = __( 'Your awesome settings have been saved.', 'woocommerce' );
break;
}
}
return $translated_text;
}
Please do note that this code is just an example and will work for paypal settings. Change everything that is needed.

Change title "Additional Information" in woocommerce

I want to change the title from the checkout page. But I just can change the label and the placeholder.
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Please type your PO number here and we will add it to the invoice.';
$fields['order']['order_comments']['label'] = '';
return $fields;
}
https://docs.woocommerce.com/document/editing-product-data-tabs/#
/**
* Rename product data tabs
*/
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['reviews']['title'] = __( 'Ratings' ); // Rename the reviews tab
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
return $tabs;
}
As it stands there is not hook to change the section title. But here's the hack if you are desperate enough to make the modification.
Locate your template folder
Create a folder named 'checkout'
Locate the file form-shipping.php in the Woocommerce plugin foler under templates/checkout/
Copy file in step 3 to the folder created in step 2
Now you have superpowers over the checkout form
Edit this line:
<h3><?php _e( 'Additional Information', 'woocommerce' ); ?></h3>
This solution worked for me:
function ajg_relabel_additional_information_tab(){
return __( 'Specification', 'text-domain' );
}
add_filter('woocommerce_product_additional_information_heading', 'ajg_relabel_additional_information_tab');
function th_wc_order_review_strings( $translated_text, $text, $domain ) {
if(is_checkout()){
switch ($translated_text) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
case 'Additional information':
$translated_text = __('New Field Name', 'woocommerce');
break;
case 'Your order':
$translated_text = __('My Order', 'woocommerce');
break;
case 'Product':
$translated_text = __('Your Product', 'woocommerce');
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'th_wc_order_review_strings', 20, 3 );
The documentation of Woocommerce is not complety...
https://docs.woocommerce.com/document/editing-product-data-tabs/
You would check condition about the callback before add or replace some value in array, otherwise the tab will display with nothing inside.
/**
* Filter product data tabs
*/
function filter_product_tabs( $tabs ) {
global $product;
if ( isset($tabs['description']['callback']) ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['description']['priority'] = 5; // Description
}
if ( isset($tabs['additional_information']['callback']) ) {
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
$tabs['additional_information']['priority'] = 10; // Additional information
}
if ( isset($tabs['reviews']['callback']) ) {
$tabs['reviews']['title'] = __( 'Review' ) . ' (' . $product->get_review_count() . ') '; // Rename the reviews tab
$tabs['reviews']['priority'] = 15; // Reviews
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_product_tabs', 98 );
Why ? because the developper Woocommerce will check the content of array in the tab template
(version 3.8.0) (WC
/**
* Filter tabs and allow third parties to add their own.
*
* Each tab is an array containing title, callback and priority.
*
* #see woocommerce_default_product_tabs()
*/
$product_tabs = apply_filters( 'woocommerce_product_tabs', array() );
if ( ! empty( $product_tabs ) ) :
....
This worked for me if anyone is still after this change
//Shipping Weight custom tab name
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['additional_information']['title'] = __( 'Additional Information' ); // Rename the Additional Information text
return $tabs;
}

Resources