BJ Bowen
BJ Bowen

Reputation: 1

If product variation contains this phrase, then do this

Edited: Problem: Force Sells plugin quantity needs to vary based on the quantity of the item added to the cart OR it needs to vary based on the selected variation (pa_size).

In other words, we are trying to force sell Ice Packs. If the customer buys less than 4(quantity) products, we want to force sell 1 ice pack. If they buy 8 (quantity) products, we want to force sell 2 ice packs. AND, if they purchase 1 (quantity) of a 6 Pack, we want to force sell 2 ice packs. AND, if they purchase 1 (quantity) of a 12 Pack, we want to force sell 3 ice packs. For everything else, force sell 1 Ice Pack.

I would really love to learn how to pull this off.

I need the variable $quantitybj to be the result because it is being passed by more code further down.

The output I get now is $quantitybj = 3 -- regardless. The output I expect is that $quantitybj varies depending on quantities and selected product variation (ie. a 6 pack of cookies as opposed to one package of cookies).

I have commented out the errors in the code below.

$product_size should be a string and then I am trying to see if that string contains the words "6 pack" or "12 pack". How do I get the selected variation to make that comparison?

This is the full function from the woocommerce-force-sells.php. The code i added is commented out with BJ -- just so you know what I've added.

    echo '<pre><code>', print_r($product->get_available_variations()), '</code></pre>'; exit();

                        public function add_force_sell_items_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
        // Check if this product is forced in itself, so it can't force in others (to prevent adding in loops)
        if ( isset( WC()->cart->cart_contents[ $cart_item_key ]['forced_by'] ) ) {
            $forced_by_key = WC()->cart->cart_contents[ $cart_item_key ]['forced_by'];

            if ( isset( WC()->cart->cart_contents[ $forced_by_key ] ) ) {
                return;
            }
        }

        $product = wc_get_product( $product_id );

        $force_sell_ids = array_filter( $this->get_force_sell_ids( $product_id, array( 'normal', 'synced' ) ), array( $this, 'force_sell_is_valid' ) );
        $synced_ids     = array_filter( $this->get_force_sell_ids( $product_id, array( 'synced' ) ), array( $this, 'force_sell_is_valid' ) );



        // BJ - CHANGE THE QUANTITY OF ICE PACKS TO EXACTLY HOW MANY IS NEEDED DEPENDING ON THE ORDER //



        //This part is working
        $quantitybj = '1';

        if ($quantity > '4') {
            $quantitybj = '2';
        }
        if ($quantity > '8') {
            $quantitybj = '3';
        }

        // this part is not working. I am attempting to say "if the selected variation CONTAINS the string "6 Pack", then $quantitybj = something new. 

        // FIRST PROBLEM: I get an error "Warning: strpos() expects parameter 1 to be string, array given in /home/mfpnuts/public_html/novo/wp-content/plugins/woocommerce-force-sells/woocommerce-force-sells.php on line 

        //SECOND PROBLEM: $quantitybj passes as "3" regardless of the option chosen.


        $product_size = $product->get_available_variations();

        if(strpos( $product_size, '6 Pack') !== FALSE ){
            $quantitybj = '2';
        }
        if (strpos( $product_size, '12 Pack') !== FALSE ){
            $quantitybj = '3';
        }


        // END BJ EDITS FOR QUANITY -- more edits on lines 390 & 400


        if ( ! empty( $force_sell_ids ) ) {
            foreach ( $force_sell_ids as $id ) {
                $cart_id = WC()->cart->generate_cart_id( $id, '', '', array( 'forced_by' => $cart_item_key ) );
                $key = WC()->cart->find_product_in_cart( $cart_id );

                if ( ! empty( $key ) ) {
                    WC()->cart->set_quantity( $key, WC()->cart->cart_contents[ $key ][$quantitybj ] );
                } else {
                    $args = array();

                    if ( $synced_ids ) {
                        if ( in_array( $id, $synced_ids ) ) {
                            $args['forced_by'] = $cart_item_key;
                        }
                    }

                    // BJ changed "$quantity" to "$quantitybj"

                    $params = apply_filters( 'wc_force_sell_add_to_cart_product', array( 'id' => $id, 'quantity' => $quantitybj, 'variation_id' => '', 'variation' => '' ), WC()->cart->cart_contents[ $cart_item_key ] );
                    $result = WC()->cart->add_to_cart( $params['id'], $params['quantity'], $params['variation_id'], $params['variation'], $args );

                    // If the forced sell product was not able to be added, don't add the main product either. "Can be filtered"
                    if ( empty( $result ) && apply_filters( 'wc_force_sell_disallow_no_stock', true ) ) {
                        WC()->cart->remove_cart_item( $cart_item_key );
                        throw new Exception( sprintf( __( '%s will also be removed as they\'re sold together.', 'woocommerce-force-sells' ), $product->get_title() ) );
                    }
                }
            }
        }
    }

Upvotes: 0

Views: 231

Answers (1)

David
David

Reputation: 6111

Essentially you need a few conditional statements, one to check if the quantity is 1, another to check if the quantity is 4 or more, another to check if the quantity is 8 or more, and another to check the size of the packs.

Take a look at this example, I've commented the code heavily in the hopes that you can easily follow it:

<?php

// Declare the sizes of the packs, get a random number between 1-8, and then a random pack size
$sizes = array("6 pack", "12 pack", "18 pack", "24 pack", "30 pack");
$quantity = rand(1, 8);
$product_size = $sizes[rand(0, count($sizes) - 1)];

// Print the quantity and pack size
echo $quantity, ' cases of ', $product_size, 's';

// The default number of ice packs is 1
$ice_packs = 1;
if ($quantity === 1) {
    // if the quantity is 1 and it is a 6 pack, then add an aditional ice pack
    // if the quantity is 1 and it is a 12 pack, then add one more addtional ice pack
    if ($product_size === "6 pack") {
        $ice_packs++;
    } elseif ($product_size === "12 pack") {
        $ice_packs += 2;
    }
} else {
    // if the quantity is 4 or more then add an additional ice pack
    // if the quantity is 8 or more then add one more additional ice pack
    if ($quantity > 3) {$ice_packs++;}
    if ($quantity > 7) {$ice_packs++;}
}

echo '<br />Ice Packs: ', $ice_packs;

Fiddle: Live Demo

Upvotes: 2

Related Questions