commit cdb81a28532c499b3611c95a6f21fd499ddbe52b Author: jacob.eva Date: Thu May 18 12:26:20 2023 +0100 Initial commit diff --git a/les-variations-js-free.php b/les-variations-js-free.php new file mode 100755 index 0000000..fb46463 --- /dev/null +++ b/les-variations-js-free.php @@ -0,0 +1,159 @@ +prefix}posts as p + "; + + $join_sql = " + JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id + "; + + $where_sql = " + WHERE pm.meta_key = %s + AND pm.meta_value LIKE %s + "; + + $parent_sql = " + AND p.post_parent = %s + "; + + // add additional attributes to JOIN if there are more than 1 + for ( $x = 2; $x < count( array_keys($attributes) ) + 1; $x++ ) { + $join_sql = $join_sql . " + JOIN {$wpdb->prefix}postmeta as pm{$x} ON p.ID = pm{$x}.post_id + "; + } + + // add additional attributes to AND section if there are more than 1 + for ( $x = 2; $x < count( array_keys($attributes) ) + 1; $x++ ) { + $where_sql = $where_sql . " + AND pm{$x}.meta_key = %s + AND pm{$x}.meta_value LIKE %s + "; + } + + // merge attribute arrays into one for use in prepared statement + $parameters = array(); + + for ( $x = 0; $x < count ( array_keys($attributes) ); $x++ ) { + $parameters[] = array_keys($attributes)[$x]; + $parameters[] = array_values($attributes)[$x]; + } + $parameters[] = $product_id; + + $sql = $select_from_sql . $join_sql . $where_sql . $parent_sql; + + return $wpdb->get_var($wpdb->prepare($sql, $parameters)); +} + + +add_filter( 'woocommerce_add_to_cart_handler', 'les_add_to_cart_handler'); +/* + * Override function to change type of variable product, so custom function is + * called to handle it +*/ +function les_add_to_cart_handler( $type ) { + if ( $type === "variable" || $type === "variation" ) { + return "les_variation"; + } + else { + return $type; + } +} + +add_action( 'woocommerce_add_to_cart_handler_les_variation', 'les_add_variation_to_cart', 10, 2 ); +/* + * Add to cart function, created to allow shopping without JS +*/ +function les_add_variation_to_cart() { + global $woocommerce; + $added_products = 0; + + $product_id = (int) $_POST['product_id']; + + $quantity = (int) $_POST['quantity']; + + $product = wc_get_product( $product_id ); + + // do nothing if attributes were not sent + $attributes_valid = false; + + // if the user has JS enabled and / or has already set the variation ID + // themselves + if ( ! empty( $_POST['variation_id'] ) ) { + $variation_id = $_POST['variation_id']; + } + + else { + // create array with all attribute keys + $attributes = array(); + $attr_exp = "/attribute.+/"; + foreach (array_keys($_POST) as $field) + { + if ( preg_match($attr_exp, $field) ) { + $attributes[$field] = $_POST[$field]; + } + } + + + // if there are attributes, process them to get the variation id + if ( ! empty( $attributes ) ) { + $variation_id = get_variation_id( $attributes, $product_id ); + } + } + + $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $attributes ); + + if ( ! $passed_validation ) { + return false; + } + + if( (int) $_POST['quantity'] > 0 ) { + // if no variation id was found for product + if ( empty( $variation_id ) ) { + /* translators: 1: product link, 2: product name */ + wc_add_notice( sprintf( __( 'Please choose product options by visiting %2$s.', 'woocommerce' ), esc_url( get_permalink( $product_id ) ), esc_html( $product->get_name() ) ), 'error' ); + return false; + } + else { + $added_to_cart = $woocommerce->cart->add_to_cart( + $_POST['product_id'], // string $product_id + $_POST['quantity'], // string $quantity = 1 + (int) $variation_id, // integer $variation_id = '' + false // no attributes + ); + $added_products ++; + + if ( $added_to_cart ) { + wp_safe_redirect( wc_get_cart_url() ); + } + return $added_to_cart; + } + } +}