Support » Plugin: WooCommerce » Validate woocommerce cart depending on shop archive page

  • Resolved giuls22

    (@giuls22)


    Is it possible to validate the woocommerce cart with woocommerce_add_to_cart_validation based on the unique shop archive page the user is currently on? I am adding to the cart via ajax.

    e.g.

    IF user is on Page strongshop AND max item count is 20…apply rule.
    IF user is on Page smallshop AND max item count is 10….apply rule.

    Below code works when the is_page constraints are removed:

    // remove the redirect
    add_filter( 'woocommerce_cart_redirect_after_error', '__return_false'); 
    add_action( 'woocommerce_add_to_cart_validation', 'restrict_only_one_item_in_cart' );
    
     function restrict_only_one_item_in_cart($cart_item_data) {
    
       global $woocommerce;
       $item_count = $woocommerce->cart->cart_contents_count;
    
       if((is_page('strongshop') || is_page('strongshopmobile')) && $item_count >= 20){
          return false;
       }
       return $cart_item_data;
    }
    • This topic was modified 1 year, 5 months ago by  giuls22.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support dougaitken

    (@dougaitken)

    Automattic Happiness Engineer

    Hey @giuls22

    Questions like this are normally more technical than normal in this support forum.

    Saying that I’m sure someone in the WooCommerce Slack Community would be able to help soundboard your idea (https://woocommerce.com/community-slack/).

    Let me know how you get on!

    Thanks,

    Hi @dougaitken

    I’ve tried posting there already, but not getting any responses to help….seems to be alot of conversations going on and looks like its hard to keep track of whats been asked/answered. šŸ™

    Any further suggestions?

    Thanks!

    Plugin Support RK

    (@riaanknoetze)

    This is a fairly complex development topic. Iā€™m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the Advanced WooCommerce group on Facebook (https://www.facebook.com/groups/advanced.woocommerce/) for more development-oriented questions.

    Plugin Support Jesse Pearson

    (@jessepearson)

    Automattic Happiness Engineer

    @giuls22 It looks like you are using woocommerce_add_to_cart_validation as an action when it is actually a filter. If this is being called via AJAX (which is when the filter is applied at one point) it will not work because the AJAX call is not on a page.

    To test this out, I would recommend adding this to your function:

    
    global $wp_query;
    error_log( print_r( $wp_query, true ) );
    

    Then review what is output in your debug.log, as long as debugging is on, and see if a page is listed in the query.

    @jessepearson Where can I view the output of the debug log? Is it via the developer functionality within the browser?

    Thanks.

    Plugin Support Jesse Pearson

    (@jessepearson)

    Automattic Happiness Engineer

    @giuls22 I recommend reviewing this whole page about debugging in WordPress:
    https://codex.wordpress.org/Debugging_in_WordPress

    There is an example on the page that can be copied and pasted, you can use everything in the example except the last part about script debugging.

    Once debugging and logging is turned on, then everything will be logged to debug.log in your /wp-content/ folder.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Validate woocommerce cart depending on shop archive page’ is closed to new replies.