сначала измените текст в зависимости от количества. Я только что добавил условие if в вашу функцию, чтобы проверить количество для этого конкретного продукта в корзине и соответственно изменить текст следующим образом:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
function woo_add_to_cart_again() {
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $values['quantity'] == 1 && get_the_ID() == $_product->id ) {
return __( 'Buy again & Get 5% OFF', 'woocommerce' );
}
if ( $values['quantity'] == 2 && get_the_ID() == $_product->id ) {
return __( 'Buy 3 and get 10% OFF', 'woocommerce' );
}
if ( $values['quantity'] > 2 && get_the_ID() == $_product->id ) {
return __( 'Add Again', 'woocommerce' );
}
}
return __( 'Add to cart', 'woocommerce' );
}
Примечание: , если вы тоже хотите изменить текст на странице архива, вы можете использовать woocommerce_product_add_to_cart_text
ловушку и добавить его к приведенному выше коду следующим образом:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_add_to_cart_again' );
Затем нам нужно вычислитьцена для тех продуктов, которая соответствует скидке и добавляет скидку на основе цены продукта.
Мы собираемся использовать woocommerce_cart_calculate_fees
, чтобы вставить эту скидку в нашу корзину следующим образом:
add_action( 'woocommerce_cart_calculate_fees', 'add_discount' );
function add_discount( WC_Cart $cart ) {
$discounts = []; //Store the disocunts in array
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $values['quantity'] == 2 ) {
// Calculate the discount amount
$product_price = ( $_product->get_sale_price() ) ? $_product->get_sale_price() : $_product->get_regular_price();
$price = $product_price * 2;
array_push( $discounts, $price * 0.05 ); //Push the discount
}
if ( $values['quantity'] > 2 ) {
// Calculate the discount amount
$product_price = ( $_product->get_sale_price() ) ? $_product->get_sale_price() : $_product->get_regular_price();
$price = $product_price * $values['quantity'];
$discount = $price * 0.1;
array_push( $discounts, $price * 0.1 );//Push the discount
}
}
$cart->add_fee( 'Your Disocunt', -array_sum( $discounts ) );
}
Обновлено:
Чтобы изменить текст add_to_cart при использовании Ajax, на него уже есть ответ. @LoicTheAztec по этой ссылке и с небольшими изменениями вы можетедостигните своей целевой цели следующим образом:
add_action( 'wp_footer', 'change_add_to_cart_jquery' );
function change_add_to_cart_jquery() {
if ( is_shop() || is_product_category() || is_product_tag() ) : ?>
<script type="text/javascript">
(function ($) {
$('a.add_to_cart_button').click(function () {
$this = $(this);
if (typeof $($this).attr('data-qty') == 'undefined') {
$($this).text('Buy again & Get 5% OFF');
$($this).attr('data-qty', 1);
return;
}
if ($($this).attr('data-qty') == 1) {
$($this).text('Buy 3 and get 10% OFF');
$($this).attr('data-qty', 2);
return
}
$($this).text('Add Again');
});
})(jQuery);
</script>
<?php
endif;
}
, конечно, код выше был проверен.