How do I display the categories of an individual product under the product name on the Shop page listing?
screenshot of Shop page:
You should try this to show the product category above the price of each product.
add_filter( 'woocommerce_get_price_html', 'woo_shipping_with_price_display' );
add_filter( 'woocommerce_cart_item_price', 'woo_shipping_with_price_display' );
function woo_shipping_with_price_display( $price ) {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$text = '';
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats );
$text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';
}
if ($price == true) {
return $text . $price;
}
else {
return $text;
}
}
You can change/update html,class etc.. from line:
$text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';
Related
I'm trying to check with my ERP if a product is in stock. So when you clic add to cart, I run a code checks if is in stock and then remove it if is out of stock. I want to show a message that said the product was remove cause outstock.
function custom_validate_stock() {
if(1==1){
$stockERP = 0;
if($stockERP < 1){
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product->id ){
WC()->cart->remove_cart_item($cart_item_key);
$removed = true;
break;
}
}
}
}
if($removed){
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
add_action( 'woocommerce_cart_is_empty', 'custom_empty_cart_message', 10 );
}
}
}
function custom_empty_cart_message() {
$html = '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">';
$html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your product is out of stock, please refresh the page.', 'woocommerce' ) ) );
echo $html . '</p></div>';
}
Right after
WC()->cart->remove_cart_item($cart_item_key);
I think you could add a notice:
wc_add_notice( __( 'Thing removed.', 'your-textdomain' ), 'error' );
I have advanced custom field plugin and i have added a custom field to my product the label name is (stock_number) my question is how do it display/show this field data in the new order email.
The below code should do the trick.
Option1:
add_action( 'woocommerce_order_item_meta_start', 'ts_order_item_meta_start', 10, 4 );
function ts_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
if( $stock_number = get_field( 'stock_number', $item->get_product_id() ) ;
echo $stock_number;
}
Option2:
add_action( 'woocommerce_email_order_details', 'display_stock_email_order_details', 10, 4 );
function display_stock_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
foreach( $order->get_items() as $item ) {
if( $stock_number = get_field( "stock_number", $item->get_product_id() ) ){
echo '<p><strong>'.__('Stock Number').': </strong>'.$stock_number.'</p>';
}
}
}
Option3:
The below code will replace the Product Title with the ACF Custom Value.
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Targeting email notifications only
if( is_wc_endpoint_url() )
return $item_name;
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $stock_number = get_field('stock_number', $product->get_id()) ) {
$item_name = '<p class="item-stck" style="margin:12px 0 0;">
<strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
}
return $item_name;
}
Option4:
The below code will replace the Product Name with your custom field.
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
// Get the WC_Product object (from order item)
$product = $item->get_product();
if( $stock_number = get_field('stock_number', $product->get_id()) ) {
$item_name = '<p class="item-stck" style="margin:12px 0 0;">
<strong>' . __( 'Stock Number', 'woocommerce' ) . ': </strong>' . $stock_number . '</p>';
}
return $item_name;
}
I am trying to change the link & text of the Add to Cart button on the archive page if the products is already in cart.
I only allow quantity one per product to be added.
But the default way of Woocommerce is to direct to the single page with the notice.
Instead I would like the user to either
See the text "View Cart" and direct the user to the cart
or
See the text "Added" and have no link whatsoever.
Thank you
To change the text, you need the codes below.
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "View Cart";
}
return $text;
}
To redirect to cart, this will do it.
add_action( 'wp_loaded', 'wc_add_to_cart_action', 19 );
function wc_add_to_cart_action(){
if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
return;
}
$product_id = absint( $_REQUEST['add-to-cart'] );
$adding_to_cart = wc_get_product( $product_id );
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && $adding_to_cart->is_sold_individually() ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
or use this for the second part.
add_action( 'woocommerce_simple_add_to_cart', 'wc_simple_add_to_cart' );
function wc_simple_add_to_cart(){
global $product;
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
echo sprintf( '<form class="cart">%s</form>', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ) );
}
}
update
for shop page you need this code:
add_filter('woocommerce_product_add_to_cart_url', 'wc_product_add_to_cart_url', 10, 2 );
function wc_product_add_to_cart_url( $url, $product ){
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && is_shop() ) {
$url = wc_get_cart_url();
}
return $url;
}
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php
I am working on woocommerce cart page with add_filter "woocommerce_in_cart_product_title", There I want to show price with title.
Like "Product title ($price)"
i have tried this code, but i can not get price.
add_filter( 'woocommerce_in_cart_product_title', 'cart_product_title', 20, 3);
function cart_product_title( $title, $values, $cart_item_key ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
print_r($items);
foreach ($items as $cart_item_key => $values) {
echo $name = $values['prodaddons'][0]['name'].' --- ';
echo $price = $values['prodaddons'][0]['price'].'<br>';
}
echo 'aaaa-'.$title.'-'.$pricees;
}
try this :
add_filter( 'woocommerce_cart_item_name', 'cart_product_title', 20, 3);
function cart_product_title( $title, $values, $cart_item_key ) {
return $title . ' - ' . $values[ 'line_total' ] . '€';
}
I don't know your version of woocommerce but woocommerce_in_cart_product_title is depreciated, it's why I use woocommerce_cart_item_name, so you should try both ;)