【问题标题】:Replace Upsells with products from a specific product category in Woocommerce 3用 Woocommerce 3 中特定产品类别的产品替换追加销售
【发布时间】:2018-10-27 11:53:55
【问题描述】:

我有以下情况 - 到目前为止,我一直将 Upsell 产品自定义设置为 1 到 1,因为无法在管理员中选择产品类别。

现在在对不再可用的产品进行大清洗后,网站上的许多产品页面有 1、2 或 3 个而不是默认数量的 4 个 Upsell 产品,这破坏了设计。

有没有办法用特定产品类别的产品替换这些追加销售?

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    要使用特定产品类别的产品替换 woocommerce 追加销售,请使用以下代码(您将在其中定义产品类别 slug)

    add_filter( 'woocommerce_product_get_upsell_ids', 'custom_upsell_ids', 20, 2 );
    function custom_upsell_ids( $upsell_ids, $product ){
        // HERE define your product categories slugs in the array
        $product_categories = array( 't-shirts' ); // <=====  <=====  <=====  <=====  <=====
    
        // Return the custom query
        return wc_get_products( array(
            'status'    => 'publish', // published products
            'limit'     => 4, // 4 products
            'category'  => $product_categories, // Product categories
            'orderby'   => 'rand', // Random order
            'exclude'   => array( $product->get_id() ), // Exclude current product
            'return'    => 'ids', // Query returns only IDs
        ) );
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    查询设置为以随机顺序显示特定产品类别的4个产品(不包括当前产品)。

    官方文档: The wc_get_products() and WC_Product_Query

    【讨论】:

    • 您好,感谢您的支持。问题是 - 有数百种产品并且还在增加。他们每个人都需要不同的来源(产品类别)来购买 Upsell 产品。分别编辑每个产品时,我需要能够手动设置。
    • 嘿 Loic,不幸的是,此代码在添加到 functions.php 后不起作用。类别的 url 是西里尔文,所以是 slug。将它们添加到代码后,它会破坏站点。我尝试使用逗号分隔的类别 ID,但完全没有成功,在发布或编辑产品时我没有选择类别的选项。
    • @StephanG 此代码在 Woocommerce 3+ 中完美运行,并且产品类别需要是 slug(而不是名称),因为使用西里尔文的名称肯定会破坏网站。
    • Urls,分别是 slugs 本身也是西里尔文。 Woocommerce 是 3.4.6
    • @StephanG 这是基于官方 woocommerce 工作代码...如果问题来自西里尔字符,您应该将产品类别 slug 转换为非西里尔字符以避免出现问题。这就是我能说和抱歉的全部内容,但我不会在我的代码中管理外来字符问题。所以你应该向 Woocommerce 报告这个问题,如果它来自 WC_Product_query 中关于产品类别的西里尔字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多