How to call function in other ajax of function - wordpress

I have a change_flat_rates_cost() function, but it works only when loading the page. but it is necessary for me that function worked at each change of the field "city", (in checkout fields).
I have tried to use ajax function and to call there add_filter() function, but it doesn't help.
I do it to change delivery cost without reset of the page depending on the chosen city
change_flat_rates_cost function (changes the shipping price)
function change_flat_rates_cost($rates, $package) {
$userid = get_current_user_id();
$meta_city = get_user_meta( $userid, 'billing_city', true );
if ( isset( $rates['shipping_method_1'] ) ) {
if ($meta_city == 'City1'){
$rates['shipping_method_1']->cost = 100;
}
if ($meta_city == 'City2'){
$rates['shipping_method_1']->cost = 200;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'change_flat_rates_cost', 10, 2 );
ajax function:
add_action( 'wp_ajax_change_shipping', 'change_shipping' );
add_action( 'wp_ajax_nopriv_change_shipping', 'change_shipping' );
function change_shipping() {
global $woocommerce;
add_filter( 'woocommerce_package_rates', 'change_flat_rates_cost', 10, 2 );
die();
}

You can add filter as normal "add_filter". No need to put it there in ajax, because anyways there are conditions mentioned in your function.
So, do add_filter normally and instead of using add_filter in ajax function, you should use "apply_filters". This will call your custom shipping function.
I hope I've made it clear.

Related

Issue when using "woocommerce_product_get_stock_quantity" filter hook

I want to replace the stock quantity on the single product page with "5+", if the stock quantity is more than 5, else display the original quantity.
I tried to modify the hook as below
add_filter( 'woocommerce_product_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_get_stock_quantity', 10, 2 );
function custom_get_stock_quantity( $value, $_product ) {
global $product;
if($_product->get_stock_quantity() > 5){
return "5+";
}
else{
return $_product->get_stock_quantity();
}
}
This gives me 502 server error. What could be the issue here?
The error is due to several reasons
Hook is deprecated
Your callback function only returns a number: 5, not a string: "5+"
By using $_product->get_stock_quantity() you create an infinite loop, since this function calls the hook
So to meet your demand, you will have to approach this differently. For example by using the woocommerce_get_availability_text hook
So you get:
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
if ( $product->get_stock_quantity() > 5 ) {
$availability = '+5';
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

Woocommerce hook that fires after a product is updated via REST API

I am using this hook to run some code after the product is updated:
add_action( 'updated_post_meta', 'attach_variation_images_on_product_save', 10, 4 );
function attach_variation_images_on_product_save( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_edit_lock' ) {
if ( get_post_type( $post_id ) == 'product' ) {
//do something
}
}
}
This is working as expected, the function is executed after the product is updated. I want to run the same function when a product is getting updated via the REST API. I hooked my function to woocommerce_rest_insert_product_object like this but it did not work:
add_action( 'woocommerce_rest_insert_product_object', 'attach_variation_images_on_product_update_via_rest', 10, 3 );
function attach_variation_images_on_product_update_via_rest( $post, $request, $true ) {
if ( get_post_type( $post ) == 'product' ) {
$product = wc_get_product( $post );
//do something
}
}
Am I not using the right hook? Is there another hook I can use?
EDIT 1: It seems that my code was not running because get_post_type($post) is type of post and not product. I am trying to attach an image to variations using add_post_meta($variation_id, '_thumbnail_id', $image_id); inside a loop. It seems the function attach_variation_images_on_product_update_via_rest( $post, $request, $true ) is executed till the end but it does not attach the image to the variations.
I ended up using this hook add_action( 'woocommerce_update_product', 'my_callback_function', 10, 1 ); that executes the callback function when you updating products from Woocommerce back-end as well from the REST API.

woocommerce hook delete product from the cart

what are you doing for it?
i wrote this string in the end of file (wp-content/plugins/woocommerce/woocommerce.php):
add_filter('woocommerce_before_cart_item_quantity_zero', 'wordpress_before_cart_item_quantity_zero', 10, 1);
function wordpress_before_cart_item_quantity_zero($item) {
global $wpdb;
global $woocommerce;
$cart = $woocommerce->cart;
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$id_product = $cart_item['product_id'];
$wpdb->prepare("DELETE FROM wp_block_product WHERE product_id = %d", $id_product);
$preparing_query = $wpdb->query($preparing_query);
}
var_dump($wpdb);
exit;
}
but when i delete item from the cart it's not working!
i was try do so:
add_action('woocommerce_before_cart_item_quantity_zero', 'wordpress_before_cart_item_quantity_zero');
but it's not working too
According to the source, you're looking for 'woocommerce_cart_item_removed', which runs when an item is removed from the cart:
function so31115243_after_remove_product($cart_item_key) {
// Your custom function
}
add_action( 'woocommerce_cart_item_removed', 'so31115243_after_remove_product' );
There is also a hook that runs before removing the item, which is woocommerce_remove_cart_item.
I believe that is what Danya is looking for in the comment above in case anyone runs into a similar issue.
#rnevius solution works but need to change the priority of the action. For me it was 21 but i think it can change regarding plugins using this.
add_action( 'woocommerce_cart_item_removed', 'so31115243_after_remove_product', 21 );
function so_27030769_maybe_empty_cart() {
global $woocommerce;
$woocommerce->cart->empty_cart();}
add_filter( 'woocommerce_add_to_cart_validation_custom', 'so_27030769_maybe_empty_cart', 10, 3 );
// Usage
apply_filters('woocommerce_add_to_cart_validation_custom','','');

WooCommerce - disable postcode validation

Does anybody know how to disable the Postcode validation on the checkout page in WooCommerce?
My country is set up as Switzerland, but I want also people from Austria and Germany allow to order.
So when I enter a German Postcode with 5 digits (in Switzerland there are only 4), the shop says it's an invalid postcode. (but in the settings I allowed every country).
Any idea how to fix that?
Adding this code to the functions.php file should work:
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}
EDIT:
// Hook into the checkout fields (shipping & billing)
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Hook into the default fields
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_checkout_fields( $fields )
{
unset( $fields['billing']['billing_postcode'] );
unset( $fields['shipping']['shipping_postcode'] );
return $fields;
}
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}
ANOTHER EDIT:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields )
{
$address_fields['postcode']['required'] = false;
return $address_fields;
}
So I didn't actually find a simple code solution for this one but I noticed that if I set
WooCommerce > Preferences > General > Geolocate address
it will work (if settings are set to "Sell to all countries", in my case)
This code only removes validation of address fields in my-account page, what you need:
add_filter( 'woocommerce_default_address_fields',
'custom_override_default_address_fields' );
function custom_override_default_address_fields($address_fields)
{
$address_fields['postcode']['validate'] = false;
return $address_fields;
}
for billing and shipping:
add_filter( 'woocommerce_checkout_fields' , 'remove_postcode_validation', 99 );
function remove_postcode_validation( $fields ) {
unset($fields['billing']['billing_postcode']['validate']);
unset($fields['shipping']['shipping_postcode']['validate']);
return $fields;
}
Also i think with removing "validate-required" class in wc-template-function.php, this feature will be deactivated (no test).
Sorry for bad English and hope this solutions solve your problem.
The previous answers don't seem to address the question! The postcode is still a required field, it's matter of whether other postcodes can be allowed that WooCommerce is saying is wrong.
For a checkout I'm building I've used this filter to allow any postcode to be considered valid regardless of country/postcode given.
add_filter( 'woocommerce_validate_postcode' , 'mycode_override_postcode_check', 99, 3 );
function mycode_override_postcode_check( $valid, $postcode, $country ) {
return true;
}
You can change that return true; for more complex code logic that reviews the postcode and country, this can help if your version of WooCommerce doesn't cover new postcode/zip code rules and you need them to be accepted.
Source: https://github.com/EloxZ/wordpresscrm/blob/main/wp-content/plugins/woocommerce/includes/class-wc-validation.php#L123

hook into woocommerce_variation_price_html wordpress

I am working on a plugin for woocommerce and came across the hook "woocommerce_variation_price_html" which allows you to hook into the variation prices drop down on single products. So created a quick function to test and have a play:
add_action( 'woocommerce_variation_price_html' , 'wholesale_variation_price' );
function wholesale_variation_price($term){
return '';
}
The above works nicely and removes all data. However i am trying to add custom meta_data to replace the default values.
So i then did the following:
add_action( 'woocommerce_variation_price_html' , 'wholesale_variation_price' );
function wholesale_variation_price($term){
$var_price = get_post_meta( get_the_id(), '_my_price', true );
return $var_price;
}
This for some reason does not work? Has anyone had any experience working with this hook in woocommerce? There is not much documentation on that hook anywhere.
Any help would be greatly appreciated!
As already indicated that you can read here about filters for html.
Maybe you have Sale price set for variation? Then in link you can see that there are total of 4 filters. For: price, sale price, free and no price.
This code works
add_filter( 'woocommerce_variation_sale_price_html', 'my_html', 10, 2);
add_filter( 'woocommerce_variation_price_html', 'my_html', 10, 2);
function my_html( $price, $variation ) {
return woocommerce_price(5);
}
Also this one works too. This allows you to modify whole variation. Read in code here
add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
$data['price_html'] = woocommerce_price(6);
return $data;
}
Screenshots:
First example
Second example
Works like magic!
P.S. you can see prices like this in screenshots because I have design that show them like that.
EDIT 2016-09-10
Version 2.6+
As woocommerce_price is deprecated in 2.6 then use wc_price for newer versions.
// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
if ($value['price_html'] == '') {
$value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
}
return $value;}, 10, 3);
By looking at WooCommerce source code, I see that woocommerce_variation_price_html is a filter, not an action. I don't know how you managed to get your sample code to work like that... weird.
Try this:
function wholesale_variation_price( $price, $product ) {
$var_price = get_post_meta( $product->variation_id, '_my_price', true );
return $var_price;
}
add_filter( 'woocommerce_variation_price_html', 'wholesale_variation_price', 10, 2 )
I used this to modify the variation prices to show the price for each variation product excluding tax before and including tax after :)
add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
$data['price_html'] = "<span class='ex-vat-price'>ex. " . woocommerce_price($variation->get_price_excluding_tax()) . "</span><br>";
$data['price_html'] .= "<span class='inc-vat-price'>inc. " . woocommerce_price($variation->get_price_including_tax()) . "</span>";
return $data;
}

Resources