| |
| <?php
|
|
|
| add_action('woocommerce_product_options_general_product_data', 'add_whatsapp_order_fields');
|
| function add_whatsapp_order_fields() {
|
| woocommerce_wp_checkbox(array(
|
| 'id' => 'allow_whatsapp_order',
|
| 'label' => __('Allow WhatsApp Order', 'text-domain'),
|
| 'description' => __('Check this box to enable WhatsApp ordering for this product.', 'text-domain'),
|
| ));
|
|
|
| woocommerce_wp_text_input(array(
|
| 'id' => '_whatsapp_number',
|
| 'label' => __('WhatsApp Number', 'text-domain'),
|
| 'placeholder' => __('Enter WhatsApp Number', 'text-domain'),
|
| 'desc_tip' => 'true',
|
| 'description' => __('Enter the WhatsApp number for this product. Include the country code without spaces or special characters (e.g., +123456789).', 'text-domain'),
|
| ));
|
| }
|
|
|
|
|
| add_action('woocommerce_process_product_meta', 'save_whatsapp_order_fields');
|
| function save_whatsapp_order_fields($post_id) {
|
| $allow_whatsapp_order = isset($_POST['allow_whatsapp_order']) ? 'yes' : 'no';
|
| update_post_meta($post_id, 'allow_whatsapp_order', $allow_whatsapp_order);
|
|
|
| $whatsapp_number = sanitize_text_field($_POST['_whatsapp_number']);
|
| update_post_meta($post_id, '_whatsapp_number', $whatsapp_number);
|
| }
|
|
|
|
|
| add_action('woocommerce_single_product_summary', 'display_whatsapp_order_button', 30);
|
| function display_whatsapp_order_button() {
|
| global $product;
|
|
|
|
|
| $allow_whatsapp_order = get_post_meta($product->get_id(), 'allow_whatsapp_order', true);
|
|
|
| if ($allow_whatsapp_order !== 'yes') {
|
|
|
| return;
|
| }
|
|
|
|
|
| $product_title = get_the_title();
|
| $product_price = $product->get_price();
|
| $stock_quantity = $product->get_stock_quantity();
|
| $product_link = get_permalink($product->get_id());
|
|
|
|
|
| $whatsapp_number = get_post_meta($product->get_id(), '_whatsapp_number', true);
|
| $whatsapp_number = !empty($whatsapp_number) ? preg_replace('/\D/', '', $whatsapp_number) : 'YOUR_DEFAULT_WHATSAPP_NUMBER';
|
|
|
|
|
| $whatsapp_message = urlencode(
|
| "I would like to place an order for product: $product_title\n" .
|
| "Price: $product_price\n" .
|
| "Current Stock Quantity: $stock_quantity\n" .
|
| "Product Link: $product_link"
|
| );
|
|
|
|
|
| echo '<style>
|
| a.whatsapp-button {
|
| display: inline-block;
|
| padding: 10px 15px;
|
| background-color: #25D366; /* WhatsApp green color */
|
| color: #fff;
|
| text-decoration: none;
|
| border-radius: 5px;
|
| font-size: 16px;
|
| }
|
|
|
| a.whatsapp-button i {
|
| margin-right: 5px; /* Adjust the spacing between icon and text */
|
| }
|
|
|
| a.whatsapp-button:hover {
|
| background-color: #128C7E; /* Darker shade for hover effect */
|
| }
|
| </style>';
|
|
|
|
|
| echo '<a href="https://api.whatsapp.com/send?phone=' . esc_attr($whatsapp_number) . '&text=' . $whatsapp_message . '" class="whatsapp-button" target="_blank" rel="noopener"><i class="fa fa-whatsapp"></i> WhatsApp Order</a>';
|
|
|
|
|
| remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
|
| }
|
| |
| |
Comments