【问题标题】:How to update the clone fields which is inside the repeater field in ACF?如何更新 ACF 中转发器字段内的克隆字段?
【发布时间】:2019-10-19 16:39:05
【问题描述】:

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

我需要以编程方式创建发票帖子类型。 “发票”的所有字段都已创建,但“项目”字段除外。如何将数据添加到包含“销售项目”克隆字段的“项目”收割机字段?

销售项目和发票字段组。

[
    {
        "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;

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    您实际上不需要调用add_row 并按顺序添加每个转发器条目。您可以一次性推送整个项目数组。您需要执行以下操作以确保满足以下条件并且代码适合您。

    1. 您的转发器字段具有正确的字段键(与 field_5bfed0e63a470 类似的内容)。
    2. 转发器组中每个重复元素的 slug 是正确的(例如转发器组有一个现场销售项目,它的 slug 格式为sales_item_item)。

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

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

    这对你来说应该没问题。祝你好运!!!

    【讨论】:

    • 完美运行。有什么原因,为什么 update_field 使用字段键而不使用字段名称?
    • 在将新值保存到帖子时(不存在值时)应使用该字段的键。这有助于 ACF 在值和字段设置之间创建正确的“引用”。如果这已经为您解决了问题,请标记答案。
    猜你喜欢
    • 2014-07-07
    • 2012-10-09
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多