i want to remove woocommerce_order_item_visible - woocommerce

enter image description here
I want to remove the given image product variable area to be remove which code should I use in product order details file in woo commerce plugin.

function alter_woocommerce_order_item_name($name, $item, $is_visible) {
if (isset($item['product_id'])) {
$product_id = $item['product_id'];
$product = wc_get_product($product_id);
$product_permalink = $product->get_permalink();
$name = $product_permalink ? sprintf('%s', $product_permalink, $product->get_name()) : $product->get_name();
}
return $name;
}
add_filter('woocommerce_order_item_name', 'alter_woocommerce_order_item_name', 10, 3);
Add this code into your active theme functions.php file

Related

Show the name of the chosen variation under the product title on the My Account > Downloads page

By default WooCommerce shows the attribute of a variable product in the title and I'm using this code to show the attribute below the title in the cart and checkout pages:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
But that doesn't work in My Account page, users see the full product name with no attribute.
To fix it I'm using the code below to show the attribute in the product title:
function show_attributes_outside_title_1( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_2( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
But I'd like to show the attribute below the title (or a new column), it's easier to read and goes with the same desing you see in the cart and checkout pages.
There is some confusion in the initial part of the question.
You say you want to show the attribute under the product title on the cart and checkout page but then return __return_false, do you intend to do the opposite?
SOLUTION #1
You may want to reverse the check to make sure that your chosen product variation attribute is shown under the product name on your account page under Downloads (as evidenced by your comment above):
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_1( $enabled ) {
if ( is_account_page() ) {
$enabled = true;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
function show_attributes_outside_title_2( $enabled ) {
if ( ! is_account_page() ) {
$enabled = false;
}
return $enabled;
}
SOLUTION #2
If you want to leave the code in your question unchanged you can use the woocommerce_account_downloads_column_download-product hook where download-product is the id of the product name column (in the /my-account/downloads/ page). Here the documentation.
Finally, with the wc_get_formatted_variation function you can get the name of the chosen variation. For more information on parameters read the documentation.
// shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the produc
$product_name = $download['product_name'];
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name() . " - " . wc_get_formatted_variation( $product, true, false, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '';
} else {
echo esc_html( $product_name );
}
}
The code has been tested and works. Add it to your active theme's functions.php.
I changed Vincenzo's answer a bit to make it look the same way I see the attributes in my Cart and Checkout pages. Here's the code in case anybody else needs it:
// Shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the product
$product_name = $download['product_name'];
// define variable
$product_attributes = '';
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name();
$product_attributes = wc_get_formatted_variation( $product, true, true, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
} else {
echo esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
}
}
// Shows variation outside title in cart and checkout pages
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
The last two filters replace my code and the first part solves the issue in the Downloads page.

How can I modify get_price_html to create custom price display for on sale items

this is my first question on Stack Overflow...
I am modifying a WooCommerce website for a client. What they want is for all items which have a sale prices to display a red dot next to the sale price.
I have located the public function 'get_price_html' in abstract-wc-product.php:
/**
* Returns the price in html format.
*
* #param string $deprecated Deprecated param.
*
* #return string
*/
public function get_price_html( $deprecated = '' ) {
if ( '' === $this->get_price() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $this );
} elseif ( $this->is_on_sale() ) {
$price = wc_format_sale_price( wc_get_price_to_display( $this, array( 'price' => $this->get_regular_price() ) ), wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
} else {
$price = wc_price( wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
}
return apply_filters( 'woocommerce_get_price_html', $price, $this );
}
I have figured out that if I modify the 'elseif ( $this->is_on_sale()' section, I can do what I want, and this works, but only when I modify and upload the core version of abstract-wc-product.php, which I don't want to do.
I have child theme going, and in my child theme directory, I have copied abstract-wc-product.php into the following folder structure:
woocommerce/includes/abstracts
This isn't working, so I have also tried to copy the function above into my functions.php file, to see if this overrides the core version.
Neither of these is working - can anybody help with how to do this, please?
Thank you!
Stuart
If you can't use CSS, here's how you do it:
/* Here's the woocommerce filter. Then we use a static function (anonymous function) to pass the two arguments that the original filter passes.*/
add_filter( 'woocommerce_get_price_html', static function( $price, $product ) {
// Now we check to see if the product is onsale.
if ( $product->is_on_sale() ) :
// Change this to whatever worked for you when you modified core files.
$price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
endif;
// Return the $price. This is here in the event the product that is passed isn't on sale.
return $price;
}, 10, 2 );
This goes into your child theme functions.php.
Your key is in return apply_filters( 'woocommerce_get_price_html', $price, $this );
Check the doc here about how to use filters in WordPress: https://developer.wordpress.org/reference/functions/apply_filters/
In your case, I believe you can achieve your goal with this function below - not tested!!
function custom_sale_price( $price, $product ){
if( $product->is_on_sale() ){
return '<span class="red-dot"></span>' . $price;
}
return $price;
}
add_filter('woocommerce_get_price_html', 'custom_sale_price', 10, 2 );

Add fields in custom taxonomy in WordPress admin for saved data

I want to add custom fields in custom taxonomy for my plugin in form-fields and also show saved data as like name, slug and description are showed.
Someone suggest something!
function custom_column_header( $columns ){
$columns['address'] = 'Address';
$columns['phoneno'] = 'Phone No';
return $columns;
}
add_filter( "manage_edit-nwcm_news_category_columns", 'custom_column_header', 10);
// To show the column value
function custom_column_content( $value, $column_name , $term){
if ($column_name === 'address') {
$t_id = $term;
$term_meta = get_option( "$t_id" );
print_r($term_meta['address']);
}
if ($column_name === 'phoneno') {
$t_id = $term;
$term_meta = get_option( "$t_id" );
print_r($term_meta['phone']);
}
}
add_action( "manage_nwcm_news_category_custom_column", 'custom_column_content', 10, 3);

filter by price based on product category

I am developing site in WordPress using woo-commerce
I want to achieve the functionality that on single page price will be displayed by counting 50% discount on single product page
I had implemented the below code, it is working nicely on the simple products but not working on variable product
Please help me how can I achieve the custom price filtering on variable product
OR suggest me which filter to used to variable products
add_filter('woocommerce_get_price', 'custom_price_WPA111772', 10, 2);
function custom_price_WPA111772($price, $product) {
global $product;
$pid = get_the_ID();
$user = wp_get_current_user();
if(!is_user_logged_in()) return $price;
if( has_term( 'sale', 'product_cat' ,$product->ID) || has_term( 'Gift Ideas', 'product_cat' ,$product->ID))
{
$price = $price;
}
else
{
$price = $price * 50 / 100;
}
return $price;
}
I got the answer why my variable product is not showing custom price
i added this in function.php and its works
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}

Woocommerce: How do I get the product slug from the id?

I currently have the product id available in the cart and I need to retrieve the slug. How can I do this?
You can use get_post
$product = get_post( 27 );
$slug = $product->post_name;
echo $slug;
Alternatively to get_post, you can use get_product if you already have a product or need it for other purpose
$_pf = new WC_Product_Factory();
$product = $_pf->get_product($product_id);
$slug = $product->get_slug();
A product is a post. To retrieve the post slug, that correspond to post_name post field, from the post ID, the get_post_field() function can be used.
$product_slug = get_post_field('post_name', $product_id);
`Hi, in my case I did it as below: I needed to customize add to cart button entirely on shop page and category page. so I used $product object for this purpose. I needed product id, slug and name, so it did it. hope this will help you as well on cart page.`
add_filter( 'woocommerce_loop_add_to_cart_link', 'ij_replace_add_to_cart_button', 10, 2 );
function ij_replace_add_to_cart_button( $button, $product ) {
$productid = $product->id;
$productslug = $product->slug;
$productname = $product->name;
if (is_product_category() || is_shop()) {
$button_text = __("More Info", "woocommerce");
$button_link = $product->get_permalink();
$button = '<a href="'. $button_link .'" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="'. $productid.'" data-product_sku="" aria-label="Add “'.$productname.'” to your cart" rel="nofollow" data-productslug="'. $productslug.'" >' . $button_text . ' </a>';
return $button;
}
}

Resources