null,
),
$atts,
'aafw_product_addons'
);
$product_id = intval( $atts['product_id'] );
if ( ! $product_id ) {
global $post;
$product_id = $post ? $post->ID : 0;
}
return $this->aafw_get_addons_html( $product_id );
}
/**
* Display addons on the product page.
*/
public function aafw_display_addons_on_product_page() {
global $post;
if ( ! $post ) {
return;
}
echo wp_kses_post( $this->aafw_get_addons_html( $post->ID ) );
}
/**
* Retrieve product addons HTML.
*
* @param int $product_id Product ID.
* @return string HTML content for addons.
*/
private function aafw_get_addons_html( $product_id ) {
$addons = get_post_meta( $product_id, '_product_addons', true );
if ( empty( $addons ) || ! is_array( $addons ) ) {
return '';
}
$addons = aafw_prepare_addons_for_display( $addons );
if ( empty( $addons ) ) {
return '';
}
return $this->aafw_render_addons_recursive( $addons );
}
/**
* Render addons recursively.
*
* @param array $addons Addons array.
* @return string HTML content for addons.
*/
private function aafw_render_addons_recursive( $addons ) {
ob_start();
$default_state = get_option( 'default_addon_state', 'expanded' );
$require_addon_selection = get_option( 'require_addon_selection', 'no' );
?>
aafw_render_addons_recursive( $addon['children'] ) ); ?>
get_id() ) ) {
return;
}
?>
get_price( 'edit' );
}
return $cart_item_data;
}
/**
* Display selected addons in cart.
*
* @param array $item_data Cart item data.
* @param array $cart_item Cart item.
* @return array Modified cart item data.
*/
public function aafw_woocommerce_get_item_data( $item_data, $cart_item ) {
if ( empty( $cart_item['selected_addons'] ) ) {
return $item_data;
}
foreach ( $cart_item['selected_addons'] as $addon ) {
$item_data[] = array(
'name' => isset( $addon['title'] ) ? $addon['title'] : '',
'value' => implode( ', ', array_column( $addon['subItems'], 'value' ) ),
);
}
return $item_data;
}
/**
* Persist selected addons on order line items (HPOS-compatible).
*
* @param WC_Order_Item_Product $item Order line item.
* @param string $cart_item_key Cart item key.
* @param array $values Cart item values.
* @param WC_Order $order Order.
*/
public function aafw_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if ( ! empty( $values['selected_addons'] ) ) {
$item->add_meta_data( 'selected_addons', $values['selected_addons'], true );
}
}
/**
* Display selected addons in order meta on the admin side.
*
* @param int $item_id Order item ID.
* @param WC_Order_Item_Product $item Order item.
* @param WC_Product|null $product Product object.
*/
public function aafw_display_selected_addons_in_order_meta( $item_id, $item, $product ) {
$selected_addons = $item->get_meta( 'selected_addons', true );
if ( empty( $selected_addons ) || ! is_array( $selected_addons ) ) {
return;
}
echo '';
}
/**
* Add custom price based on selected addons (from stored base price).
*
* @param WC_Cart $cart Cart object.
*/
public function aafw_add_custom_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
return;
}
$enable_tax = get_option( 'enable_addon_tax', 'no' );
$tax_class = get_option( 'addon_tax_class', '' );
foreach ( $cart->get_cart() as $cart_item ) {
if ( empty( $cart_item['selected_addons'] ) ) {
continue;
}
$base_price = isset( $cart_item['aafw_base_price'] )
? (float) $cart_item['aafw_base_price']
: (float) $cart_item['data']->get_price( 'edit' );
$addon_total = aafw_calculate_addon_total( $cart_item['selected_addons'] );
if ( 'yes' === $enable_tax && $addon_total > 0 ) {
$tax_rates = WC_Tax::get_rates( $tax_class );
$taxes = WC_Tax::calc_tax( $addon_total, $tax_rates, true );
$addon_total += array_sum( $taxes );
}
$cart_item['data']->set_price( $base_price + $addon_total );
}
}
/**
* Validate addons selection before adding to cart.
*
* @param bool $passed Whether the product can be added to the cart.
* @param int $product_id The product ID.
* @param int $quantity The quantity being added to the cart.
* @return bool
*/
public function aafw_validate_addons_selection( $passed, $product_id, $quantity ) {
if ( ! $passed || ! aafw_product_has_addons( $product_id ) ) {
return $passed;
}
$require_addon_selection = get_option( 'require_addon_selection', 'no' );
$selected_raw = isset( $_POST['selected_addons'] ) ? wp_unslash( $_POST['selected_addons'] ) : '';
if ( 'yes' === $require_addon_selection ) {
if ( ! isset( $_POST['woocommerce_addon_nonce'] ) || ! wp_verify_nonce( wp_unslash( sanitize_key( $_POST['woocommerce_addon_nonce'] ) ), 'woocommerce_addon_nonce' ) ) {
wc_add_notice( __( 'Security check failed. Please refresh the page and try again.', 'advanced-addons-for-woocommerce' ), 'error' );
return false;
}
$selected_addons = aafw_parse_selected_addons( $selected_raw );
if ( null === $selected_addons || empty( $selected_addons ) ) {
wc_add_notice( __( 'Please select an addon before adding this product to your cart.', 'advanced-addons-for-woocommerce' ), 'error' );
return false;
}
return $passed;
}
if ( '' !== $selected_raw && '[]' !== $selected_raw ) {
if ( ! isset( $_POST['woocommerce_addon_nonce'] ) || ! wp_verify_nonce( wp_unslash( sanitize_key( $_POST['woocommerce_addon_nonce'] ) ), 'woocommerce_addon_nonce' ) ) {
wc_add_notice( __( 'Security check failed. Please refresh the page and try again.', 'advanced-addons-for-woocommerce' ), 'error' );
return false;
}
if ( null === aafw_parse_selected_addons( $selected_raw ) ) {
wc_add_notice( __( 'Invalid addon selection. Please try again.', 'advanced-addons-for-woocommerce' ), 'error' );
return false;
}
}
return $passed;
}
}