Initial commit
This commit is contained in:
commit
cdb81a2853
159
les-variations-js-free.php
Executable file
159
les-variations-js-free.php
Executable file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce Variations Add to Cart JS-free
|
||||
* Determines the variation ID for the user's chosen attributes on variable items server-side, so that users can shop without Javascript enabled.
|
||||
* @author Jacob Eva, Liberated Embedded Systems, liberatedsystems.co.uk
|
||||
* @version 1
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Plugin Name: LES Variations Add-to-Cart JS-free
|
||||
Plugin URI: https://git.liberatedsystems.co.uk
|
||||
Description: This plugin determines the variation ID for a product from the server side, allowing users to shop without Javascript enabled. Inspiration taken from eggplant studios' variations add to cart plugin (www.eggplantstudios.ca).
|
||||
Version: 1.0.0
|
||||
Author: Jacob Eva
|
||||
License: GPLv2 or later
|
||||
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/*
|
||||
* Get variation ID function, uses an array of the attributes to construct an
|
||||
* SQL query that can determine the correct variation ID to return, so that the
|
||||
* product can be added to the user's cart
|
||||
*/
|
||||
function get_variation_id( $attributes, $product_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$select_from_sql = "
|
||||
SELECT p.ID
|
||||
FROM {$wpdb->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 <a href="%1$s" title="%2$s">%2$s</a>.', '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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user