This is my first post here so I apologize if I do not do it correctly.
I am using Woocommerce Advanced Shipping plugin on a winery website to programmatically offer penny (0.01USD) shipping for orders with 6 or more bottles (as it is efficient to ship in multiples of 6). However, this client offers products that include 2 or 3 bottles. The quantity condition does not work for me because a three-bottle product counts as a single product when calculated for quantity.
I have begun trying to write a custom condition function as the guide here explains: http://docs.shopplugins.com/article/41-developer-adding-a-custom-condition
I feel like I am getting close, but it still does not work. Here is my code:
/** Woocommerce Advanced Shipping (WAS) **/
function check_total() {
// Only run in the Cart or Checkout pages
if ( is_cart() || is_checkout() ) {
global $woocommerce, $product;
$total_bottles = 0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) {
// Add all bottles, 'bottles' is custom attribute set on the product page
$total_bottles += $product['bottles'];
}
}
return $total_bottles;
}
/**
* Add condition to conditions list.
*
* @param array $conditions List of existing conditions.
* @return aray List of modified conditions.
*/
function was_conditions_add_bottles( $conditions ) {
// 'General', 'User Details', 'Cart' are default groups, you can also use something custom
$conditions['General']['bottlenum'] = __( 'Bottles', 'woocommerce-advanced-shipping' );
return $conditions;
}
add_filter( 'was_conditions', 'was_conditions_add_bottles', 10, 1 );
/**
* Add value field for 'bottles' condition
*
* @param array $values List of value field arguments
* @param string $condition Name of the condition.
* @return array $values List of modified value field arguments.
*/
function was_values_add_bottles( $values, $condition ) {
switch ( $condition ) {
case 'bottlenum':
$values['field'] = 'text';
$values['placeholder'] = 'ie. 3';
// Option 2; Drop down value field
//
// $values['field'] = 'select';
//
// foreach ( array( '1', '2' ) as $key => $value ) :
// $values['options'][ $key ] = $value;
// endforeach;
break;
}
return $values;
}
add_filter( 'was_values', 'was_values_add_bottles', 10, 2 );
/**
* Must match quantity of bottles.
*
* @param bool $match Current matching status. Default false.
* @param string $operator Store owner selected operator.
* @param mixed $value Store owner given value.
* @param array $package Shipping package.
* @return bool If the current user/environment matches this condition.
*/
function was_match_condition_bottles( $match, $operator, $value, $package ) {
// Set total quantity of bottles in cart
$total_bottles = check_total();
// Check if value exists
if ( $value ) :
if ( $operator == '==' ) :
$match = ( $total_bottles == $value );
elseif ( $operator == '!=' ) :
$match = ( $total_bottles != $value );
elseif ( $operator == '>=' ) :
$match = ( $total_bottles >= $value );
elseif ( $operator == '<=' ) :
$match = ( $total_bottles <= $value );
endif;
endif;
return $match;
}
add_action( 'was_match_condition_bottles', 'was_match_condition_bottles', 10, 4 );
I am having trouble getting the ‘bottles’ custom attribute from the products in cart. I feel like everything else would work once I get that. There isn’t much documentation for this plugin but this is a pretty universal Woocommerce feature, I think. However, I was unable to find the documentation I needed from Woocommerce to go over getting custom attributes from products in the cart.
Thanks for any help that can be offered.