Created
January 2, 2025 22:21
-
-
Save hmowais/673c182d0172498323f5c8215f49eb12 to your computer and use it in GitHub Desktop.
Add Envelope Addon Option to Woocommerce Product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adding Checkbox and Price Field in Product Admin Panel | |
*/ | |
function custom_add_envelope_checkbox_and_price() { | |
echo '<div class="custom-envelop-checkbox">'; | |
// Envelope activation checkbox | |
woocommerce_wp_checkbox( array( | |
'id' => 'custom_envelope_active', | |
'label' => __('Activate Envelope', 'woocommerce'), | |
'description' => __('Check to activate envelope.', 'woocommerce'), | |
)); | |
// Custom price input field for Envelope Price | |
woocommerce_wp_text_input( array( | |
'id' => 'custom_envelope_price', | |
'label' => __('Envelope Price', 'woocommerce'), | |
'description' => __('Set the price for the envelope (leave empty for default €50).', 'woocommerce'), | |
'type' => 'number', | |
'desc_tip' => true, | |
'custom_attributes' => array( 'step' => '0.01', 'min' => '0' ) | |
)); | |
echo '</div>'; | |
} | |
add_action( 'woocommerce_product_options_general_product_data', 'custom_add_envelope_checkbox_and_price' ); | |
/** | |
* Saving Envelope Checkbox and Price to Product Meta | |
*/ | |
function custom_save_envelope_checkbox_and_price( $post_id ) { | |
$product = wc_get_product( $post_id ); | |
// Save the envelope activation | |
$checkbox_value = isset( $_POST['custom_envelope_active'] ) ? 'yes' : 'no'; | |
$product->update_meta_data( 'custom_envelope_active', $checkbox_value ); | |
// Save the custom envelope price | |
$envelope_price = isset( $_POST['custom_envelope_price'] ) ? $_POST['custom_envelope_price'] : '50'; // Default to €50 if no price set | |
$product->update_meta_data( 'custom_envelope_price', sanitize_text_field( $envelope_price ) ); | |
$product->save(); | |
} | |
add_action( 'woocommerce_process_product_meta', 'custom_save_envelope_checkbox_and_price' ); | |
/** | |
* Displaying the Envelope Checkbox on Product Page | |
*/ | |
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_checkbox_to_product', 10 ); | |
function add_custom_checkbox_to_product() { | |
global $product; | |
// Check if the product has the 'custom_envelope_active' meta | |
$custom_envelope_active = get_post_meta( $product->get_id(), 'custom_envelope_active', true ); | |
// If 'custom_envelope_active' is set to 'yes', display the checkbox | |
if ( $custom_envelope_active === 'yes' ) { | |
$custom_envelope_price = get_post_meta( $product->get_id(), 'custom_envelope_price', true ); | |
$custom_envelope_price = !empty( $custom_envelope_price ) ? $custom_envelope_price : '50'; // Default to 50 if no price set | |
echo '<div class="custom-checkbox"> | |
<input type="checkbox" id="add_custom_option" name="add_custom_option" value="1" /> | |
<label for="add_custom_option">Add Envelope Price for €' . esc_html( $custom_envelope_price ) . '</label> | |
</div>'; | |
// Store the price in a hidden input for use in the cart | |
echo '<input type="hidden" id="custom_envelope_price" name="custom_envelope_price" value="' . esc_attr( $custom_envelope_price ) . '" />'; | |
} | |
} | |
/** | |
* Adding Envelope Price to Cart Item Data | |
*/ | |
add_filter( 'woocommerce_add_cart_item_data', 'add_envelope_price_to_cart', 10, 2 ); | |
function add_envelope_price_to_cart( $cart_item_data, $product_id ) { | |
if ( isset( $_POST['add_custom_option'] ) && $_POST['add_custom_option'] == '1' ) { | |
$envelope_price = isset( $_POST['custom_envelope_price'] ) ? $_POST['custom_envelope_price'] : 50; // Default €50 if not set | |
$cart_item_data['custom_envelope_price'] = $envelope_price; | |
} | |
return $cart_item_data; | |
} | |
/** | |
* Add Envelope Price to Cart Total (Subtotal) | |
*/ | |
add_action( 'woocommerce_cart_calculate_fees', 'add_envelope_price_to_cart_total', 20, 1 ); | |
function add_envelope_price_to_cart_total( $cart ) { | |
$envelope_total = 0; | |
// Loop through each cart item to check for the envelope option | |
foreach ( $cart->get_cart() as $cart_item ) { | |
if ( isset( $cart_item['custom_envelope_price'] ) ) { | |
$envelope_total += $cart_item['custom_envelope_price']; // Add the envelope price to total | |
} | |
} | |
// If there's an envelope price, add it as a fee | |
if ( $envelope_total > 0 ) { | |
$cart->add_fee( 'Envelope Price', $envelope_total, true, '' ); // true indicates taxable | |
} | |
} | |
/** | |
* Display Envelope Price in the Cart Item | |
*/ | |
add_filter( 'woocommerce_get_item_data', 'display_envelope_price_in_cart_item', 10, 2 ); | |
function display_envelope_price_in_cart_item( $item_data, $cart_item ) { | |
if ( isset( $cart_item['custom_envelope_price'] ) ) { | |
$item_data[] = array( | |
'name' => 'Envelope Price', | |
'value' => '€' . esc_html( $cart_item['custom_envelope_price'] ) | |
); | |
} | |
return $item_data; | |
} | |
/** | |
* Update Elementor Mini Cart Subtotal Dynamically Using AJAX | |
*/ | |
add_filter( 'woocommerce_add_to_cart_fragments', 'update_elementor_mini_cart_after_add_to_cart' ); | |
function update_elementor_mini_cart_after_add_to_cart( $fragments ) { | |
ob_start(); | |
// Get the total envelope price | |
$envelope_total = 0; | |
foreach ( WC()->cart->get_cart() as $cart_item ) { | |
if ( isset( $cart_item['custom_envelope_price'] ) ) { | |
$envelope_total += $cart_item['custom_envelope_price']; // Add the envelope price | |
} | |
} | |
// Get the cart subtotal | |
$cart_subtotal = WC()->cart->get_subtotal(); | |
// Add the envelope price to the subtotal | |
$updated_subtotal = $cart_subtotal + $envelope_total; | |
?> | |
<div class="elementor-menu-cart__subtotal"> | |
<strong>Subtotal:</strong> | |
<span class="woocommerce-Price-amount amount"> | |
<bdi> | |
<span class="woocommerce-Price-currencySymbol">€</span> | |
<?php echo number_format( $updated_subtotal, 2 ); ?> | |
</bdi> | |
</span> | |
</div> | |
<?php | |
// Return the updated fragment | |
$fragments['div.elementor-menu-cart__subtotal'] = ob_get_clean(); | |
return $fragments; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment