Home Forums WoodMart support forum Upsells customization add_action problem

Upsells customization add_action problem

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #660873

    Alex
    Participant

    I noticed the peculiarities of the impact of the theme settings in the Single product > Related & Upsells section on the work of add_action

    If you set Hide in the settings of the “Upsells products position” block, then add_action does not work with the function, but it works if you leave it “Standard”

    For example,

    function custom_output_upsells() {
    	global $product;
    	$upsells = $product->get_upsell_ids();
    	if ( empty( $upsells ) ) {
    		return;
    	}
    	
    	echo '<aside class="container related-and-upsells" data-nosnippet="true">';
    	woocommerce_upsell_display( 24, 4 ); 
    	echo '</aside>';
    }
    
    add_action( 'wp', 'remove_miii', 1010 );
    function remove_miii() {
    	if ( ! is_product() ) {
    		return;
    	}
    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
    remove_action( 'woocommerce_after_single_product_summary', 'woodmart_upsells_products', 15 );
    remove_action( 'woocommerce_after_single_product', 'woocommerce_upsell_display', 15 );
    remove_action( 'woocommerce_after_single_product', 'woodmart_upsells_products', 15 );
    remove_action( 'woodmart_woocommerce_after_sidebar', 'woocommerce_upsell_display', 10 );
    remove_action( 'woodmart_woocommerce_after_sidebar', 'woodmart_upsells_products', 10 );
    remove_action( 'woodmart_before_sidebar_area', 'woocommerce_upsell_display', 20 );
    remove_action( 'woodmart_before_sidebar_area', 'woodmart_upsells_products', 20 );
    }
    
    add_action( 'template_redirect', 'dodaty_miii', 101 );
    function dodaty_miii() {
    	if ( ! is_product() ) {
    		return;
    	}
    add_action( 'woocommerce_after_single_product', 'custom_output_upsells', 90 );
    }

    This is one of the examples, I tested different combinations – without remove_action at all, with add_action at the wp level, with remove_action and add_action in one function at the wp level. The result is the same, this setting affects the work of add_action (does not work if Hide)

    PS. At the same time, there is no such problem with Related products. add_action works if in the “Show related products” block of the theme settings – OFF

    #660889

    Hung Pham
    Keymaster

    Hello Alex,

    Thanks for reaching to us.

    1. You should deactivate all plugins (except Required Plugins) and then reactivate them one by one. This makes it very easy to isolate the plugin causing problems.

    2. Could you please check how it works with some default WordPress themes like TwentyTwenty or WooCommerce Storefront to understand is it our theme issue or not?

    Best Regards,

    #660910

    Alex
    Participant

    I didn’t disable the plugins, because they all have to be enabled. Instead, I switched to Twenty Twenty-Five for a minute (because it doesn’t really fit the design and operation of the site) – I see that add_action works

    This confirms that it is the theme settings that affect the operation of add_action

    Here are my plugins, there were no manual changes to the theme code except through these plugins.
    Advanced Google reCAPTCHA, Classic Widgets, Code Snippets (I provided you with a snippet fragment from here, all other snippets do not apply to the product page and cannot affect it), Custom Taxonomy Order, Cyr-To-Lat, GTM4WP – A Google Tag Manager (GTM) plugin for WordPress, Instant Indexing, Loco Translate, MC4WP: Mailchimp for WordPress, Polylang, Polylang for WooCommerce, Print Invoice & Delivery Notes for WooCommerce, PWA, Rank Math SEO, W3 Total Cache, WooCommerce, Woodmart Core, WP-Mail-SMTP

    #660919

    Alex
    Participant

    By the way, after switching between themes, returning to Woodmart Child, I immediately noticed a few things:

    Warning on the main page:
    Warning: Undefined array key “file” in /home/siteua/site.com/www/wp-includes/media.php on line 1768
    Warning: Undefined array key “file” in /home/siteua/site.com/www/wp-includes/media.php on line 1768
    (I change here site name in path)

    Notification in the admin panel:
    Thumbnail regeneration is performed in the background. Depending on the number of images in your store, this will take some time.

    I also noticed that all my customized Woodmart widgets disappeared from the “Store Page Widget Area”. Now they need to be manually restored there.

    This should be warned about when you say to switch the theme for verification.

    #660983

    Alex
    Participant

    I fixed the errors after switching the theme.
    1. Warning: Undefined array key “file” – related to SVG loaded in the blocks of the Gutenberg theme – Infobox.
    It arose due to the loss of the path after switching the theme and the fact that I had long ago disabled the “Allow SVG upload” function and removed the Safe SVG plugin. And I also have Polylang. When switching between themes, thumbnail regeneration was started, which did not occur due to these features.
    I fixed it by enabling the function, the plugin and reloading the SVG.

    3. Regarding widgets – I manually added them again.

    Regarding the Upsells add_action problem, I await your consideration, it looks like an error in the theme.

    #661252

    Hung Pham
    Keymaster

    Hi Alex,

    To assist you in the best possible manner, I kindly ask that you please provide me temporary wp-admin info (wp-admin URL, username, password) and screenshots that illustrate the problem to the Private Content area, this will allow me to thoroughly investigate and address your concerns more efficiently.

    Kind Regards,

    #661293

    Alex
    Participant

    Theme settings:
    Single product > Related & Upsells > “Upsells products position” block
    If
    – Standard – All ok. If this is selected, upsells are displayed and the add_action and my function work.
    – Hide – If this is selected, upsells disappear from the product page completely, my add_action and function do not work, although they should.

    #661295

    Alex
    Participant

    Another problem that I haven’t written about in this thread yet is the loading speed (performance) if you display a lot of additional products (dozens).

    The Upsells, Related and Cross Sells blocks affect the loading speed of the product page. Caching and optimizations don’t help much here.

    Therefore, I posted a proposal (for voting) to implement Ajax for them – https://woodmart.canny.io/feature-requests/p/deferred-loading-via-ajax-of-product-blocks-upsells-related-cross-sells-etc-and

    I also contacted Woocommerce developers regarding the same issue https://github.com/woocommerce/woocommerce/issues/58004

    Their response:
    “Thanks for the suggestions! Frontend performance is something we will be looking at more closely (and wholistically) in the nearish future (1-2 months) and strategies like what you describe will be among the considerations. However, since this is something already being tracked elsewhere and not immediately actionable, I’m going to close the issue.”

    #661475

    Hung Pham
    Keymaster

    Hi Alex,

    I’ve passed this topic to one of our developers, and I will let you know once get an answer.

    Thanks for your patience.

    Best Regards,

    #662505

    Hung Pham
    Keymaster

    Hi Alex,

    Thanks for your patience.

    The Upsells products position option does indeed affect your custom functionality, but not the add_action itself.

    This is because you’re using the woocommerce_upsell_display function, which calls a template from the theme. That template already includes a check for the Upsells products position option, and unfortunately, this cannot be changed.

    In this case, we recommend using the Single product layout option, where this issue does not occur. Further, you can read more about the Layout Builder here: https://xtemos.com/docs-topic/woodmart-woocommerce-layout-builder/

    Best Regards,
    Hung PD

    #662548

    Alex
    Participant

    Unfortunately, I can’t use Single product layout in this case because I have my own Cross sells output code on the product page, and the Single product layout in the Gutenberg block “Products” (from the theme) doesn’t contain settings for it.

    I previously created a proposal about displaying cross-sales on the product page:
    https://woodmart.canny.io/feature-requests/p/cross-sell-output-on-product-page-and-theme-filter-for-them

    #662558

    Alex
    Participant

    To be even more precise, there is a Gutenberg block for cross-selling from WooCommerce, but I need theme styles and theme settings (there is none in the Gutenberg blocks from the theme).

    • This reply was modified 2 months, 2 weeks ago by Alex.
    • This reply was modified 2 months, 2 weeks ago by Alex.
    #662751

    Hung Pham
    Keymaster

    Hi Alex,

    Unfortunately, that at the moment we have no way to launch Cross sells products.

    Best Regards,
    Hung PD

    #663190

    Alex
    Participant

    Converted to layouts. Cross-sale output on the product page via shortcode.

    The following question arose:
    For upsells and related in the Gutenberg block there is an option “Init carousel on scroll”, when I activate it I see that the scroll-init class is added to these carousels.
    How to activate “Init carousel on scroll” in my own code for cross-sales? I looked through the code and documentation, I did not find any information.

    Now the code for outputting the shortcode is as follows:

    add_shortcode('my_product_cross_sells2', function($atts) {
        global $product;
    
        if ( ! is_product() || ! $product instanceof WC_Product ) {
            return '';
        }
    
        $atts = shortcode_atts([
            'limit' => 24,
            'columns' => 4,
            'orderby' => 'rand',
            'order' => 'desc'
        ], $atts, 'my_product_cross_sells2');
    
        $cross_sells = array_filter(
            array_map('wc_get_product', $product->get_cross_sell_ids()),
            'wc_products_array_filter_visible'
        );
    
        if (empty($cross_sells)) return '';
    
        if (function_exists('woodmart_lazy_loading_init')) {
            woodmart_lazy_loading_init(true);
        }
        if (function_exists('woodmart_enqueue_inline_style')) {
            woodmart_enqueue_inline_style('lazy-loading');
        }
    	
    
        ob_start();
    
        wc_set_loop_prop('name', 'cross-sells');
        wc_set_loop_prop('columns', apply_filters('woocommerce_cross_sells_columns', $atts['columns']));
    
        $orderby = apply_filters('woocommerce_cross_sells_orderby', $atts['orderby']);
        $order = apply_filters('woocommerce_cross_sells_order', $atts['order']);
        $cross_sells = wc_products_array_orderby($cross_sells, $orderby, $order);
    
        $limit = apply_filters('woocommerce_cross_sells_total', $atts['limit']);
        $cross_sells = $limit > 0 ? array_slice($cross_sells, 0, $limit) : $cross_sells;
    
        wc_get_template(
            'cart/cross-sells.php',
            [
                'cross_sells' => $cross_sells,
                'posts_per_page' => $atts['limit'],
                'orderby' => $atts['orderby'],
                'columns' => $atts['columns'],
            ]
        );
    	
    
        return ob_get_clean();
    });
    #663224

    Hung Pham
    Keymaster

    Hi Alex,

    Unfortunately, it requires customization and out of our basic support. Thanks for understanding our limitations.

    Regards,
    Hung PD

Tagged: 

Viewing 15 posts - 1 through 15 (of 15 total)