Как обновить поля клонирования, которые находятся внутри поля повторителя в ACF? - PullRequest
4 голосов
/ 04 июня 2019

У меня есть группа полей с именами «Товар» и «Счет-фактура».В группе полей счета-фактуры есть поле «Предметы», являющееся полем повторителя, поле «Предметы» содержит группу полей «Товар» в качестве поля клона.

Мне нужно программно создать тип записи «Счет-фактура».Создается все поле «Счет-фактура», кроме поля «Товары».Как добавить данные в поле жнеца «Предметы», содержащее поле клона «Товар»?

Группа полей «Товар» и «Счет-фактура».

[
    {
        "key": "group_5c4033565a799",
        "title": "Invoice",
        "fields": [
            {
                "key": "field_5c44150d123de",
                "label": "Type",
                "name": "ims_invoice_type",
                "type": "select",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "choices": {
                    "sales": "Sales",
                    "purchases": "Purchases"
                },
                "default_value": [
                    "purchases"
                ],
                "allow_null": 0,
                "multiple": 0,
                "ui": 0,
                "return_format": "value",
                "ajax": 0,
                "placeholder": ""
            },
            {
                "key": "field_5c4033c40bce3",
                "label": "Invoice Number",
                "name": "ims_invoice_invoice_number",
                "type": "text",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": "",
                "maxlength": "",
                "placeholder": "",
                "prepend": "",
                "append": ""
            },
            {
                "key": "field_5c4e943170485",
                "label": "Customer",
                "name": "ims_invoice_customer",
                "type": "relationship",
                "instructions": "",
                "required": 1,
                "conditional_logic": [
                    [
                        {
                            "field": "field_5c44150d123de",
                            "operator": "==",
                            "value": "sales"
                        }
                    ]
                ],
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "post_type": [
                    "ims_customer"
                ],
                "taxonomy": "",
                "filters": [
                    "search",
                    "post_type",
                    "taxonomy"
                ],
                "elements": "",
                "min": 1,
                "max": 1,
                "return_format": "object"
            },
            {
                "key": "field_5c4033620bce1",
                "label": "Vendor",
                "name": "ims_invoice_vendor",
                "type": "relationship",
                "instructions": "",
                "required": 1,
                "conditional_logic": [
                    [
                        {
                            "field": "field_5c44150d123de",
                            "operator": "==",
                            "value": "purchases"
                        }
                    ]
                ],
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "post_type": [
                    "ims_vendor"
                ],
                "taxonomy": "",
                "filters": [
                    "search",
                    "post_type",
                    "taxonomy"
                ],
                "elements": "",
                "min": 1,
                "max": 1,
                "return_format": "object"
            },
            {
                "key": "field_5c40339b0bce2",
                "label": "Date",
                "name": "ims_invoice_date",
                "type": "date_picker",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "display_format": "F j, Y",
                "return_format": "d\/m\/Y",
                "first_day": 0
            },
            {
                "key": "field_5c4034030cff9",
                "label": "Items",
                "name": "ims_invoice_sales_items",
                "type": "repeater",
                "instructions": "",
                "required": 1,
                "conditional_logic": [
                    [
                        {
                            "field": "field_5c44150d123de",
                            "operator": "==",
                            "value": "sales"
                        }
                    ]
                ],
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "collapsed": "",
                "min": 0,
                "max": 0,
                "layout": "table",
                "button_label": "Add Product",
                "sub_fields": [
                    {
                        "key": "field_5c443f4dd87de",
                        "label": "Item",
                        "name": "ims_invoice_sales_items",
                        "type": "clone",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "clone": [
                            "group_5c443e86ad330"
                        ],
                        "display": "seamless",
                        "layout": "block",
                        "prefix_label": 0,
                        "prefix_name": 1
                    }
                ]
            },
            {
                "key": "field_5c78f715c5efe",
                "label": "Items",
                "name": "ims_invoice_purchases_items",
                "type": "repeater",
                "instructions": "",
                "required": 1,
                "conditional_logic": [
                    [
                        {
                            "field": "field_5c44150d123de",
                            "operator": "==",
                            "value": "purchases"
                        }
                    ]
                ],
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "collapsed": "",
                "min": 0,
                "max": 0,
                "layout": "table",
                "button_label": "Add Product",
                "sub_fields": [
                    {
                        "key": "field_5c78f730c5eff",
                        "label": "Items",
                        "name": "ims_invoice_purchases_items",
                        "type": "clone",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "clone": [
                            "group_5c78e0e32443c"
                        ],
                        "display": "seamless",
                        "layout": "block",
                        "prefix_label": 0,
                        "prefix_name": 1
                    }
                ]
            },
            {
                "key": "field_5c41b2ff17b09",
                "label": "Sub Total",
                "name": "ims_invoice_sub_total",
                "type": "read_only",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            },
            {
                "key": "field_5c41b31a17b0b",
                "label": "Discount",
                "name": "ims_invoice_discount",
                "type": "number",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": "0.0",
                "placeholder": "",
                "prepend": "",
                "append": "",
                "min": 0,
                "max": "",
                "step": ""
            },
            {
                "key": "field_5c41b32617b0c",
                "label": "V.A.T",
                "name": "ims_invoice_vat",
                "type": "number",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": "0.0",
                "placeholder": "",
                "prepend": "",
                "append": "",
                "min": 0,
                "max": "",
                "step": ""
            },
            {
                "key": "field_5c41b33317b0d",
                "label": "Total",
                "name": "ims_invoice_total",
                "type": "read_only",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            },
            {
                "key": "field_5c41b33b17b0e",
                "label": "Paid",
                "name": "ims_invoice_paid",
                "type": "number",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": "0.0",
                "placeholder": "",
                "prepend": "",
                "append": "",
                "min": 0,
                "max": "",
                "step": ""
            },
            {
                "key": "field_5c41b34517b0f",
                "label": "Due",
                "name": "ims_invoice_due",
                "type": "read_only",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            }
        ],
        "location": [
            [
                {
                    "param": "post_type",
                    "operator": "==",
                    "value": "ims_invoice"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "left",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": 1,
        "description": ""
    },
    {
        "key": "group_5c443e86ad330",
        "title": "Sales Item",
        "fields": [
            {
                "key": "field_5c443eab22fe1",
                "label": "Item",
                "name": "sales_item_item",
                "type": "post_object",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "post_type": [
                    "ims_product"
                ],
                "taxonomy": "",
                "allow_null": 0,
                "multiple": 0,
                "return_format": "object",
                "ui": 1
            },
            {
                "key": "field_5c779ac395834",
                "label": "SKU",
                "name": "sales_item_sku",
                "type": "read_only",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            },
            {
                "key": "field_5c443eba22fe2",
                "label": "Quantity",
                "name": "sales_item_quantity",
                "type": "number",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": 1,
                "placeholder": "",
                "prepend": "",
                "append": "",
                "min": "",
                "max": "",
                "step": ""
            },
            {
                "key": "field_5c443ec622fe3",
                "label": "Unit Price",
                "name": "sales_item_unit_price",
                "type": "read_only",
                "instructions": "",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            },
            {
                "key": "field_5c443edf22fe4",
                "label": "Price",
                "name": "sales_item_price",
                "type": "read_only",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            },
            {
                "key": "field_5c443efd22fe5",
                "label": "Discount",
                "name": "sales_item_discount",
                "type": "number",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": "0.0",
                "placeholder": "",
                "prepend": "",
                "append": "",
                "min": "",
                "max": "",
                "step": ""
            },
            {
                "key": "field_5c443f0a22fe6",
                "label": "Amount",
                "name": "sales_item_amount",
                "type": "read_only",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "copy_to_clipboard": 0,
                "display_type": "text"
            }
        ],
        "location": [
            [
                {
                    "param": "post_type",
                    "operator": "==",
                    "value": "post"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "top",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": 0,
        "description": ""
    }
]

Вот код, который я написал до сих пор.

$invoice_key = 'ims_invoice_sales_items_';
$customer_id = $_POST['customer_id'];
$ims_products = $_POST['ims_product'];
$basket_discount = $_POST['basket_discount'];
$basket_vat = $_POST['basket_vat'];
$paid = $_POST['paid'];

$sub_total = 0.0;       
foreach( $ims_products as $ims_product ) {

    $id = $ims_product['id'];
    $quantity = intval( $ims_product['quantity'] );
    $unit_price = floatval( get_field( 'ims_product_mrp', $id ) );
    $discount = floatval( $ims_product['discount'] );
    $price = $quantity * $unit_price;
    $amount = $price - ( $discount * $price ) / 100.0;
    $sub_total += $amount;

    $items [] = array(
        $invoice_key . 'sales_item_item'       => $id,
        $invoice_key . 'sales_item_sku'        => get_field( 'ims_product_sku', $id ),
        $invoice_key . 'sales_item_quantity'   => $quantity,
        $invoice_key . 'sales_item_unit_price' => $unit_price,
        $invoice_key . 'sales_item_price'      => $price,
        $invoice_key . 'sales_item_discount'   => $discount,
        $invoice_key . 'sales_item_amount'     => $amount               
    );
}

// var_dump( $items );

// Calculate grand total after discount and vat.
$grand_total = $sub_total - ( $basket_discount * $sub_total ) / 100.0;
$grand_total = $grand_total + ( $basket_vat * $grand_total ) / 100.0;

// Calculate due.
$due = $grand_total - $paid;

$meta_inputs = array(
    'ims_invoice_type'           => 'sales',
    'ims_invoice_invoice_number' => uniqid(),
    'ims_invoice_customer'       => $customer_id,
    'ims_invoice_date'           => current_time('d/m/Y', false ),
    'ims_invoice_sub_total'      => $sub_total,
    'ims_invoice_discount'       => $basket_discount,
    'ims_invoice_vat'            => $basket_vat,
    'ims_invoice_total'          => $grand_total,
    'ims_invoice_paid'           => $paid,
    'ims_invoice_due'            => $due
);

$invoice_args = array(
    'post_type' => 'ims_invoice',
    'post_status' => 'publish',
    'meta_input' => $meta_inputs
);

// var_dump( $invoice_args );

$post_id = wp_insert_post( $invoice_args, true );

var_dump( "<h1>Post Id = ${post_id}</h1>");


foreach( $items as $item ) {

    var_dump( $item );
    add_row( 'ims_invoice_sales_items', $item, $post_id );
}

die;

1 Ответ

4 голосов
/ 07 июня 2019

На самом деле вам не нужно звонить add_row и добавлять каждую запись повторителя по порядку.Вы можете сдвинуть весь массив элементов за один раз.Вам нужно будет выполнить следующие действия, чтобы убедиться, что указанные ниже условия выполнены и код будет работать для вас.

  1. У вас есть правильный ключ поля для вашего поля повторителя (Что-то вроде эффекта field_5bfed0e63a470).
  2. Пуля каждого повторяющегося элемента в группе повторителей является правильной (например, в группе повторителей есть полевая торговая позиция, и для нее в формате sales_item_item).

После того, как вы убедились в этом, вам просто нужно создать массив новых элементов, которые вы уже создали с помощью переменной $items, и вызвать следующий код:

$items_inserted = update_field('field_5bfed0e63a470', $items, $post_id);

Все должно работатьхорошо для вас с этим.Удачи !!!

...