WooCommerce line_total and line_subtotal - wordpress

Is there any way to change the line_total and line_subtotal in WooCommerce once an order is in add to cart.
Thanks in advance.

There two hook for check out page where you can modify line total and line sub total.
woocommerce_calculate_totals
function action_woocommerce_calculate_totals( $fee ) {
// add your logic
};
// add the action
add_action('woocommerce_calculate_totals','action_woocommerce_calculate_totals', 10, 1 );
woocommerce_cart_subtotal
function filter_woocommerce_cart_subtotal( $array, $int, $int ) {
// implement your logic
return $array;
};
// add the filter
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );

Related

In WooCommerce, Is it possible to display specific country only if cart total is over 100?

I have tried to do it using a filter hook called "woocommerce_countries". I tried getting the cart total in this hook but it's not working. Is anyone have any idea on this OR suggest any hook?
Please try with this code. WooCommerce 3.6.2, they have removed the ability to check the cart data. But cart session is accessible right now so we use this function WC()->session->cart
add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
function products_disable_country( $countries ) {
$cartdata = WC()->session->cart;
if (is_array($cartdata) && !empty($cartdata)) {
foreach ($cartdata as $itemdata) {
$sum += $itemdata['line_total'];
}
if ($sum >= 100) {
unset($countries["CA"]);
}
}
return $countries;
}

Woocommerce Checkout: Add placeholder in country dropdown [duplicate]

This question already has answers here:
WooCommerce: Set country by default in checkout page
(5 answers)
Closed 2 years ago.
in the checkout of my Woocommerce-Shop is a dropdown to choose your country.
On default the USA is automatically selected already.
How to just have a placeholder with “select your country” instead?
I couldn’t find any solution to this topic somebody have any idea?
I changed the other Placeholders which aren't Dropdowns but are inside the same form:
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'First Name*';
$fields['billing']['billing_last_name']['placeholder'] = 'Last Name*';
$fields['billing']['billing_city']['placeholder'] = 'Town / City*';
$fields['billing']['billing_postcode']['placeholder'] = 'ZIP*';
$fields['billing']['billing_email']['placeholder'] = 'Email Address*';
return $fields;
}
My poor solution for now is: I created a new "Country in the Dropdown List" which I just named: select your country* and then just selected it on default instead of the USA. But the problem is here that the system thinks a real country is chosen already so it's not a mandatory field anymore and also the fact that it just doesn't look usual for the user when they choose their country:
function woo_add_my_country( $country ) {
$country["PLACE"] = 'select your country*';
return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'PLACE';
}
I would appreciate any help or tips!
This is working for my side try this
// Change the default country and state on checkout page.
// This works for a new session.
add_filter( 'default_checkout_country', 'xa_set_default_checkout_country' );
add_filter( 'default_checkout_state', 'xa_set_default_checkout_state' );
function xa_set_default_checkout_country() {
// Returns empty country by default.
return null;
// Returns India as default country.
// return 'IN';
}
function xa_set_default_checkout_state() {
// Returns empty state by default.
return null;
// Returns Madhya Pradesh as default state.
// return 'MP';
}
function woo_add_my_country( $country ) {
$country["PLACE"] = 'select your country*';
return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'PLACE';
}
Just go to WooCommerce > Settings.
On General tab you will find Default customer location. Set it to No location by default
This was one of the first results that popped up for me when searching how to set a placeholder for a select field on the Woocommerce checkout page, so for any future visitors:
I have a company select field that I populate dynamically with values from the database. In order to set a placeholder value, I just add an option with a blank value.
add_filter( 'woocommerce_checkout_fields' , 'populate_company_field' );
function populate_company_field($fields) {
$results = get_results_from_database();
$options[''] = __('Choose your company', 'gs'); //add blank value = placeholder
foreach ($results as $result) {
if (!empty($result->company)) {
$options[$result->ID] = $result->company;
}
}
$fields['billing']['company']['options'] = $options;
return $fields;
}

template_include stop working on woocommerce update

The following code is to override the Product Details page template, It was working since the last update on WooCommerce. Can anyone help me out on this, thanks in advance.
add_filter('template_include', 'wpautomate_plugin_templates');
function wpautomate_plugin_templates( $template )
{
$plugin_path='';
$reflector = new ReflectionClass('Ze_Single_Product_Layout');
$file_name=plugin_dir_path($reflector->getFileName());
$plugin_path=$file_name;
$post_types = array('product');
$template_id=get_post_meta( get_the_ID(), '_product_layout', true );
if (is_singular('product') && !empty($template_id))
{
//render custom template for single product
$template = $plugin_path . 'template/woo-single-page.php';
}
return $template;
}//end of function
You need to call this filter
add_filter('template_include', 'wpautomate_plugin_templates');
with init action hook
add_action('init','load_custom_template_woo');
function load_custom_template_woo(){
add_filter('template_include', 'wpautomate_plugin_templates');
}
Thanks
For me, I had to set the priory for the hook callback greater than 10, like this
// priority = 11
add_action('template_include', 'wpautomate_plugin_templates', 11);

WooCommerce not letting me add custom column to shop_order post type

I just started writing a plugin and ran into a problem right away. I want to add a column to the order overview page in the WooCommerce admin. The straight forward filter below doesn't do anything. Replacing shop_order with post or product, however, does show the extra column on the respective overview page.
add_filter('manage_edit-shop_order_columns', 'add_sales_column');
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Trying this on:
WC Version: 2.1.5
WP Version: 3.8.1
How to solve this?
I encountered a similar issue when adding a custom column to the WooCommerce Orders overview page from my theme's functions.php file. I was able to get a custom column to show up by increasing the filter priority above the default value of 10. Try replacing your code with the following:
add_filter('manage_edit-shop_order_columns', 'add_sales_column', 11);
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Tested on WP 3.9.1 and WC 2.1.12.
Check out the WordPress Codex entry on add_filter for more details on the behavior of filters using the $priority parameter.
The problem is that we have to wait for WooCommerce to finish its setup. This goes two ways, first running all our hooks inside a plugins_loaded main hook, and then setting the priority of our hooks for something greater than WC's.
add_action( 'plugins_loaded', 'setup_so_22237380' );
function setup_so_22237380()
{
// Just to make clear how the filters work
$posttype = "shop_order";
// Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
// Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column", 'column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns", 'column_sort_so_22237380' );
}
function column_set_so_22237380( $columns )
{
$columns['order_sales'] = "Sales";
return $columns;
}
function column_display_so_22237380( $column_name, $post_id )
{
if ( 'order_sales' != $column_name )
return;
$sales_information = 'Your custom get_order_sales_information($post_id)';
if ( $sales_information )
echo "<strong style='color:#f00;'> $sales_information </strong>";
}
function column_sort_so_22237380( $columns )
{
$columns['order_sales'] = 'order_sales';
return $columns;
}
Setting the column as sortable is optional (manage_edit-{$posttype}_sortable_columns) and needs an extra action hook to make it work (pre_get_posts). This may be a complex function to build and requires its own research.
It will working fine if you include all the order hook on
add_action( 'admin_init', 'setup_all_order_column_hook' );
function setup_all_order_column_hook(){
//Just to make clear how the filters work
$posttype = "shop_order";
//Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
//Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column",column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns",'column_sort_so_22237380' );
}

Hide Shipping Options Woocommerce

So I'm trying to hide certain ship methods in Woocommerce based on a product tag. The main problem I face is my own lack PHP knowledge so I frankensteined the following code together with the help of some very friendly folks:
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
if (isset($array_item['product_tag']) && $array_item['product_tag'] == "CHOSEN_TAG") { // Replace "CHOSEN_TAG" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function abve to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flar_rate" with the shipping option that yu want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
I understand that this code is by no means universal and thus the variables will be different from case to case but perhaps someone can tell me if something looks off in the code. Much appreciated
No doubt you have sorted this by now but your code was a good start for me ... and since I sorted it I have published it below. Your problem was that woocommerce doesn't have the product_tag in the cart array so you have to go and get it.
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
$term_list = wp_get_post_terms( $array_item['product_id'], 'product_tag', array( "fields" => "names" ) );
if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function above to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
You can use shipping class of Woocommerce to achieve this.
Here is the step by step instructions.
Create a shipping class (woocommerce->settings->shipping->shipping classes).
Associate this shipping class with products (that you wand to hide shipping methods for)
Use this snippet. (copy and pate to function.php).
For more information about the snippet is available here.

Resources