Skip to content

Instantly share code, notes, and snippets.

@woogists
Created April 29, 2022 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogists/e67d8e6c9d3bbf48dc266b8082798bda to your computer and use it in GitHub Desktop.
Save woogists/e67d8e6c9d3bbf48dc266b8082798bda to your computer and use it in GitHub Desktop.
[Conditional Shipping & Payments] Creating new conditions that include multiple modifiers
<?php
add_filter( 'woocommerce_csp_conditions', function( $condition_classes ) {
class WC_CSP_Condition_Package_Item_Quantity_Bottles extends WC_CSP_Condition_Package_Item_Quantity {
public function __construct() {
parent::__construct();
$this->id = 'bottles_in_package';
$this->title = __( 'Bottles Count', 'woocommerce-conditional-shipping-and-payments' );
}
}
$condition_classes[] = 'WC_CSP_Condition_Package_Item_Quantity_Bottles';
return $condition_classes;
} );
add_filter( 'woocommerce_csp_package_item_quantity_count', 'wc_sw_csp_bottles_items_count', 10, 4 );
function wc_sw_csp_bottles_items_count( $total_quantity, $package, $data, $args ) {
$total_quantity = 0;
if ( 'shipping_methods' === $args[ 'restriction_data' ][ 'restriction_id' ] && 'bottles_in_package' === $data[ 'condition_id' ] ) {
foreach ( $package[ 'contents' ] as $cart_item_key => $cart_item_data ) {
$product_id = $cart_item_data[ 'product_id' ];
$terms = get_the_terms( $product_id, 'product_cat' );
$terms_slugs = array();
foreach ( $terms as $term ) {
$terms_slugs[] = $term->slug;
}
if ( in_array( 'wine', $terms_slugs ) ) {
$total_quantity += $cart_item_data[ 'quantity' ];
}
}
}
return $total_quantity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment