8000 Quantity buttons not added to products loaded with Infinite Scroll · Issue #285 · oceanwp/oceanwp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Quantity buttons not added to products loaded with Infinite Scroll #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
broiniac opened this issue Jul 10, 2020 · 4 comments
Open

Quantity buttons not added to products loaded with Infinite Scroll #285

broiniac opened this issue Jul 10, 2020 · 4 comments

Comments

@broiniac
Copy link

No description provided.

@marcofrasson
Copy link

@eramits any update?

@MarkHollo
Copy link
Contributor

Hello @marcofrasson ,
As you know, this year we changed the JS so all feature requests similar to this one need to be reexamined.

As we never had a default options for quantity buttons on archive pages, can you tell me how do you implement it so that we can replicate this on our end to be able to investigate?

Thank you

@broiniac
Copy link
Author

As we never had a default options for quantity buttons on archive pages, can you tell me how do you implement it so that we can replicate this on our end to be able to investigate?

It just strikes me - problem might be somewhere on our side, since it's not build-in function 😵‍💫

First of all - we use plugin Product Quantity for WooCommerce Pro by WPWhale.

But also there is this snippet (for rendering quantity buttons I presume):

<?php

/**
 * Override loop template and show quantities next to add to cart buttons
 */
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" name="add-to-cart" class="button alt add_to_cart_button ajax_add_to_cart" data-product_id="'.$product->get_id().'" data-quantity="1">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

And then we do this, to display and add some events to the buttons after some new products are added to the page:

  $('.infinite-scroll-wrap').on('append.infiniteScroll', function() {
    oceanwpWooQuantityButtons();
  });

After reexamination of this issue I think it is save to close without actual fix - there are some strong chances, that it is fault in our implementation. Sadly, I'm no longer in touch with author of this approach, so some questions might be unanswered forever.

I've created PR for this one assuming that more people might bump onto this.

@broiniac
Copy link
Author
broiniac commented Nov 3, 2021

After Theme update to 3.x there were some new issues. Gladly I've been able to fix/workaround them all.

First issue - php snippet given above? It was breaking InfiniteScrolling on WC archive pages. Pages were able to load only once giving "no more results" afterwards even, if there was plenty of undisplayed products.
"Add to cart" button is anchor now in OceanWP (button previously?) so all I had to do was rewrite snippet.

<?php

/**
 * Override loop template and show quantities next to add to cart buttons
 */
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<div class="cart">';
            $html .= woocommerce_quantity_input( array(), $product, false );
            $html .= '<a href="' . esc_url( $product->add_to_cart_url() ) . '" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="'.$product->get_id().'" data-quantity="1" rel="nofollow">' . esc_html( $product->add_to_cart_text() ) . '</a>';
        $html .= '</div>';
    }
    return $html;
}

That fixed InfiniteScrolling, but there was still problem with displaying quantity buttons after appending new page. To solve this I've to change second snippet as well by replacing oceanwpWooQuantityButtons(); with oceanwpWooCustomFeatures.quantityButtons.start();.

OceanWP is not using jQuery anymore, so I've tried to hook into InfiniteScroll with vanilla js, but it just didn't work, so alternatively I can use MutationObserver to look for attributes changes to trigger quantity buttons related code:

const observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    if (mutation.type === 'attributes' && mutation.target.style.display === 'none') {
      oceanwpWooCustomFeatures.quantityButtons.start();
    }
  });
});

observer.observe(document.querySelector('.infinite-scroll-request'), {
  attributes: true,
});

This is too "hacky" for my taste, so I'll just stay this time with jQuery version since it is and it will be required by most of the plugins anyway.

I've closed my PR as not applicable anymore. I think this issue can be closed too, although I think that introducing some js events to hook-up into OceanWP frontend logic is a nice idea.

@shervElmi shervElmi removed their assignment Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0