2014-04-08 6 views
6

Sto creando un plugin in WooCommerce e ho riscontrato un piccolo problema con l'aggiunta di sconti personalizzati alla pagina CART/CHECKOUT.Come creare sconti personalizzati per il carrello in woocommerce

Come posso applicare lo sconto personalizzato al carrello senza creare coupon? Dire che voglio dare uno sconto di 5 dollari sulla pagina del carrello. Come lo posso fare?

Di seguito è riportato il mio codice dal file di estensione in cui è stato utilizzato un coupon per applicare lo sconto, ma desidero aggiungere un altro sconto personalizzato senza l'uso di coupon.

Azione Hook nel file plugin:

add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action')); 

e la sua funzione nel file plugin è:

public function cart_order_total_action(){ 
    if (is_user_logged_in()){ 
     global $woocommerce; 
     global $current_user; 
     global $wpdb; 
     $u_id = $current_user->ID; 
     $table_name = $wpdb->prefix."woocommerce_customer_reward_ms"; 
     $thetable2 = $wpdb->prefix . "woocommerce_customer_reward_cart_ms"; 
     $table_name3 = $wpdb->prefix."woocommerce_customer_reward_points_log_ms"; 
     $data  = $wpdb->get_row("SELECT * from $table_name where id=$u_id"); 
     $data2  = $wpdb->get_row("SELECT * from $thetable2"); 
     /* Order Id goes here */ 
     $orders=array();//order ids 
     $args = array(
      'numberposts'  => -1, 
      'meta_key'  => '_customer_user', 
      'meta_value'  => $current_user->ID, 
      'post_type'  => 'shop_order', 
      'post_status'  => 'publish', 
      'tax_query'=>array(
        array(
         'taxonomy' =>'shop_order_status', 
         'field'  => 'slug', 
         'terms'  =>'on-hold' 
         ) 
      ) 
     ); 
     $posts=get_posts($args); 
     $orders=wp_list_pluck($posts, 'ID'); 
     $order = $orders[0]; 
     /* Order Id ends here */ 
     if($data){ 
      $user_points = $data->points; 
      $points_set = $data2->woo_pts_set; 
      $coupon_code = 'wooreward_discount'; 
      if($user_points>=$points_set){ 
       // this following Code is optional and can be removed......as there is no need of if statement here 
       if ($woocommerce->cart->has_discount($coupon_code)) { 
        /*$woocommerce->add_error(__('Coupon Code Already Applied.!!','woocommerce'));*/ 
        return false; 
       }else{ 
        $woocommerce->cart->add_discount(sanitize_text_field($coupon_code)); 
        $woocommerce->add_message(__('Taxco925 Reward Discount Applied.!!','woocommerce')); 
       } 
      }else{ 
       $woocommerce->add_error(__('Not Enough Taxco925 Points.!!','woocommerce')); 
      } 
     }else{ 
      $woocommerce->add_error(__('You have have not earned any Taxco925 Points yet.!!','woocommerce')); 
     } 
    } 
} 

Come si può vedere questa linea $woocommerce->cart->add_discount(sanitize_text_field($coupon_code)); aggiunge il mio sconto al carrello. Ma usa il coupon sullo sfondo per farlo. C'è un modo per aggiungere uno sconto personalizzato senza l'uso di coupon.

risposta

3

Forse troppo tardi, ma se qualcuno ha un'altra soluzione dimmi.

ho usare qualcosa come:

$discount = floatval(10); 
if(!empty($discount) || $discount != 0){ 
    $discount *= -1; // convert positive to negative fees 
    $woocommerce->cart->add_fee('discount', $discount, true, ''); // add negative fees 
} 

Se si utilizza il pagamento standard PayPal, hai un errore perché non è possibile inviare un prodotto con prezzi negativo.

Hai solo bisogno di modificare il plugin woocommerce paypal per passare questo valore.

Ma l'altro metodo di pagamento è ok!

migliori saluti,

+0

Sulla base di wooCommerce API, non si dovrebbe usare add_fee con importi negativi. Link: http://woocommerce.wp-a2z.org/oik_api/wc_cartadd_fee/ –

6
add_action('woocommerce_checkout_order_processed','custom_disount',10,1); 
function custom_disount($order_id){ 
    $order = wc_get_order($order_id); 
    $order_items = $order->get_items(); 
    foreach ($order_items as $order_item_key => $order_item) { 
     $product = new WC_Product((int) $order_item['product_id']); 
     $quantity = (int) $order_item['qty']; 
     $discount=($product->regular_price*30)/100; //30% disount. 
     wc_update_order_item_meta($order_item_key,'_line_total',($product->regular_price*$quantity)-($discount*$quantity)); 
    } 
} 
5

È possibile aggiungere di sconto per ogni singolo prodotto nel carrello utilizzando il gancio "woocommerce_get_discounted_price". Per esempio .:

function filter_woocommerce_get_discounted_price($price, $values, $instance) { 
//$price represents the current product price without discount 
//$values represents the product object 
//$instance represent the cart object 
$discount = 300; // add custom discount rule , This is just an example 
return ($price - $discount); 
}; 
add_filter('woocommerce_get_discounted_price','filter_woocommerce_get_discounted_price', 10, 3);