I have been using this code sucessfully for nearly 2 years, however I'm wondering if the latest Woocommerce updates have changed it somewhat as I now get a warning error in the log regarding this line $thumbnail = $product->get_image(array( 60, 120));
Is it possible this needs updating, and if so, what to? Any help would be much appreciated
add_filter( 'woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3 );
function display_product_image_in_order_item( $item_name, $item, $is_visible ) {
// Targeting view order pages only
if( is_wc_endpoint_url( 'view-order' ) ) {
$product = $item->get_product();
$thumbnail = $product->get_image(array( 60, 120));
if( $product->get_image_id() > 0 )
$item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
}
return $item_name;
}
Related
I am using some custom logics to show or hide variations in variable products using this hook woocommerce_get_children https://stackoverflow.com/a/67804835/6829420 and its working fine as per my expectations.
However, I am having wrong 'Sign Up Fee' in the variable product page because its keep getting the values from one of my hidden variations. Here below is my try and what I have done so far.
add_filter( 'woocommerce_subscriptions_product_price_string', 'subscr_product_price_string', 10, 3 );
function subscr_product_price_string( $subscription_string, $product, $include ){
if (is_admin() || (! $product->is_type( 'variable-subscription' ))) return $subscription_string;
if ( $include['sign_up_fee'] && $product->get_sign_up_fee( $product ) > 0 ) {
$available_variations = $product->get_available_variations();
echo count($available_variations);
echo $product->get_sign_up_fee();
exit;
//echo $prices = get_price_sign_up_fee($product);
exit;
}
Say for example, I have 3 variations "Blue, Yellow and Green" and I have hide "Blue" and go to variation product page.
Then If I do this,
$available_variations = $product->get_available_variations();
echo count($available_variations);
Then its showing 2 but my echo $product->get_sign_up_fee(); keep getting value from "Blue" which has the lowest value but this is wrong value.
Can someone guide me how can I get sign up values based on my available variations only.
I have already updated default From value using below code with woocommerce_variable_price_html hook and its working fine as its getting value based on available variations.
function updateFromDefaultValue($price, $product) {
$prefix = sprintf('%s: ', __('From', 'iconic'));
$min_price_regular = $product->get_variation_regular_price('min', true);
$min_price_sale = $product->get_variation_sale_price('min', true);
$max_price = $product->get_variation_price('max', true);
$min_price = $product->get_variation_price('min', true);
$price = ($min_price_sale == $min_price_regular) ?
wc_price($min_price_regular) :
'<del>' . wc_price($min_price_regular) . '</del>' . '<ins>' . wc_price($min_price_sale) . '</ins>';
return ($min_price == $max_price) ?
$price :
sprintf('%s%s', $prefix, $price);
}
add_filter('woocommerce_variable_price_html', 'updateFromDefaultValue', 10, 2);
Thanks
UPDATE
I have found a hook woocommerce_subscriptions_product_price_string_inclusions through which I am getting an updated sign_up_fee value based on my "Available Subscription". Below is my code.
add_filter('woocommerce_subscriptions_product_price_string_inclusions', 'updateSubscriptionPeriod', 10, 2);
function updateSubscriptionPeriod( $include, $product ) {
if (is_admin() || (! $product->is_type( 'variable-subscription' ))) return $include;
$min_max_variation_data = $product->get_min_and_max_variation_data( array_keys( $prices ) );
if(empty($min_max_variation_data) || empty($min_max_variation_data['subscription'])) return $include;
$include['sign_up_fee'] = $min_max_variation_data['subscription']['signup-fee'];
$include['trial_length'] = 5;
$include['subscription_period'] = 50;
$include['subscription_length'] = $min_max_variation_data['subscription']['interval'];
$include['subscription_price'] = $min_max_variation_data['max']['price'];
return $include;
}
Now I am able to get value 20, please see screenshot below.
But as you can see the difference in my screenshot, I am still not getting the updated values here every 3 years with a 2-week free trial a as it should be said, very 4 years with a 2-month free trial because this is what my values from my available variations.
You can also see I am trying to put some static values as well as dynamic values like this.
$include['trial_length'] = 5;
$include['subscription_period'] = 50;
$include['subscription_length'] = $min_max_variation_data['subscription']['interval'];
$include['subscription_price'] = $min_max_variation_data['max']['price'];
But its just not updating in my product page.
Can someone guide me please how can I update these due values.
Thanks in advance.
UPDATE 2
I have searched across and found woocommerce_subscriptions_product_price_string hook and here is my attempt.
add_filter( 'woocommerce_subscriptions_product_price_string', 'subscriptions_custom_price_string', 20, 3 );
function subscriptions_custom_price_string( $price_string, $product, $args ) {
// Get the trial length to check if it's enabled
$trial_length = $args['trial_length'];
$speriod = $args['subscription_period'];
$sfee = $args['sign_up_fee'];
$sfee = wc_price($sfee);
$sign_up_fee = isset($args['sign_up_fee']) ? __(" and a $sfee sign-up fee", "woocommerce") : '';
if( $trial_length > 0 )
$price_string = $args['price'] . ' / ' . $speriod . $sign_up_fee;
return $price_string;
}
And its showing like this below.
So now basically its updating but its not correct as I lost a format and some other important values.
Can someone guide how can I properly overwrite this filter ?
Thanks
woocommerce_variable_subscription_price_html is the correct hook that you should be using, and here's an example that might work for you:
add_filter( 'woocommerce_variable_subscription_price_html', 'variable_subscription_custom_price_html', 10, 2 );
function variable_subscription_custom_price_html( $price, $product ) {
// Unhook to avoid infinite loop.
remove_filter( 'woocommerce_variable_subscription_price_html', __FUNCTION__, 10, 2 );
// List of product IDs for variations you're hiding.
$hidden_products = array( 4171 );
// Check if the product has any variations that you manually hid.
$hidden = array_intersect( $hidden_products, get_children( array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'fields' => 'ids',
) ) );
if ( ! empty( $hidden ) ) {
$prices = $product->get_variation_prices( true )['price'];
asort( $prices ); // sort from lowest to highest price
$variation = wc_get_product( array_keys( $prices )[0] );
// It's a variation, so we manually add the 'From: text'.
$price = 'From: ' . $variation->get_price_html();
}
// Re-hook this function.
add_filter( 'woocommerce_variable_subscription_price_html', __FUNCTION__, 10, 2 );
return $price;
}
I am using a function to add custom meta to products. I have used the following hooks for SHOW on Product Loop woocommerce_after_shop_loop_item and Product Single Page woocommerce_product_meta_end.
So, when applying to get the same results on CART PAGE product/item by using the hook
woocommerce_after_cart_item_name it doesn’t work with no results.
Why isn’t the hook woocommerce_after_cart_item_name working if it works with the other previous hooks mentioned?
This the code I am using. I just change the hook to make it to show in Product Loop and Product Single Page, need it to show on cart Products as well. I was just wondering why it doesn't work with cart item hook..
public function woocommerce_after_cart_item_name()
{
global $product;
if ( !PluginOptions::value('shop_season') && !PluginOptions::value('shop_car_type') )
return;
$product_id = $product->get_id();
$season = get_post_meta( $product_id, 'season', true );
$car_type = get_post_meta( $product_id, 'car_type', true );
$tips_seasons = $this->ui_tips_season();
$tips_car_types = $this->ui_tips_car_types();
?>
<div class="tyre-details tyre-tips">
<?php if ( PluginOptions::value('shop_season') && $season ): ?>
<?php echo $tips_seasons[ strtolower($season) ]; ?>
<?php endif ?>
<?php if ( PluginOptions::value('shop_car_type') && $car_type ): ?>
<?php echo $tips_car_types[ strtolower($car_type) ]; ?>
<?php endif ?>
</div>
<?php
}
It is from a plugin. I was just given this code from woocommerce support but i do not know how to complete it. He says to replace with my meta_keys in the code which I will post below here. Can you help me finish this? or tell me where I need to replace. My meta_keys are $season and $car-type but i don't know how to apply with the code provided by woocommerce.
// Display custom cart item meta data (in cart and checkout)
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
$meta_key = 'PR CODE';
if ( isset($cart_item['add_size']) && isset($cart_item['add_size'] [$meta_key]) ) {
$item_data[] = array(
'key' => $meta_key,
'value' => $cart_item['add_size'][$meta_key],
);
}
return $item_data;
}
// Save cart item custom meta as order item meta data and display it everywhere on orders and email notifications.
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
$meta_key = 'PR CODE';
if ( isset($values['add_size']) && isset($values['add_size'][$meta_key]) ) {
$item->update_meta_data( $meta_key, $values['add_size'][$meta_key] );
}
}
I located the ClassName (class FeatureTyreTips) and added that to the code you provided. I entered this in functions.php and it said fatal error when loading the cart page. I also tried placing it in the cart.php with same results...and I tried in the same plugin file where this code came from originally. All 3 locations did not work...only difference was that it did not show fatal error when adding and activating your new code in the plugin file when loading cart page. Any ideas? grazie Vdgeatano
The "Fatal Error" is most likely due to the fact that "the non-static method cannot be called statically": PHP - Static methods
I have now corrected the code.
Considering that the class name that was missing in your code is FeatureTyreTips, the correct code to add some text after the product name is:
add_action( 'woocommerce_after_cart_item_name', 'add_custom_text_after_cart_item_name', 10, 2 );
function add_custom_text_after_cart_item_name( $cart_item, $cart_item_key ) {
// create an instance of the "PluginOptions" class
$PluginOptions = new PluginOptions();
if ( ! $PluginOptions->value( 'shop_season' ) && ! $PluginOptions->value( 'shop_car_type' ) ) {
return;
}
$product = $cart_item['data'];
$season = get_post_meta( $product->get_id(), 'season', true );
$car_type = get_post_meta( $product->get_id(), 'car_type', true );
// create an instance of the "FeatureTyreTips" class
$FeatureTyreTips = new FeatureTyreTips();
$tips_seasons = $FeatureTyreTips->ui_tips_season();
$tips_car_types = $FeatureTyreTips->ui_tips_car_types();
if ( ! $PluginOptions->value('shop_season') || ! $season ) {
$season = '';
}
if ( ! $PluginOptions->value('shop_car_type') || ! $car_type ) {
$car_type = '';
}
$html = '<div class="tyre-details tyre-tips">' . trim( $season . ' ' . $car_type ) . '</div>';
echo $html;
}
The code has been tested (where possible) and works.It needs to be added to your theme's functions.php file or in your plugin.
I have three versions of this code in my site to product 3 buttons ( 1x quantity, 3x quantity and 10x quantity). My products are variable. The code didn't work until I removed this line if( ! $product->is_type('simple') ) return; . With this line removed, should it be replaced with something else for variable products to ensure the code is complete properly?
add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart_10', 20 );
function additional_simple_add_to_cart_10() {
global $product;
// Only for simple product type
if( ! $product->is_type('simple') ) return; // Removed this line
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=10';
$class = 'ingle_add_to_cart_button-10 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$button_text = __( "10 Tickets", "woocommerce" );
// Output
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
}
// Redirect to checkout after add to basket
add_filter( 'woocommerce_add_to_cart_redirect', 'misha_skip_cart_redirect_checkout' );
function misha_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
I would like to filter the products on a category products so that it shows only the products of a certain author or multiple authors.
I already have the following code. This works as it filters the products. The correct products are displayed. Except the Woocommerce filters on in the left sidebar are not affected by the filter. The filters on the left side are showing all the original products in the category (also from other users) so the count isn't correct and also attributes from products that are filtered are showing. This shouldn't be the case. Do I have to add another pre_get_posts for the filters?
<?php
function pre_get_posts_by_author( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
$cat_obj = $q->get_queried_object();
if($cat_obj->name == 'Nieuw')
{
$q->set( 'author_ids', '2086,2084');
}
}
add_action( 'pre_get_posts', 'pre_get_posts_by_author' );
add_filter( 'posts_where', 'author_posts_where', 10, 2 );
function author_posts_where( $where, &$wp_query )
{
global $wpdb;
if ( $wp_query->get( 'author_ids' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_author IN (' . $wp_query->get( 'author_ids' ) .')';
}
return $where;
}
?>
Thanks for helping out!
Probabaly a better way to do this is by setting 'author__in' argument in your query which will take an array of authors the query will return products of.
function pre_get_posts_by_author( $q ) {
if ( ! $q->is_main_query() || !$q->is_post_type_archive() ) return;
$cat_obj = $q->get_queried_object();
if( $cat_obj->name == 'Nieuw' ){
$q->set( 'author__in', array(2086,2084));
}
}
add_action( 'pre_get_posts', 'pre_get_posts_by_author' );
My updated solution: still dirty as it modifies Woocommerce's core files (2.2.4), but it works:
in the pre_get_posts-hook I retrieve the ids of the products I want to exclude with the following code:
$_SESSION['total_excluded'] = get_objects_in_term( $term_ids, $taxonomies, $args )`
(reference: http://codex.wordpress.org/Function_Reference/get_objects_in_term)
Then in woocommerce/includes/widgets/class-wc-widget-layered-nav.php I changed line 258 to:
$count = sizeof( array_diff(array_intersect( $_products_in_term, WC()->query->filtered_product_ids) , $_SESSION['total_excluded'] ) );
In woocommerce/includes/widgets/class-wc-widget-price-filter.php the new lines 121, 136 are:
%1$s.ID IN (' . implode( ',', array_map( 'absint', array_diff(WC()->query->layered_nav_product_ids, $_SESSION['total_excluded'] ) ) ) . ')
In woocommerce/includes/widgets/class-wc-widget-price-filter.php the new lines 123, 138 are:
%1$s.post_parent IN (' . implode( ',', array_map( 'absint', array_diff(WC()->query->layered_nav_product_ids, $_SESSION['total_excluded'] ) ) ) . ')
Hi I use Woocommerce plugin. All works great, I have added a custom field or attribute to a product which is fine and it shows on the products detail page but my problem is getting this data to come through in the PayPal IPN.
I am trying to edit this code as I believe the custom field data is held in meta but cannot find the id of my custom field or where to add it:
// Cart Contents
$item_loop = 0;
if ( sizeof( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( $item['qty'] ) {
$item_loop++;
$product = $order->get_product_from_item( $item );
$item_name = $item['name'];
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
if ( $meta = $item_meta->display( true, true ) )
$item_name .= ' ( ' . $meta . ' )';
$paypal_args[ 'item_name_' . $item_loop ] = html_entity_decode( $item_name, ENT_NOQUOTES, 'UTF-8' );
$paypal_args[ 'quantity_' . $item_loop ] = $item['qty'];
$paypal_args[ 'amount_' . $item_loop ] = $order->get_item_subtotal( $item, false );
if ( $product->get_sku() )
$paypal_args[ 'item_number_' . $item_loop ] = $product->get_sku();
}
}
}
Need to change script that send "order_id" to PayPal on script to send my custom field to PayPal. That's all.
Appreciate any answers. Thanks.!