2013-04-01 17 views
27

W Woocommerce próbuję znaleźć sposób na zastosowanie 10% zniżki do całego zamówienia klienta, jeśli waga w koszyku przekracza 100 funtów. Jestem po części sposobem na osiągnięcie tego. W następnym kroku szukam sposobu, aby programowo zastosować kod kuponu poprzez akcję/hook poprzez functions.php.Jak programowo zastosować kupon w Woocommerce?

Wygląda na to, że mogę to zrobić za pomocą funkcji woocommerce_ajax_apply_coupon (http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html), ale nie jestem pewien, jak z niego korzystać.

Do tej pory zmodyfikowałem cart.php, aby uzyskać całkowitą wagę wszystkich produktów w koszyku, utworzyłem kupon, który stosuje zniżkę (jeśli wprowadzono ręcznie) i dodałem kod do funkcji .php, aby sprawdzić wagę i wyświetlić komunikat dla użytkownika.

EDYCJA: Usunięto częściowy kod, skompletowany kod zawarty w poniższym rozwiązaniu.


Dzięki za poradę Freney. Oto działa efekt końcowy, który z powodzeniem stosuje kupon rabatowy, gdy warunek jest spełniony, a także usuwa go, gdy nie jest już spełniony:

/* Mod: 10% Discount for weight greater than 100 lbs 
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart: 
     global $total_weight; 
     $total_weight = $woocommerce->cart->cart_contents_weight; 
*/ 
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100'); 
function discount_when_weight_greater_than_100() { 
    global $woocommerce; 
    global $total_weight; 
    if($total_weight > 100) { 
     $coupon_code = '999'; 
     if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
     echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has been Applied!</strong> Your total order weight is <strong>' . $total_weight . '</strong> lbs.</div>'; 
    } 
} 

/* Mod: Remove 10% Discount for weight less than or equal to 100 lbs */ 
add_action('woocommerce_before_cart_table', 'remove_coupon_if_weight_100_or_less'); 
function remove_coupon_if_weight_100_or_less() { 
    global $woocommerce; 
    global $total_weight; 
    if($total_weight <= 100) { 
     $coupon_code = '999'; 
     $woocommerce->cart->get_applied_coupons(); 
     if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
     $woocommerce->cart->calculate_totals(); 
    } 
} 

Odpowiedz

27

Najpierw utwórz kupon rabatowy (poprzez http://docs.woothemes.com/document/create-a-coupon-programatically/):

$coupon_code = 'UNIQUECODE'; // Code - perhaps generate this from the user ID + the order ID 
$amount = '10'; // Amount 
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product 

$coupon = array(
    'post_title' => $coupon_code, 
    'post_content' => '', 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_type'  => 'shop_coupon' 
);  

$new_coupon_id = wp_insert_post($coupon); 

// Add meta 
update_post_meta($new_coupon_id, 'discount_type', $discount_type); 
update_post_meta($new_coupon_id, 'coupon_amount', $amount); 
update_post_meta($new_coupon_id, 'individual_use', 'no'); 
update_post_meta($new_coupon_id, 'product_ids', ''); 
update_post_meta($new_coupon_id, 'exclude_product_ids', ''); 
update_post_meta($new_coupon_id, 'usage_limit', '1'); 
update_post_meta($new_coupon_id, 'expiry_date', ''); 
update_post_meta($new_coupon_id, 'apply_before_tax', 'yes'); 
update_post_meta($new_coupon_id, 'free_shipping', 'no'); 

Następnie zastosować ten kupon do zamówienia:

if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) 
    $woocommerce->show_messages(); 

Ta ostatnia funkcja zwraca wartość BOOL: TRUE jeśli zniżka był udany, FAŁSZ, jeśli nie dla każdego dnia e z wielu powodów.

+0

Ponieważ mam już kupon na zapleczu: "999" Dodałem tylko kod, który podałeś, aby zastosować kupon. Jednakże, gdy przejdę na stronę koszyka woocommerce, otrzymuję teraz błąd: wywołanie funkcji member add_discount() na obiekcie innym niż obiekt w [path to ...] functions.php na linii 65 [instrukcja if z add_discount]. Zaktualizowałem kod w moim oryginalnym wpisie, aby pokazać, jak to wygląda. – msargenttrue

+1

Spróbuj dodać 'global $ woocommerce;' wraz z linią '$ total_weight'. – Freney

+0

Dziękuję, to zadziałało! Więc teraz jestem w stanie z powodzeniem zastosować zniżkę, ale teraz zauważam inny problem ... Jeśli użytkownik usunie niektóre przedmioty z koszyka, a wózek wróci do 100 funtów lub mniej, zniżka nadal istnieje. Szukam sposobu na usunięcie zniżki, jeśli koszyk nie spełnia już warunku, więc użytkownicy nie będą mogli wykorzystać tego kuponu. Spodziewam się, że istnieje funkcja remove_discount, która działa w przeciwny sposób do add_discount, ale nie udało mi się znaleźć jej w dokumentach API Woocommerce: http://docs.woothemes.com/wc-apidocs/ – msargenttrue

6

Użyłem tego rozwiązania, ale zawiera ono błąd, o którym pisał OP. Jeśli użytkownik pominie podgląd koszyka i przejdzie od razu do formularza kasowego, nie stosuje kuponu. Oto moje rozwiązanie.

// Independence day 2013 coupon auto add 
// Add coupon when user views cart before checkout (shipping calculation page). 
add_action('woocommerce_before_cart_table', 'add_independence_day_2013_coupon_automatically'); 

// Add coupon when user views checkout page (would not be added otherwise, unless user views cart first). 
add_action('woocommerce_before_checkout_form', 'add_independence_day_2013_coupon_automatically'); 

// Check if php function exists. If it doesn't, create it. 
if (!function_exists('add_independence_day_2013_coupon_automatically')) { 

    function add_independence_day_2013_coupon_automatically() { 

     global $woocommerce; 
     $coupon_code = 'independencedaysale'; 
     $bc_coupon_start_date = '2013-06-30 17:00:00'; 
     $bc_coupon_end_date = '2013-07-08 06:59:59'; 

     // Only apply coupon between 12:00am on 7/1/2013 and 11:59pm on 7/7/2013 PST. 
     if ((time() >= strtotime($bc_coupon_start_date)) && 
      (time() <= strtotime($bc_coupon_end_date))) { 

      // If coupon has been already been added remove it. 
      if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) { 

       if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 

        $woocommerce->show_messages(); 

       } 

      } 

      // Add coupon 
      if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 

       $woocommerce->show_messages(); 

      } else { 

       $woocommerce->clear_messages(); 
       $woocommerce->add_message('Independence day sale coupon (10%) automatically applied'); 
       $woocommerce->show_messages(); 

      } 

      // Manually recalculate totals. If you do not do this, a refresh is required before user will see updated totals when discount is removed. 
      $woocommerce->cart->calculate_totals(); 

     } else { 

      // Coupon is no longer valid, based on date. Remove it. 
      if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) { 

       if ($woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 

        $woocommerce->show_messages(); 

       } 

       // Manually recalculate totals. If you do not do this, a refresh is required before user will see updated totals when discount is removed. 
       $woocommerce->cart->calculate_totals(); 

      } 

     } 

    } 

} 
Powiązane problemy