How can I get the product url? - wordpress

I am adding an extra button on single product page. I would like to get the button url from the product.
I already have the following code
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
global $product;
echo 'Extra Button';
}
what will be the editing to get href from the product on single product page?

What you are looking for is the following get_permalink( $product->get_id() );
https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
then you get
function my_extra_button_on_product_page() {
global $product;
// Get product id
$product_id = $product->get_id();
// Get url
$url = get_permalink( $product_id );
echo 'Extra Button';
}
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );

Related

Woocommerce: add 'Free Delivery' badge to product thumbnail in Woocommerce based on shipping class

i want add, on product listing, over image a text badge "Free delivery" based on specific shipping class "spedizione-gratuita".
You can help me ?
I test this code but dont work
add_action( 'woocommerce_before_shop_loop_item_title', 'single_product_label', 10 );
function single_product_label() {
global $product;
$shipping_classes = 'spedizione-gratuita';
if ( $product->get_shipping_class() ) {
echo '<div class="spedizione-gratuita"><span class="freedel">SPEDIZIONE GRATUITA</span></div>';
}
}
Tnks
I believe this will achieve what you are trying to do. You almost had it, but the $product global has no value in the shop loop you need to use $post instead
add_action( 'woocommerce_before_shop_loop_item_title', 'single_product_label', 10 );
function single_product_label() {
global $post;
$_product = wc_get_product( $post->ID );
$shipping_classes = 'spedizione-gratuita';
if ( $shipping_classes == $_product->get_shipping_class() ) {
echo '<div class="spedizione-gratuita"><span class="freedel">SPEDIZIONE GRATUITA</span></div>';
}
}

Remove product title when product is "out of stock" on WooCommerce shop and archives pages

I am running an art gallery website using WooCommerce and the owner does not want to show the product name/title if the product has been sold/out of stock.
I have got this far with putting together a function, but it doesn't work. I wondered if anyone could give me any tips?
// Hides title on Sold products
add_filter( 'woocommerce_shop_loop_item_title', 'remove_name', 10, 2 );
function remove_name ( $product ) {
if ( ! $product->is_in_stock()) {
$title = '';
}
return $title;
}
Your code contains some errors and mistakes:
woocommerce_shop_loop_item_title is NOT an filter but an action hook
No arguments are passed to this hook , while your code says there would be 2
$product is undefined, use global $product;
Note: I've added a line with debug information, it can be removed
So you get:
function action_woocommerce_shop_loop_item_title() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Should be removed but may be useful towards debugging
echo '<p style="color: red; font-size: 20px;">DEBUG information: ' . $product->get_stock_status() . '</p>';
// NOT in stock
if ( ! $product->is_in_stock() ) {
// Removes a function from a specified action hook.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
}
}
}
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 9 );
Please try to use this hook to remove product title. Use this inside the condition to check if product is in stock:
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
This does what you want but still allows access to the product page.
add_action( 'woocommerce_shop_loop_item_title', function() {
global $product;
if ( ! $product->is_in_stock()) {
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
}
}, 5 );
add_action( 'woocommerce_single_product_summary', function() {
global $product;
if ( ! $product->is_in_stock()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);
}
}, 1);

Issue hide product single price for variable products on WooCommerce shop and archives pages

Im using this snippet to hide single price if the product is variable. Works fine but not apply on first product variable of loop:
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price' );
function hide_single_price() {
global $product;
if ( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
Any idea why?
I don't immediately see a problem with your current code, what you can try:
Adjust the priority value
Echo the text 'debug'. So you can check if that is displayed for the first product.
function hide_single_price() {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
if ( $product->is_type( 'variable' ) ) {
// Debug purposes, delete afterwards!!
echo 'DEBUG!';
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price', 5 );

WooCommerce Shop page thumbnail wrap

I am looking at this: https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/
I would like to wrap thumb in shop page into another div.
If I use following code I could achieve something, but the problem is many themes remove or dont use woocommerce_before_shop_loop_item_title and use some different code and I cannot effectively do my action.
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'my_woocommerce_before_shop_loop_item_title', 10 );
function my_woocommerce_before_shop_loop_item_title() {
global $product;
$post_id = $product->get_id();
$html = woocommerce_get_product_thumbnail('woocommerce_thumbnail');
echo '<div class="my">' . $html . '</div>';
}
Is there action or hook that processes product thumbnail to which I could do my own action? Regardless of what is happening before and after that?
Please try to implement in this way.
//replace the functionprefix with your functions prefix
function functionprefix_wrap_loop_product_image() {
if ( ! class_exists( 'WooCommerce' ) ) return; //* exit if WooCommerce not active
add_action( 'woocommerce_before_shop_loop_item_title' , 'functionprefix_product_loop_image_wrapper_open', 10 );
add_action( 'woocommerce_shop_loop_item_title' , 'functionprefix_product_loop_image_wrapper_close', 10 );
}
add_action( 'woocommerce_before_shop_loop' , 'functionprefix_wrap_loop_product_image', 3 );
//replace the functionprefix with your functions prefix
function functionprefix_product_loop_image_wrapper_open() {
echo '<div class="image-wraper">';
}
function functionprefix_product_loop_image_wrapper_close() {
echo '</div>';
}

Add custom button in cart page to add a particular product in cart

Trying to add a custom button in the cart page. If the button is clicked particular product should add and cart page should refresh with ajax load. I used below code but it is refreshing the page without updating cart prices.
add_filter( 'woocommerce_cart_item_price', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('pro_price_extra_info') ) {
$product_name .= '<a class="gt_price" href="example.com/cart/?add-to-cart=250">Get this price</a>';
}
return $product_name;
}
Please help me with the code. Thanks in advance
I've just tested your code and it worked as you mentioned. All you need is to add a line to recalculate cart totals: "WC()->cart->calculate_totals();"
Based on your code:
add_filter( 'woocommerce_cart_item_price', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('pro_price_extra_info') ) {
$product_name .= '<a class="gt_price" href="example.com/cart/?add-to-cart=250">Get this price</a>';
}
WC()->cart->calculate_totals();
return $product_name;
}
For a better position for your button you may try this ():
add_action( 'woocommerce_cart_collaterals', 'custom_add_to_cart_redirect' );
function custom_add_to_cart_redirect() {
echo '<button class="de-button-admin de-button-anim-4">Get Your Discounted Product!</button>';
}
Cart Page Hooks: https://www.tychesoftwares.com/woocommerce-cart-page-hooks-visual-guide-with-code-examples/
I hope it works. Have a good day.

Resources