如何更新ACF中转发器字段内的克隆字段?

时间:2019-06-04 07:08:22

标签: wordpress advanced-custom-fields

我有一个名为“销售项目”和“发票”的字段组。发票字段组有一个“项目”字段,它是一个转发器字段,“项目”字段包含“销售项目”字段组作为克隆字段。

我需要以编程方式创建发票过帐类型。除“项目”字段外,所有“发票”字段均已创建。如何将数据添加到包含“销售商品”克隆字段的“商品”收割者字段中?

“销售项目和发票”字段组。

[
    {
        "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 个答案:

答案 0 :(得分:4)

您实际上不需要呼叫add_row并按顺序添加每个转发器条目。您可以一次性推送整个项目数组。您需要做以下事情,以确保满足以下条件,并且代码将为您工作。

  1. 您的中继器字段具有正确的字段密钥(对field_5bfed0e63a470起作用)。
  2. 转发器组中每个重复元素的子弹是正确的(例如,转发器组中有一个现场销售商品,并且该子弹的格式为sales_item_item)。

一旦确定,您只需要创建一个新项目集的数组即可,就像已经使用$items变量创建的那样,并调用以下代码:

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

与此相关的东西应该可以正常工作。祝你好运!