Skip to content
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

Cannot use orderby in shortcode, this plugin overrides it #17

Closed
n9yty opened this issue Sep 26, 2018 · 12 comments
Closed

Cannot use orderby in shortcode, this plugin overrides it #17

n9yty opened this issue Sep 26, 2018 · 12 comments
Labels

Comments

@n9yty
Copy link

n9yty commented Sep 26, 2018

I posted here:

https://wordpress.org/support/topic/product-shortcode-in-product-not-respecting-orderby/

But basically, it seems as though this plugin, in the attempt to order everything by the chosen order, is also overriding shortcodes.

In particular we are trying to use the [products] shortcode with a custom orderby, and with this plugin active it is overriding it and instead forcing the default sort to be used regardless of what we specify in the shortcode.

I had suggested a change in that forum post, but I don't know if it would be the right way to handle it or have other side-effects in how the plugin is supposed to operate.

@bekarice
Copy link
Contributor

bekarice commented Oct 2, 2018

hey @n9yty thanks for the report here! can you please let me know what version of the plugin you're using? Here's what I see using WooCommerce 3.4.5 + WooCommerce Extra Product Sorting Options 2.7.2:

  1. Use the products shortcode and orderby rating:
    Input: [products limit=3 orderby="rating"]
    Result: I see items sorted properly by rating:
    screen shot 2018-10-02 at 1 38 04 am

  2. Input: [products limit=3 orderby="popularity"]
    Result: I see items properly sorted by popularity / sales count:
    screen shot 2018-10-02 at 1 38 24 am

  3. I go to the Shop page and can confirm my default sorting (menu order) is preserved.

I know you'd noted this code section in the forums, but if you'll look a bit further, you'll see that it also has a safety check for the shortcode arguments to respect them.

On WooCommerce pages, we have to assume we'll be using the $_GET value because these pages will output the sorting dropdown, but on other pages, we should try to use the arguments from the shortcode, and then finally use the default as a fallback.

Could you please clarify what you're seeing here, or where you're using this shortcode so I can try to replicate this issue? thanks!

@n9yty
Copy link
Author

n9yty commented Oct 2, 2018

Very strange indeed. We are current on plugins, WooCommerce Extra Product Sorting Options Version 2.7.2, WooCommerce Version 3.4.5 also.

I just made a brand new prodcut, changing nothing in the default settings (although maybe that doesn't mean all our defaults are your defaults), and simply inserted this into the description:

[products ids="32424, 33041, 35662, 2645, 4842, 7524" orderby="menu_order"]

They are sorting alphabetically, not by menu_order.

image

(using preview to view product, URL looks like: '/?post_type=product&p=1203845&preview=true')

If I re-implement my patch, they sort by menu_order.

image

(url looks the same '/?post_type=product&p=1203845&preview=true')

So, nothing is being passed as a param in the URL. If I put that same shortcode in a Page, it works consistently, sorted by menu_order, regardless of whether my patch is in place or not, it only seems to be affected if it is on a product page.

Let me know how I can help further debug this.

@n9yty
Copy link
Author

n9yty commented Oct 2, 2018

I also tested various other sort parameters. For example, if I used 'popularity' as you did, limiting it to three, they did sort non-alphabetically. However, if I then put order="DESC" it did not change the sort order.

I didn't try all possible options, but 'date', 'id', 'title' don't work, I don't think we have ratings so that doesn't work. NOthing seems to respect the 'order' ASC/DESC parameter.

@n9yty
Copy link
Author

n9yty commented Oct 2, 2018

Sorry for the noise, but I just read more carefully what you wrote:

"On WooCommerce pages, we have to assume we'll be using the $_GET value because these pages will output the sorting dropdown,"

Our use case is such that we have a WooCommerce product page using a product shortcode, it does not present a sorting drop-down, so $_GET is not present. The code, as I understand it, in that case just uses the default which is why I had to override it to look for a shortcode defined value.

I'm not saying my patch is the right fix, but I guess you have not expected the use of a [products] shortcode to list other products with a custom sort order on a WooCommerce product page?

I don't know what ou would do in the case of having a dropdown sort menu on a page that also specified a hard-coded sort option in the shortcode. I don't know fi that is a real-world scenario or not.

@bekarice
Copy link
Contributor

bekarice commented Oct 3, 2018

@n9yty thanks for the clarification here! I've tested a product page as well, and unfortunately I'm still not able to replicate any of these issues. Here's my results (outputting product details for reference):

  1. [products ids="53,70,31,56" orderby="menu_order" columns="4" order="ASC"]

    ✅ Correct
    ✅ Works with order="DESC" as well
    menu-order

  2. [products ids="53,70,31,56" orderby="title" columns="4" order="ASC"]

    ✅ Correct
    ✅ Works with order="DESC" as well
    title

  3. [products ids="53,70,31,56" orderby="date" columns="4" order="ASC"]

    ✅ Correct
    ✅ Works with order="DESC" as well
    date

  4. [products ids="53,70,31,56" orderby="rating" columns="4"]

    ✅ Correct
    ✅ Does not work with DESC because WooCommerce core doesn't accept order for rating orderby
    rating

  5. [products ids="53,70,31,56" orderby="popularity" columns="4"]

    ✅ Correct
    ✅ Does not work with DESC because WooCommerce core doesn't accept order for popularity orderby
    popularity

Could you please install the Transients Manager plugin and use it to delete all transients on your site, ensure all other plugins are deactivated except WooCommerce + Extra Product Sorting Options, then try again? Your site may be returning cached results for these shortcodes, which clearing transients should reveal.

@n9yty
Copy link
Author

n9yty commented Oct 3, 2018

Hi Beka, thanks for digging into this, by the way.

At this point, I don't know what to say, I cannot make it go away and you cannot duplciate it....

I installed Transients Manager and deleted all transients
I switched the theme to the Storefront theme to remove conflicts there
I flushed browser cache, am not running any other caching that I know of
I disabled ALL plugins except WooCommerce and WooCommerce Extra Sorting Options

Problem persists, products still are sorting alphabetically instead of by menu_order with that shortcode.

I deleted the Extra Sorting plugin and re-installed it, no change. Here is the settings column:

image

If you have any thoughts on how I might help provide more debugging information, let me know. I will keep working on it. Also, if you can help me see the section where you are respecting the parameter, it might help me to debug it a bit more. I'll try below, but if I am missing something please let me know.

As I see it, you are applying add_new_shop_ordering_args() to the filter woocommerce_get_catalog_ordering_args. This will filter any product request, as I understand it.

In add_new_shop_ordering_args() it looks to see if $_GET['orderby'] is set. I believe this is where you are expecing it should be set for woocommerce product pages, shortcode or otherwise. In our case, looking at headers in the broswer debugger view, this is not set for the request. The only query string parameters are p, post_type, and preview {as I am looking at a preview of the product, it isn't published). But this is probably for the outer container product, not the shortcode.

So inside add_new_shop_ordering_args() I inserted a debug statement at the top to capture a few things:

 $get_contents = print_r( $_GET, true );
 $sort_args_contents = print_r( $sort_args, true );
 echo "DEBUG GET cotents: " . $get_contents;
 echo "DEBUG sort_args cotents: " . $sort_args_contents;

In the page output, I see this output twice, once after the page title and once after the product description header, right before the shortcode contents are displayed.

DEBUG GET contents: Array ( [post_type] => product [p] => 1203845 [preview] => true )
DEBUG sort_args contents: Array ( [orderby] => menu_order title [order] => ASC [meta_key] => )

If $_GET['orderby'] is not defined, the code after the test would apply the standard sorting. The next code section that specifically looks for the $sort_args['orderby'] does not apply here becuase is_woocommerce() is true as this is a product page.

So it seems that in your case/testing, the $_GET['orderby'] is set when the shortcode fires, and for some reason it is NOT set here. The question is WHY is it different?

Any thoughts on where to go from here?

Small note, that if I change the URL to manually include an orderby parameter, the sort order works:

https://testsite/store/test-sort-order/?orderby=menu_order

And, maybe not helpful, but the filter "woocommerce_shortcode_products_query" passes these to me:

$query_args:
Array ( [post_type] => product [post_status] => publish [ignore_sticky_posts] => 1 [no_found_rows] => 1 [orderby] => title [order] => asc [posts_per_page] => -1 [meta_query] => Array ( ) [tax_query] => Array ( [0] => Array ( [taxonomy] => product_visibility [field] => term_taxonomy_id [terms] => Array ( [0] => 11998 ) [operator] => NOT IN ) ) [post__in] => Array ( [0] => 32424 [1] => 33041 [2] => 35662 [3] => 2645 [4] => 4842 [5] => 7524 ) )

$atts:
Array ( [limit] => -1 [columns] => 3 [rows] => [orderby] => menu_order [order] => ASC [ids] => 32424, 33041, 35662, 2645, 4842, 7524 [skus] => [category] => [cat_operator] => IN [attribute] => [terms] => [terms_operator] => IN [tag] => [visibility] => visible [class] => [page] => 1 [paginate] => [cache] => 1 )

$loop_name:
products

@n9yty
Copy link
Author

n9yty commented Oct 18, 2018

@bekarice Maybe you have given up on me since it seems something strange is going on. :) Is there any more I can do to help debug this?

@bekarice
Copy link
Contributor

@n9yty nope, just been out of office for most of the past 2 weeks. :) the additional information here helped! I was able to replicate this after some settings adjustments. Could you apply this change and let me know if it resolves the issue for you? thanks!

@n9yty
Copy link
Author

n9yty commented Oct 22, 2018

Thanks, @bekarice, that takes care of things here for us! And thanks for your awesome WooSesh presentation.

@bekarice
Copy link
Contributor

excellent, we'll get this included in the next release then, thanks for hanging with me here @n9yty 😸 and glad to hear you were able to attend WooSesh! Brian and Patrick did a wonderful job, I appreciate you tuning in! 📺 🍻

@unfulvio unfulvio added the bug label Oct 24, 2018
@unfulvio
Copy link
Member

addressed by #18 -- closing this, pending deploy

thanks!

bekarice added a commit that referenced this issue Oct 30, 2018
@EdithAllison
Copy link

Just to say thank you! The official Woo documentation references the incorrect code which results in shortcodes not working, your fix works perfectly, thank you for publishing on github!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants