Disabinge woocommerce variable product price doesn't work - wordpress

I tried several codes to disable the price in product variable in woocommerce.
I've tried this but nothing happened:
/*
Disable Variable Product Price Range completely:
*/
add_filter( 'woocommerce_variable_sale_price_html','my_remove_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'my_remove_variation_price', 10, 2 );
function my_remove_variation_price( $price ) {
$price = '';
return $price;
}
I put it in functions.php but nothing happened.
My theme is saha from theme-junkie.

Best way would be to hook into woocommerce_get_price_html filter.
It passes $price HTML and WC_Product* object. You can check if it is instance of WC_Product_Variable to determine if it is a variable product. Then just return '' empty string if it is. This will handle the price display for parent Variable product.
Example
add_filter('woocommerce_get_price_html', 'mm_handle_variation_prices', 10, 2);
function mm_handle_variation_prices( $price_html, $product_obj ){
// Bail unless we are on WooCommerce page on frontend and this is an ajax request.
if( is_admin() || is_ajax() || ! is_woocommerce() ) {
return $price_html;
}
// If this is an Variable product only then return empty string.
// '$product_obj instanceof WC_Product_Variable' check would work as well.
if( 'variable' === $product_obj->get_tyepe() ) {
return '';
} else {
return $price_html;
}
}

Related

Check the SKU in WooCommerce before saving a new product

I hope you can help me out.
I need to check the SKU before saving a new product
And I also want to avoid errors due to duplicated SKU value. The SKU should just be change so it is unique.
I have tried a lot of combinations and different hooks – and nothing happens, the product just saves whether or not the SKU is set.
// Disable default duplicte sku check
add_filter( 'wc_product_has_unique_sku', '__return_false' );
//Check SKU
add_action( 'woocommerce_before_product_object_save', 'set_sku', 50, 2 );
function set_sku( $product_id, $product ) {
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
$currentSku = $product->get_sku();
if( empty( $currentSku ) ) { //If SKU field is empty then use slug for SKU
$product->set_sku($product->get_slug());
}else if( !wc_product_has_unique_sku($product_id, $currentSku ) ) {
$product->set_sku($currentSku.'-'.$generateRandomString()); //SKU duplicated - add random string to end
}
}
Why don't you just use the 'pre_post_update' hook?
The hook 'woocommerce_before_product_object_save' doesn't work, it was probably removed.
add_action( 'pre_post_update', 'set_sku', 50, 2 );
function set_sku( $product_id, $product_data ) {
// Only run this code for products
if ($product_data['post_type'] !== 'product') {
return;
}
$product = wc_get_product($product_id);
//... your sku related code ...

cant access woocommerce cart in contact form 7 hook?

Using the the wpcf7 hook wpcf7_special_mail_tags() I want to get the cart contents and return the output, but WC()->cart object is null in this hook, I cant access any methods on WC()->cart as its a call to an undefined method. I cant work out how to get access to WC()->cart in this hook.
I can access WC()->cart fine in the WPCF7 hook wpcf7_add_form_tag()
The wpcf7_special_mail_tags() works fine and inserts other data into email, I just want cart data though.
Thanks for any help.
function my_special_mail_tag( $output, $name, $html ) {
if ( 'mytag' == $name ) {
$cart = WC()->cart;
if(!WC()->cart->is_empty()) {
// do something with output
}
}
return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );
Here you go. I'm ok with this.
function my_special_mail_tag( $output, $name, $html ) {
if ( 'mytag' == $name ) {
$cart = WC()->cart;
if(!WC()->cart->is_empty()) {
// do something with output
}
}
return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );

How to Add custom price on single product without affecting to related product?

I have code to changes the custom message on all product with 2 different messages. Please take a look.
add_filter('woocommerce_empty_price_html', 'show_alert_info_if_no_price');
function show_alert_info_if_no_price ($product){
if (is_product()) {
global $product;
$price = $product->get_price();
if ($price == '') {
ob_start();
// return for the product page
return '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
} else {
// otherwise return short text as kontak kami
return 'Contact us';
}
}
}
Now I have a problem on related product. I need the related product price will appear like :
//short text as kontak kami
Now I am stuck how to add another code when using hooked.
$woocommerce_loop['name'] != 'related').
any help will appreciated!
There is an mistake in your code, because this hook is only executed for empty prices. So going to check in the hook again for an empty price and if it isn't, running an else condition is useless.
To display a different text on the single product page for related products you can use global $woocommerce_loop
So you get:
function filter_woocommerce_empty_price_html( $html, $product ) {
global $woocommerce_loop;
// True on a single product page
if ( is_product() ) {
$html = '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
// Related
if ( $woocommerce_loop['name'] == 'related' ) {
$html = __( 'Some other text', 'woocommerce' );
}
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );

Woocommerce 2.5: Hide Shipping Methods when free shipping is available not working anymore

I Had this code
/* !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;
}
in my function.php - but since woocommerce 2.5 it's not working anymore.
The other shipping methods need to be hidden when free shipping is available.
Any Ideas?
You're using a hook that was used for WooCommerce version 2.1 and below.
Instead, use the woocommerce_package_rates filter..
This code works perfect for latest woocommerce :
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
I have unset the flat shipping, you can unset other shipping methods if you enabled. This code works fine for me, i tried it yesterday.

Woocommerce add to cart with custom price

I've seen many examples of adding an item to the WC cart with a customer price, but none dynamically. I am trying to do in a shortcode function that receives a POST variables....
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$cart_object = $woocommerce->cart;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$cart_object->add_to_cart($target_product_id, "1");
foreach ( $cart_object->cart_contents as $key => $value ) {
if ( $value['product_id'] == $target_product_id ) {
$value['data']->price = $custom_price;
}
}
}
This adds the item to the cart of course, but the price is zero and I realize I need to somehow save this array back to the WC cart data. Is this method even possible or can it only be done via a filter or action hook? If so, how can I save the changed array back to the cart contents or make it work to add the one item with its posted price? Any guidance greatly appreciated.
Thanks for that answer doublesharp, I was not able to get it to work as described because the form was posting to the page with my shortcode, which has my form, instead of posting directly to the cart. The $_POST was not being seen and the product ended up zero. I did find another approach, but having a problem using wp_redirect. I changed the above shortcode to this:
if (isset($_POST['wmnf_add_donation'])) {
global $woocommerce;
$custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
$target_product_id = 65986;
$_SESSION['donation_amount'] = $custom_price;
$woocommerce->cart->add_to_cart($target_product_id, "1");
wp_redirect( site_url() . '/gifts/swag-bag/');
}
Then I added the following filters to functions.php:
add_filter('woocommerce_get_price','donation_price', 10, 2);
add_filter('woocommerce_get_regular_price','donation_price', 10, 2);
add_filter('woocommerce_get_sale_price','donation_price', 10, 2);
function donation_price($price, $productd){
if($productd->id == '65986'){
$price = $_SESSION['donation_amount'];
}
return $price;
}
This does not work except when wp_redirect is commented out, hence, not redirecting. The above redirects to the cart, but its empty. If I comment out the wp_redirect line and then manually go to the cart, the product is there with my custom price. Actually, I would like to apply a custom price and redirect directly to the checkout page instead of the cart, if possible?
You can use the woocommerce_before_calculate_totals action hook to modify the contents of the cart, including the product prices.
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
// loop through the cart_contents
foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
// you will need to determine the product id you want to modify, only when the "donation_amount" is passed
if ( $item['id'] == 65986 && isset( $_POST['donation_amount'] ) ){
// custom price from POST
$custom_price = $_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0;
// save to the cart data
$item['data']->price = $custom_price;
// new versions of WooCommerce may require (instead of line above)...
// $item['data']->set_price($custom_price);
}
}
}

Resources