dpuwov1487 2018-04-03 19:02
浏览 4
已采纳

too long

I would like to have a shortcode that I can add to a WooCommerce product page. The shortcode would simply check the product stock and say "In stock" if it has any. And then "Out of stock" if the product (or any of the variations) have inventory.

This shortcode should work for both Simple products and Variable products. With Simple products, it would just check the stock quantity. For Variable products, it would check the stock quantity of ALL of the variations (since the variable product inventory is managed at the inventory level). If ANY of the variations have stock, then it would still return as "In stock".

It would only say "Out of stock" if ALL of the variations of a particular product were 0.

I plan to use this shortcode within a product page template I am building.

  • 写回答

1条回答 默认 最新

  • dongying2112 2018-04-03 19:59
    关注

    For now I've created this by:

    /* Create stock checker of overall product */
    add_shortcode( 'fs-product-stock-status', 'fs_product_stock_status_shortcode' );
    function fs_product_stock_status_shortcode( $atts ) {
        // begin output buffering
        ob_start();
    
        $stockstatus = get_post_meta( get_the_ID(), '_stock_status', true );
    
        if ($stockstatus == 'outofstock') {
            echo '<p class="stock out-of-stock">Out of stock</p>';
        }
        elseif ($stockstatus == 'instock') {
            echo '<p class="stock in-stock">In stock</p>';
        }
    
        // end output buffering, grab the buffer contents, and empty the buffer
        return ob_get_clean();
    }
    

    But I'm open to other solutions that may be better. This seems to be working though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?