wooCommerce与ACF商店/目录页面上的自定义相关产品

发布于 2025-02-10 05:54:17 字数 1559 浏览 1 评论 0原文

我有一家WooCommerce商店,希望能够在商店/目录页面上显示每种产品的自定义相关产品。

我的设置
使用高级自定义字段Pro
我创建了一个具有关系字段的字段组,称为:

每种产品的product_Realations我通过product_Realations字段选项选择了后端的“ product_relations”字段选项。

然后,我创建了一个可以在商店/目录页面上可见的短代码。

我确实看到只能输出我想要的输出。

当前输出
我只看到最新产品的关系>>请参阅屏幕截图

Current output

,这是我到目前为止的代码:

function yl_product_relations_shortcode() {
    global $product;

    $product_relations = get_field('product_relations');

    if ($product_relations) {

        $output .= '<ul class="uk-thumbnav uk-margin-bottom">';

        foreach ($product_relations as $product_relation) {
            $title  = get_the_title($product_relation);
            $image  = get_the_post_thumbnail_url($product_relation);
            $url    = get_the_permalink($product_relation);

            $output .= '<li><a href="' . $url . '" uk-tooltip="' . $title . '"><img src="' . $image . '" class="uk-border-rounded" width="40"></a></li>';
        }

        $output .= '</ul>';

    }

    return $output;

}

add_shortcode('yl_product_relations','yl_product_relations_shortcode');

没人? 我使用WordPress Cusomizer,我正在使用短码来收集TE数据。

[yl_product_relations]

当我尝试添加product_id时,

$id = $product->get_id();
$product_relations = get_field('product_relations', $id);

我会遇到错误。可能是因为我正在使用定制器,因此在产品循环本身中没有真正编码。

I have a WooCommerce shop and want to be able to display custom related products, for each product, on the shop/catalog page.

My setup

Using Advanced Custom Fields Pro

I have created a field group with a RELATION field called: product_relations

For each product I have selected several 'custom related products' via the product_relations field option on the product page in the backend.

I then created a shortcode to be visible on the shop/catalog page.

I do see output only not the output I would like.

The current output

I only see the relations of the LATEST PRODUCT >> See screenshot

Current output

Here's my code I have so far:

function yl_product_relations_shortcode() {
    global $product;

    $product_relations = get_field('product_relations');

    if ($product_relations) {

        $output .= '<ul class="uk-thumbnav uk-margin-bottom">';

        foreach ($product_relations as $product_relation) {
            $title  = get_the_title($product_relation);
            $image  = get_the_post_thumbnail_url($product_relation);
            $url    = get_the_permalink($product_relation);

            $output .= '<li><a href="' . $url . '" uk-tooltip="' . $title . '"><img src="' . $image . '" class="uk-border-rounded" width="40"></a></li>';
        }

        $output .= '</ul>';

    }

    return $output;

}

add_shortcode('yl_product_relations','yl_product_relations_shortcode');

Nobody?
I use the WordPress Cusomizer and I'm using a shortcode to collect te data.

[yl_product_relations]

When I try to add the product_id like this

$id = $product->get_id();
$product_relations = get_field('product_relations', $id);

I get an error. Probably because I'm using the customizer and therefor not really coding within the product loop itself..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文