Answer the question
In order to leave comments, you need to log in
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).
Answer the question
In order to leave comments, you need to log in
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
/**
* Вычисляем суммы по товару, т.к. для предзаказанного товара цена должна быть регулярная
*
* @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,
);
}
..... // подготовка данных
$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 questionAsk a Question
731 491 924 answers to any question