| |
| <?php
|
|
|
| add_filter('woocommerce_product_single_add_to_cart_text', 'rename_add_to_cart_button', 10, 1);
|
| function rename_add_to_cart_button($button_text)
|
| {
|
| return __('Call to Order', 'woocommerce');
|
| }
|
|
|
|
|
| add_filter('woocommerce_product_add_to_cart_url', 'redirect_to_call_for_order', 10, 2);
|
| function redirect_to_call_for_order($add_to_cart_url, $product)
|
| {
|
| $phone_number = isset($_POST['phone_number']) ? sanitize_text_field($_POST['phone_number']) : '';
|
| return !empty($phone_number) ? 'tel:' . $phone_number : $add_to_cart_url;
|
| }
|
|
|
|
|
| add_filter('woocommerce_product_data_tabs', 'add_call_to_order_product_tab', 0);
|
| function add_call_to_order_product_tab($tabs)
|
| {
|
| $tabs['call_to_order'] = array(
|
| 'label' => __('Call to Order', 'woocommerce'),
|
| 'target' => 'call_to_order_product_options',
|
| 'class' => array('show_if_simple'),
|
| );
|
| return $tabs;
|
| }
|
|
|
|
|
| add_action('woocommerce_product_data_panels', 'call_to_order_product_tab_content');
|
| function call_to_order_product_tab_content()
|
| {
|
| global $post;
|
|
|
| echo '<div id="call_to_order_product_options" class="panel woocommerce_options_panel">';
|
| echo '<div class="options_group">';
|
|
|
|
|
| woocommerce_wp_checkbox(
|
| array(
|
| 'id' => 'call_to_order_checkbox',
|
| 'label' => __('Enable Call to Order for this product', 'woocommerce'),
|
| 'description' => '',
|
| 'value' => get_post_meta($post->ID, 'call_to_order_checkbox', true)
|
| )
|
| );
|
|
|
| echo '</div>';
|
|
|
| echo '<div class="options_group">';
|
|
|
|
|
| woocommerce_wp_text_input(
|
| array(
|
| 'id' => 'call_to_order_phone_number',
|
| 'label' => __('Phone Number', 'woocommerce'),
|
| 'desc_tip' => true,
|
| 'description' => __('Enter the phone number for call to order.', 'woocommerce'),
|
| 'value' => get_post_meta($post->ID, 'call_to_order_phone_number', true),
|
| 'type' => 'tel',
|
| )
|
| );
|
|
|
| echo '</div>';
|
| echo '</div>';
|
| }
|
|
|
|
|
| add_action('woocommerce_process_product_meta', 'save_call_to_order_meta');
|
| function save_call_to_order_meta($post_id)
|
| {
|
| $checkbox = isset($_POST['call_to_order_checkbox']) ? 'yes' : 'no';
|
| $phone_number = isset($_POST['call_to_order_phone_number']) ? sanitize_text_field($_POST['call_to_order_phone_number']) : '';
|
|
|
| update_post_meta($post_id, 'call_to_order_checkbox', $checkbox);
|
| update_post_meta($post_id, 'call_to_order_phone_number', $phone_number);
|
| }
|
|
|
|
|
|
|
| add_filter('woocommerce_is_purchasable', 'limit_call_to_order_button', 10, 2);
|
| function limit_call_to_order_button($purchasable, $product)
|
| {
|
| $checkbox = get_post_meta($product->get_id(), 'call_to_order_checkbox', true);
|
| if ($checkbox == 'yes') {
|
| $purchasable = false;
|
| }
|
| return $purchasable;
|
| }
|
|
|
|
|
| add_filter('woocommerce_loop_add_to_cart_link', 'display_call_to_order_or_add_to_cart_button', 10, 2);
|
| add_filter('woocommerce_product_single_add_to_cart_text', 'display_call_to_order_or_add_to_cart_button', 10, 2);
|
| function display_call_to_order_or_add_to_cart_button($button_text, $product)
|
| {
|
| $checkbox = get_post_meta($product->get_id(), 'call_to_order_checkbox', true);
|
|
|
| if ($checkbox == 'yes') {
|
| $phone_number = isset($_POST['phone_number']) ? sanitize_text_field($_POST['phone_number']) : '';
|
| if (!empty($phone_number)) {
|
| $button_text = '<a class="button" href="tel:' . $phone_number . '">' . __('Call to Order', 'woocommerce') . '</a>';
|
| } else {
|
| $button_text = __('Call to Order', 'woocommerce');
|
| }
|
| } else {
|
|
|
| $button_text = get_option('woocommerce_add_to_cart_text', __('Add to Cart', 'woocommerce'));
|
| }
|
|
|
| return $button_text;
|
| }
|
|
|
|
|
| add_action('admin_head', 'call_to_order_custom_styles');
|
| function call_to_order_custom_styles()
|
| {
|
| echo '<style>
|
| #call_to_order_phone_number {
|
| width: 50%;
|
| }
|
| </style>';
|
| }
|
|
|
|
|
| add_action('woocommerce_after_shop_loop_item', 'display_call_to_order_button_on_archive', 10, 2);
|
| add_action('woocommerce_single_product_summary', 'display_call_to_order_button_on_single_product', 30);
|
| function display_call_to_order_button_on_archive()
|
| {
|
| global $product;
|
| $checkbox = get_post_meta($product->get_id(), 'call_to_order_checkbox', true);
|
|
|
| if ($checkbox == 'yes') {
|
| $phone_number = get_post_meta($product->get_id(), 'call_to_order_phone_number', true);
|
|
|
| if (!empty($phone_number)) {
|
| echo '<a class="button" href="tel:' . $phone_number . '">' . __('Call to Order', 'woocommerce') . '</a>';
|
| } else {
|
| echo __('Call to Order', 'woocommerce');
|
| }
|
| }
|
| }
|
|
|
| function display_call_to_order_button_on_single_product()
|
| {
|
| global $product;
|
| $checkbox = get_post_meta($product->get_id(), 'call_to_order_checkbox', true);
|
|
|
| if ($checkbox == 'yes') {
|
| $phone_number = get_post_meta($product->get_id(), 'call_to_order_phone_number', true);
|
|
|
| if (!empty($phone_number)) {
|
| echo '<a class="button" href="tel:' . $phone_number . '">' . __('Call to Order', 'woocommerce') . '</a>';
|
| } else {
|
| echo __('Call to Order', 'woocommerce');
|
| }
|
| }
|
| }
|
| |
| |
Comments