【问题标题】:ACF form post woocommerce productACF 表单 post woocommerce 产品
【发布时间】:2018-06-29 10:03:40
【问题描述】:

我正在开发一家多供应商商店,我希望供应商从模板页面发布产品。我正在使用高级自定义字段来实现此功能。 到目前为止,我所做的是创建页面模板并设法显示表单,但我在使用产品验证字段时遇到了一些问题。我可能需要创建一些功能?

    <?php
/**
 * The template for displaying all pages.
 * Template Name: Add Product Vendor
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package Bober
 */

?>
<?php

acf_form_head();

get_header();

?>
<div id="content">

    <?php
    global $woocommerce;

    acf_form(array(
        $post_title = 'field_5a60022b402e2',
        $post_categ = 'field_5a60028e402e3',
        $post_descrip = 'field_5a600384402e4',
        $post_img = 'field_5a6005e1402e7',
        $post_price = 'field_5a61207ce5226',
        /*'post_title' =>true,
        'post_content' => true,*/
        'uploader' => 'basic',

        'post_id' => 'new_post',
        'fields' => array($post_title, $post_descrip, $post_categ,$post_price, $post_img),
        'new_post' => array(
            'post_status' => 'draft',
            'post_type' =>'product',

        ),
        /*'fields' => array('field_5a60022b402e2', 'field_5a60028e402e3', 'field_5a600384402e4', 'field_5a6005e1402e7'),*/


        'submit_value' => 'Add product'
    ));

    ?>

</div>

<?php get_footer(); ?>

此时我可以使用下面基于 ACF 表单文档的代码来指定产品的标题和内容。

'post_title' =>true,
'post_content' => true,

如何将标题值、描述值、价格值和图片分配给产品帖子?我不需要找人提供代码来替换,我想知道怎么做,给我一些想法,或者在哪里阅读如何做到这一点。

提前谢谢你!

【问题讨论】:

  • 建议:在您的acf _form() 函数数组中,您应该将所有= 替换为=&gt;。现在应该会更好。
  • 这些是可变的,我定义了它们以便人们可以理解这些字段。

标签: php wordpress woocommerce advanced-custom-fields acfpro


【解决方案1】:

首先,深入了解acf_form() 的工作原理。

在 woocommerce 中添加产品的 acf_form() 代码如下所示:

$options = array(
    'post_id' => 'new_post',
    'post_title' => true,
    'post_content' => true,
    'new_post'      => array(
      'post_type'       => 'product',
      'post_status'     => 'draft'
     ),
    'field_groups' => [$field_group_id],
    'form' => true,
     'return' => '%post_url%', // so they could be redirected to the product page in a draft state - i imagine?!
    'html_submit_button'    => '<input type="submit" value="%s" />',
    'updated_message' => 'Product Created',
    'submit_value' => 'Submit Product'
);

acf_form($options);

关于产品的特色图片,创建一个图片字段并使用下面的代码将该字段与特色图片同步。您可以通过查看acf/update_value 的 ACF API 参考来了解更多信息

add_filter('acf/update_value/name=featured_image', function ($value, $post_id, $field) {
if ($value != '') {
    update_post_meta($post_id, '_thumbnail_id', $value);
} else {
    delete_post_thumbnail($post_id);
}

return $value;
}, 10, 3);

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2021-10-25
    • 2021-02-28
    • 2015-07-27
    • 2020-07-06
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多