I
I
Ivan Destroev2019-06-05 12:07:24
WooCommerce
Ivan Destroev, 2019-06-05 12:07:24

How to add additional prices to product pages with a button like "buy in 1 click"?

Hello everyone, there is a plugin from the author Artur Abramovich "Purchase in 1 click" is most suitable, but I need to add the ability to add prices for products and a pop-up button (as in the plugin) with conditions (out of stock / price not specified (optional)) .
Example:
Product A costs a basic 100 rubles, you can also buy it on pre-order at a cost of 60 rubles (the catch is that if a person is ready to wait for the product, he will pay at the pre-order price, and if he cannot wait, then immediately).

screenshot example
5cf78607dc4cc083169345.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2019-06-05
@pton

we have the same organized, but they refused the plugin, they wrote themselves
You need to notify the buyer that he will be waiting, and if she agrees to place an order at the right price

Here is the price correction function
/**
 * Вычисляем суммы по товару, т.к. для предзаказанного товара цена должна быть регулярная
 *
 * @param WC_Product $product
 * @param int        $ordered_qty
 *
 * @return array
 */
function hqo_calculate_totals( $product, $ordered_qty ) {
  $stock = $product->get_stock_quantity();
  $rprice = $product->get_regular_price();
  $sprice = $product->get_sale_price();

  $sprice = $sprice > 0 ? $sprice : $rprice;

  if ( $stock <= 0 ) {
    $total = $ordered_qty * $rprice;
  } elseif ( $ordered_qty > $stock ) {
    $total = $sprice * $stock + $rprice * ($ordered_qty - $stock);
  } else {
    $total = $sprice * $ordered_qty;
  }

  return array(
    'subtotal' => $total,
    'total'    => $total,
  );
}

Called before the product is added to the order
We have so
.....  // подготовка данных

  $qorder = wc_create_order();

        // массив $params заполняется ранее данными которые приходят ajax-ом
  if ( 0 === $params['type'] ) { /* simple */
    $qorder->add_product( wc_get_product( $params['id'] ), $params['qty'] );

  } elseif ( 1 === $params['type'] ) { /* variable */
    $variations_array = array();

    $var_product = new WC_Product_Variation( $params['var_id'] );
    $variations_array = hqo_calculate_totals( $var_product, $params['qty'] );
    $variations_array['variation'] = array(
      $params['attr_name'] => $params['attr_value'],
    );

    $qorder->add_product( $var_product, $params['qty'], $variations_array );
  }
        ......  // добавление свойств заказа

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question