mindkomm / timber-integration-woocommerce

WooCommerce integration for Timber
MIT License
109 stars 20 forks source link

Ajax request #22

Closed ghost closed 3 years ago

ghost commented 3 years ago
add_action('wp_ajax_mnn_sign_up', array($this, 'update_cart_total'));
add_action('wp_ajax_nopriv_mnn_sign_up', array($this, 'update_cart_total'));

public function update_cart_total() {
    $context          = Timber::context();
    Timber::render( 'woocommerce/cart/cart-totals.twig', $context );
}

where twig is this {% do wc_action('woocommerce_before_cart_totals') %}.

I get an error "CRITICAL Uncaught Twig\Error\SyntaxError: Unknown "wc_action" function. Did you mean "action"?". What's wrong?

gchtr commented 3 years ago

Hey @grenders

Do you have the most current stable release of the package installed, which is version 0.5.3.1? The wc_action function was introduced in version 0.5.3: https://github.com/mindkomm/timber-integration-woocommerce/releases/tag/0.5.3.

ghost commented 3 years ago

Hey @grenders

Do you have the most current stable release of the package installed, which is version 0.5.3.1? The wc_action function was introduced in version 0.5.3: https://github.com/mindkomm/timber-integration-woocommerce/releases/tag/0.5.3.

Yes, the last one. This function works correctly when I display the woocommerce cart. However, when I try to display the same Ajax cart with a request, I get this error. I can't figure out what's wrong. Why is this happening.

There is an assumption that the Ajax request is isolated. However, without calling this function, the twig returns a valid one.

ghost commented 3 years ago

No idea why this is happening?

ghost commented 3 years ago

I found the problem. Finally. Your template in the WooCommerce class uses an admin check. However, very often when writing topics, admin ajax is used because it is convenient. Is this verification really necessary?

ghost commented 3 years ago

Sorry for my persistence. Will this check be fixed? Or should I still change the approach to the formation of ajax request?

gchtr commented 3 years ago

I found the problem. Finally. Your template in the WooCommerce class uses an admin check.

Thanks for taking the time to find what’s causing the issue!

However, very often when writing topics, admin ajax is used because it is convenient. Is this verification really necessary?

I’m not against using admin ajax at all. If I remember correctly, the reason I added the is_admin() bailout is because I wanted to prevent any unwanted side effects and not call functionality on every admin page load that is not necessary. But I have to admit I didn’t think of the admin ajax use case. And when I look at the code that is called, there shouldn’t be any side effects in the backend, because I don’t change any queries or the like.

Will this check be fixed?

Yes, I’ll definitely fix this. Probably next week, together with a proper release of the newest release candidate.

ghost commented 3 years ago

Can I install the release candidate version through the composer?

gchtr commented 3 years ago

Can I install the release candidate version through the composer?

Yes, you could install the pre-release by updating the version constraint in your composer.json file or with the following command:

composer require mindkomm/timber-integration-woocommerce:0.6.0-rc.1

But that shouldn’t be necessary anymore.

I fixed this issue in https://github.com/mindkomm/timber-integration-woocommerce/commit/9a7f37a070d7fcef0418f69b6a54dc25d927db2b and released it with the 0.6.0 release. You can update the integration with

composer update mindkomm/timber-integration-woocommerce