• Resolved brainboxstudios

    (@brainboxstudios)


    Hey there,
    I’ve recently been trying to add the following code to the snippet plugin however it doesn’t seem to work. When I add this directly to the functions.php file it’s working fine – can you please advise on what is going wrong here.

    /*
    * Automatically add the deposit to the cart when a product in the category ‘pitch’ is added to the cart.
    */

    function aaptc_add_product_to_cart( $item_key, $product_id ) {
    $product_category_id = 79; // pitch category id
    $product_cats_ids = wc_get_product_term_ids( $product_id, ‘product_cat’ );
    if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) {
    $free_product_id = 1991; // Product id of the depoist which will get added to cart
    $found = false;
    //check if product already in cart
    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values[‘data’];
    if ( $_product->get_id() == $free_product_id )
    $found = true;

    }
    // if product not found, add it
    if ( ! $found )
    WC()->cart->add_to_cart( $free_product_id );
    } else {
    // if no products in cart, add it
    WC()->cart->add_to_cart( $free_product_id );
    }
    }
    }
    add_action( ‘woocommerce_add_to_cart’, ‘aaptc_add_product_to_cart’, 10, 2 );

    /*
    * Checking and validating when products are added to cart: max 2 products
    */

    add_filter( ‘woocommerce_add_to_cart_validation’, ‘only_two_items_allowed_add_to_cart’, 10, 3 );

    function only_two_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {

    $cart_items_count = WC()->cart->get_cart_contents_count();
    $total_count = $cart_items_count + $quantity;

    if( $cart_items_count >= 2 || $total_count > 2 ){
    // Set to false
    $passed = false;
    // Display a message
    wc_add_notice( __( “Sorry, you can only order one pitch at a time.”, “woocommerce” ), “error” );
    }
    return $passed;
    }

    Thanks
    Emma

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello.

    We found a bug in our plugin. We will fix it in the next update, but you can fix it yourself now. Replace the lines in the file
    wp-content\plugins\insert-php\includes\class.execute.snippet.php
    from
    35: add_action( 'wp', array( $this, 'executeEverywhereSnippets' ), 1 );
    to
    35: add_action( 'plugins_loaded', array( $this, 'executeEverywhereSnippets' ), 1 );

    Let us know if this solved the problem.

    Hi,

    The problem is fixed in the latest Woody 2.2.4. Please update.

    Thread Starter brainboxstudios

    (@brainboxstudios)

    Thanks for this – I’ve added the code back into the plugin and it’s all working fine now 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Snippets Not Working’ is closed to new replies.