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

replace get_post_status( $product_id ) with $product->get_status() in WC_AJAX::add_to_cart() #321

Closed
magenta-cuda opened this issue Jun 18, 2021 · 0 comments

Comments

@magenta-cuda
Copy link

https://github.com/ClassicPress-plugins/classic-commerce/blob/10650f8347d4307b8b02ccdc76195e544cb50e51/includes/class-wc-ajax.php#L381

I think this code can be more efficiently written as:

$product_status = $product->get_status();

get_post_status( $product_id ) will force ClassicCommerce to find the WP_Post object in the cache whereas $product->get_status() just dereferences an existing object.

This is not a quibble for me as the products of my plugin are virtual (i.e., they do not exists in the database) and the WP_Post objects are not naturally created. Of course, I can artificially create them but this really should not be necessary. When a ClassicCommerce WC_Product object is constructed a WP_Post object is also constructed and the fields in the WP_Post object are copied to fields in the WC_Product object so the WP_Post object is really not needed anymore. Of course the WP_Post object continues to live in the cache so using it is not very expensive. However, in my case it does not naturally exists in the cache. Currently, I artificially create it and insert into the cache but I would rather remove that code.

Moreover, as I commented in an earlier issue I think $product_status is misused and the behavior of WC_AJAX::add_to_cart() is incorrect. However, I can understand the reluctance to change the behavior of code that has seemingly worked correctly for several years. The change I propose here should not change the behavior of WC_AJAX::add_to_cart() except for running slightly faster but will allow my plugin to work better.

@magenta-cuda magenta-cuda mentioned this issue Jun 19, 2021
7 tasks
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.

1 participant