Skip to content

Instantly share code, notes, and snippets.

@acanza
Last active August 29, 2015 14:25
Show Gist options
  • Save acanza/17a891d131d09c55a9f0 to your computer and use it in GitHub Desktop.
Save acanza/17a891d131d09c55a9f0 to your computer and use it in GitHub Desktop.
Aplicar gastos de envío gratuito a nivel de producto o categoría de producto
/* Only one single product may be in the cart */
add_action( 'wafs_match_condition_single_product', 'wafs_match_condition_single_product', 10, 3 );
/* Match single category
*
* @param bool $match
* @param string $operator
* @param mixed $value
* @return bool
*/
function wafs_match_condition_single_product( $match, $operator, $value ) {
global $woocommerce;
$match = true;
if ( ! isset( $woocommerce->cart ) || empty( $woocommerce->cart->cart_contents ) ) return;
if ( '==' == $operator ) :
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( $product['product_id'] != $value ) :
$match = false;
endif;
endforeach;
elseif ( '!=' == $operator ) :
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( $product['product_id'] == $value ) :
$match = false;
endif;
endforeach;
endif;
return $match;
}
add_filter( 'wafs_conditions', 'wafs_conditions_add_single_product', 10, 1 );
function wafs_conditions_add_single_product( $conditions ) {
$conditions['Product']['single_product'] = 'Only product in cart';
return $conditions;
}
add_filter( 'wafs_values', 'wafs_values_add_single_product', 10, 2 );
function wafs_values_add_single_product( $values, $condition ) {
switch ( $condition ) {
case 'single_product':
$values['field'] = 'select';
$products = get_posts( array( 'posts_per_page' => '-1', 'post_type' => 'product', 'order' => 'asc', 'orderby' => 'title' ) );
foreach ( $products as $product ) :
$values['options'][$product->ID ] = $product->post_title;
endforeach;
break;
}
return $values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment