我有一个问题,如何从类别产品中删除购物车。如果我将它应用于特定的id或一般的所有内容,它的工作正常,但我无法为类别执行此操作。以下是我对此所做的代码。
此外,我正在努力将相同的模式应用于相关文章部分,因此任何帮助将不胜感激。
谢谢。
#!/usr/bin/env python
import gtk
print (gtk.Clipboard().wait_for_contents('text/html')).data
答案 0 :(得分:5)
要使其与产品类别一起使用,您可以使用WordPress条件函数has_term()
:
//function for deleting ....
function remove_product_description_add_cart_button(){
global $product;
// Set HERE your category ID, slug or name (or an array)
$category = 'categoryslug';
//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $category, 'product_cat', $product->id ) )
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
代码进入活动子主题(或主题)的function.php文件。或者也可以在任何插件php文件中。
答案 1 :(得分:1)
您尝试过这样的事情:
function remove_product_description_add_cart_button(){
global $product;
$termsOfProduct = wp_get_post_terms( $product->id, 'product_cat' );
if (in_array("CatToFind", $termsOfProduct)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
Woocommerce中的分类产品只是简单的术语。 Wp_get_post_terms允许您查找与post(产品ID)相关联的任何类别。
参考:https://codex.wordpress.org/Function_Reference/wp_get_post_terms
答案 2 :(得分:0)
您可以尝试这样的事情
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function linkUp() {
$("#IssueList25").sortable({
connectWith: ".connectedSortable",
receive: function(event, ui) {
var droppedIssueId = ui.item.attr("id");
var destListId = this.id;
var sourceListId = ui.sender[0].id;
var destList = $('#IssueList25').sortable('toArray').toString();
$.ajax({
async: true,
type: "GET",
url: "Lists.aspx",
data:"event=receive&sourceListId = " + sourceListId + "&destListId = " + destListId + "&droppedIssueId=" + droppedIssueId + "&destList=" + destList,
dataType: "text"
});
}
});});
</script>
参考:https://www.themelocation.com/how-to-remove-add-to-cart-button-from-a-certain-category-woocommerce/
答案 3 :(得分:0)
使用此代码
/*
Plugin Name: Remove 'Add to cart' conditionally
Plugin URI: https://www.damiencarbery.com/2020/03/remove-add-to-cart-conditionally/
Description: Conditionally remove the 'Add to cart' button in WooCommerce.
Author: Damien Carbery
Version: 0.2
*/
class IsPurchasableConditionalFiltering {
// A reference to an instance of this class.
private static $instance;
// Store whether 'Add to cart' button should be displayed.
private $purchasable;
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new IsPurchasableConditionalFiltering;
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->purchasable = array();
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
add_filter( 'woocommerce_is_purchasable', array( $this, 'is_purchasable_conditionals' ), 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', array( $this, 'variation_is_purchasable_conditionals' ), 10, 2 );
// Remove variations dropdown and 'Add to cart' button for variable products.
add_action( 'woocommerce_before_single_product_summary', array( $this, 'before_single_product_summary' ) );
}
public function is_purchasable_conditionals( $whether_purchasable, $product ) {
// Return cached result.
if ( array_key_exists( $product->get_id(), $this->purchasable ) ) {
return $this->purchasable[ $product->get_id() ];
}
// Only do our conditional checks if WooCommerce deems the item to be purchasable.
if ( $whether_purchasable ) {
$result = true; // Default to allowing purchase.
// Check our specific conditions - some examples.
/* // Product over a certain price.
if ( $product->get_price() > 2 ) {
$result = false;
}*/
// Check if product in a certain categores.
if ( has_term( array( 'hoodies', 'accessories' ), 'product_cat', $product->get_id() ) ) {
$result = false;
}
$this->purchasable[ $product->get_id() ] = $result;
}
else {
// Store that this item cannot be purchased.
$this->purchasable[ $product->get_id() ] = false;
}
return $this->purchasable[ $product->get_id() ];
}
public function variation_is_purchasable_conditionals( $whether_purchasable, $product ) {
return $whether_purchasable;
}
public function before_single_product_summary() {
$product_id = get_the_ID();
if ( array_key_exists( $product_id, $this->purchasable ) && !$this->purchasable[ $product_id ] ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
}
}
}
$IsPurchasableConditionalFiltering = new IsPurchasableConditionalFiltering;
参考:https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/