D
D
Darklez2015-08-05 10:55:53
JavaScript
Darklez, 2015-08-05 10:55:53

How to add the cost of each product in the Robokassa plugin?

It is necessary to add the names of all products in the order
. Here is the original part of the edited code:

public function generate_form($order_id){
    global $woocommerce;

    $order = new WC_Order( $order_id );

    if ($this->testmode == 'yes'){
      $action_adr = $this->testurl;
    }
    else{
      $action_adr = $this->liveurl;
    }

    $out_summ = number_format($order->order_total, 2, '.', '');

    $crc = $this->robokassa_merchant.':'.$out_summ.':'.$order_id.':'.$this->robokassa_key1;

    $args = array(
        // Merchant
        'MrchLogin' => $this->robokassa_merchant,
        'OutSum' => $out_summ,
        'InvId' => $order_id,
        'SignatureValue' => md5($crc),
        'Culture' => 'ru',
      );

Here's what I got:
public function generate_form($order_id){
    global $woocommerce;

    $order = new WC_Order( $order_id );

    $items = $order->get_items();

    foreach( $items as $item ) {
    $item_total = $item['line_subtotal'];
    $product_name = $item['name'];
    }
    
    if ($this->testmode == 'yes'){
      $action_adr = $this->testurl;
    }
    else{
      $action_adr = $this->liveurl;
    }

    $out_summ = number_format($order->order_total, 2, '.', '');
    $out_summ2 = number_format($item_total, 2, '.', '');

    $crc = $this->robokassa_merchant.':'.$out_summ.':'.$order_id.':'.$this->robokassa_key1;

    $args = array(
        // Merchant
        'MrchLogin' => $this->robokassa_merchant,
        'OutSum' => $out_summ,
        'OutSum2' => $out_summ2,
        'ProductName' => $product_name
        'InvId' => $order_id,
        'SignatureValue' => md5($crc),
        'Culture' => 'ru',
      );

Unfortunately, only the name of the last product and the cost of the last product are displayed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-08-05
@Darklez

Step 1. Get a good understanding of what this code does:

foreach( $items as $item ) {
    $item_total = $item['line_subtotal'];
    $product_name = $item['name'];
}

Step 2. Do it right.
UPD: Ok, I'll spell it out:
// Для каждого элемента массива $items
foreach( $items as $item ) {
    // Устанавливаем значение переменной $item_total равной полю line_subtotal текущего элемента
    $item_total = $item['line_subtotal'];

    // Устанавливаем значение переменной $product_name равной полю name текущего элемента
    $product_name = $item['name'];
}

Now it's clear why you have the values ​​of the last product in $item_total and $product_name ? UPD2: The last approach to the projectile:
$item_total = 0.0;
$product_names = [];

foreach ($items as $item)
{
  $item_total += (float)$item['line_subtotal'];
  $product_names[] = $item['name'];
}

$out_summ2 = number_format($item_total, 2, '.', '');

$args = [
  'MrchLogin' => $this->robokassa_merchant,
  'OutSum' => $out_summ,
  'OutSum2' => $out_summ2,
  'ProductName' => implode(', ', $product_names),
  'InvId' => $order_id,
  'SignatureValue' => md5($crc),
  'Culture' => 'ru',
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question