【问题标题】:woocommerce - trying to return class for out of stock itemwoocommerce - 尝试为缺货商品返回课程
【发布时间】:2012-09-02 18:41:05
【问题描述】:

我在使用 WooCommerce 时遇到问题,试图在已售罄的商品上退回特定类别的“售罄”商品。我正在使用一个返回 div 而不是经典下拉列表的插件。所以可以选择[S][M][L][XL]等。看看我的意思-http://www.shultzilla.com/product/tees/greater-than/-

所以我有一个要添加此类的 div。我会设计它以使您无法单击它,并将其设计为即使产品售罄也无法单击该项目。

这是我想要做的,但它似乎根本没有返回任何东西。甚至没有错误:

public function is_in_stock() {
        if ( $this->managing_stock() ) :
            if ( ! $this->backorders_allowed() ) :
                if ( $this->get_total_stock() <  1 ) :
                    return false;
                else :
                    if ( $this->stock_status == 'instock' ) return true;
                    return false;
                endif;
            else :
                return true;
            endif;
        endif;
        if ( $this->stock_status == 'instock' ) return true;
        return false;
    }

function is_sold_out () {
         if ($product->is_in_stock()) {
          $soldOutClass = 'in-stock';
         } else {
          $soldOutClass = 'sold-out';
         }  
    }

public function get_output($placeholder = true, $placeholder_src = 'default') {
        global $woocommerce;


        $out = '<div class="select-option swatch-wrapper '.$soldOutStatus .'" data-value="' . $this->term_slug . '" ' . ($this->selected ? 'data-default="true"' : '') . '>';

        if ($this->type == 'photo' || $this->type == 'image') {

            $out .= '<a href="#" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" title="' . $this->term_label . '">';
            $out .= '<img src="' . $this->thumbnail_src . '" alt="Thumbnail" class="wp-post-image swatch-photo' . $this->meta_key() . '" width="' . $this->width . '" height="' . $this->height . '"/>';
            $out .= '</a>';
        } elseif ($this->type == 'color') {
            $out .= '<a href="#" style="text-indent:-9999px;width:' . $this->width . 'px;height:' . $this->height . 'px;background-color:' . $this->color . ';" title="' . $this->term_label . '">' . $this->term_label  . '</a>';
        } elseif ($placeholder) {
            if ($placeholder_src == 'default') {
                $src = $woocommerce->plugin_url() . '/assets/images/placeholder.png';
            } else {
                $src = $placeholder_src;
            }

            $out .= '<a href="#" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" title="' . $this->term_label . '">';
            $out .= '<img src="' . $src . '" alt="Thumbnail" class="wp-post-image swatch-photo' . $this->meta_key() . '" width="' . $this->width . '" height="' . $this->height . '"/>';
            $out .= '</a>';
        } else {
            return '';
        }

        $out .= '</div>';

        return $out;
    }'

如您所见,我正在尝试的方法不起作用。我不确定我做错了什么。

【问题讨论】:

  • @jvdberg,你是如何修复格式的?我无法让它工作。

标签: php wordpress woocommerce


【解决方案1】:

在你的模板 php 中试试这个函数:

function is_out_of_stock() {
    global $post;
    $post_id = $post->ID;
    $stock_status = get_post_meta($post_id, '_stock_status',true) == 'outofstock';
}

然后您可以执行以下操作:

<img class="<?php echo is_out_of_stock()? 'outofstock':''; ?>" src="..."></img>

【讨论】:

  • 嘿,马克。感谢您的回复。我会解决这个问题,看看我们能做些什么。感谢您的意见!
【解决方案2】:

在您的主题目录中创建一个名为 /woocommerce 的文件夹,然后在其中创建另一个名为 /loop 的文件夹,最后将插件中的 add-to-cart.php 文件放入其中,将第 17 行修改为: ..class="out-of-stock mycustomclasshere"&gt;&lt;?php echo apply_filters('out_of_stock_add_to_cart_text',__( 'Out of Stock','woocommerce'));?&gt;..

mycustomclasshere 显然是您自己的 CSS 样式输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2021-08-31
    • 2019-09-04
    • 2023-03-30
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多