WooCommerce-Send Custom Selected Price in Cart - wordpress

Hello Everyone I have customized the woo-commerce single product page according to my requirement, So now two prices are coming first price is my custom price as per my selection, Second price i have entered from WooCommerce product page(entered from wp-admin dashboard), right now second price is going in cart but i want to pass custom price how can i achieve this please suggest me thank you in advance.
Using ajax I am changing my custom price
Custom Price:
<p class="prices" id="demo" style="margin-left:22%;"></p>
Default Price:
<p class="prices" id="demo" style="margin-left:22%;"><?php echo $product->get_price_html(); ?></p>
Update:
I am using this code
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price');
function add_custom_price( $cart_object ) {
$custom_price = 70; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
right now 70 is static price instead of this i want to pass paragraph because I am getting my price in paragraph using ajax..
<p class="prices" id="demo" style="margin-left:22%;"></p>

add_action('woocommerce_get_price','change_price_custom', 10, 2);
function change_price_custom($price, $productd){
return $price*2; // do calculation here
}
Try this in theme's function.php

Related

Default product short description

On my woocommerce site I want to add the text "Only available in stores" to all my product's short description, and also have that same text be default for all future products I add.
I had a search but the solutions seemed way to complex for me. Is there some code I can paste in the functions.php?
Thanks!
You can solve this problem easily. You need to some code push to theme functions.php or use code snippets plugin. This code only works when the WooCommerce product short description is empty.
function wp_woocommerce_short_description_if_empty(){
global $post;
if (empty($post->post_excerpt)) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'Your Custom Message Here.';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
add_action('woocommerce_single_product_summary', 'wp_woocommerce_short_description_if_empty', 21);
You can also try this for your default product description, you can Add Custom Text before/after the Product Short Description
add_filter( 'woocommerce_short_description', 'woo_add_text_after_excerpt_single_product', 20, 1 );
function woo_add_text_after_excerpt_single_product( $post_excerpt ){
/* Method 1: Add Custom Text before the Product Short Description on product page */
/* $content= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $content.'<br>'.$post_excerpt;
*/
/* Method 2: Add Custom Text after the Product Short Description on product page */
$post_excerpt .= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $post_excerpt;
}
Note: Add Custom Text before the Product Short Description on the product page - code is commented so you can uncomment accordingly.

Change Buy button for variants in backorder Woocommerce

Dears,
The following code works ok to change the Add to Cart text on single products when it is on Backorder enabled.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'change_add_to_cart_single_prod', 10, 2 );
function change_add_to_cart_single_prod( $text, $product ){
if ( $product->is_on_backorder( 1 ) ) {
return __( 'Reservar', 'woocommerce' );
}
return __( 'Comprar', 'woocommerce' );
}
But it does not work with variations unless the every variation is in backorders or out of stock. What may be the best way to change the Add to Cart button to a variations with Backorders allowed?
I am guessing it has to be done with js.
When a product variation is selected, a hidden input field is set in the variation form with the id of the selected product variation. For example:
<input type="hidden" name="variation_id" class="variation_id" value="44">
If the selected attributes do not match any product variation, the
value will be blank.
All possible product variations are in the data-product_variations attribute of the variation form element.
Then you can use this jQuery script to change the button name. It will be Reservar if the product variation allows backorders while Comprar if it does not.
// change the text of the add to cart button based on product backorders
add_action( 'wp_footer', 'change_the_text_of_the_add_to_cart_button' );
function change_the_text_of_the_add_to_cart_button() {
?>
<script type="text/javascript">
jQuery(function($){
$('input[name=variation_id].variation_id').change(function(){
const variationID = $(this).val();
const variationData = $('form.variations_form').data("product_variations");
$(variationData).each(function(index,variation){
if ( variationID == variation.variation_id ) {
if ( variation.backorders_allowed ) {
$('form.variations_form button[type=submit]').text('Reservar');
} else {
$('form.variations_form button[type=submit]').text('Comprar');
}
}
});
});
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.

add an icon after product price on woocommerce

Hello as the title says.
i'm after a solution for wordpresss woocommerce with products that needs to have a added icon with a van.
same as the following link
Add an icon after product price on Woocommerce archive pages
However i've managed to made categories with fa fast-shipping and gotten the icon.
I've added the code
add_filter( 'woocommerce_get_price_html', 'prepend_append_icon_to_price', 10, 2 );
function prepend_append_icon_to_price( $price, $product ) {
if( has_term( 'fast-shipping', 'product_cat', $product->get_id() ) && ! is_product() ){
$price .= '<span style="float:right"><i class="fas fa-shipping-fast"></i></span> ';
}
return $price;
}
but it still doesn't want to pop up on my product which i dont understand.
Display Picture
I am not sure about this. Try going to the product code(where your product page with the product is shown) which will be located in themes mostly and then go below the pic and add your icon. I hope this helps :) Let me know if you find it difficult to figure it out and ill help you

URL to product page with quantity inputed - WooCommerce

I am trying to find a way to edit a product url in WooCommerce so that when visited a quantity is pre-selected for the quantity input, e.g. 2.
I know there is an add to cart url for it:
http://yourdomain.com/?add-to-cart=47&quantity=2
But was wondering if it was possible to just input the quantity on the product page with a similar URL.
Thanks for any ideas on the matter.
you can do it like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_input_args' ); // Simple products
add_filter( 'woocommerce_available_variation', 'custom_woocommerce_quantity_input_args' ); // Variations
function custom_woocommerce_quantity_input_args( $args ) {
if ( isset( $_GET['qty'] ) && is_numeric($_GET['qty']) ) {
$args['input_value'] = $_GET['qty'];
}
return $args;
}
with that you can then do http://yourdomain.com/product/ship-your-idea/?qty=10.

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources