簡體   English   中英

在Woocommerce 3中用特定產品類別的產品替換加售商品

[英]Replace Upsells with products from a specific product category in Woocommerce 3

我遇到以下情況-到目前為止,我始終會按1設置Upsell產品自定義設置,因為無法在管理員中選擇產品類別。

現在,在對不再使用的產品進行大范圍清洗之后,站點上的許多產品頁面上都有1、2或3,而不是默認數量的4個Upsell產品,這破壞了設計。

有沒有一種方法可以用特定產品類別的產品代替那些加價銷售?

任何幫助表示贊賞。

要用來自特定產品類別的產品替換woocommerce加售,請使用以下代碼(您將在其中定義您的產品類別塊)

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個產品(當前產品除外)。

官方文檔: wc_get_products()WC_Product_Query

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM