我在 woocommerce 中创建了一个名为 Booking 的自定义产品类型。
这是我的代码:
class WC_Product_Booking extends WC_Product{
/**
* __construct function.
*
* @access public
* @param mixed $product
*/
public function __construct( $product ) {
$this->product_type = 'booking';
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
add_action('woocommerce_booking_add_to_cart', array($this, 'add_to_cart'),30);
}
private function show_pricing_fields(){
?><script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.options_group.pricing' ).addClass( 'show_if_booking' ).show();
});
</script><?php
}
/**
* Display the add to cart button (same as for simple product)
*/
public function add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
}
目前唯一的问题是产品页面上的添加到购物车部分显示了 6 次,我不明白为什么。
我该如何解决这个问题?
谢谢
@Update … 解决方案 (因为 LoicTheAztec 让我走上正轨):
我已经找到了解决这个问题的方法,使用这段代码
if (! function_exists( 'woocommerce_booking_add_to_cart' ) ) {
/**
* Output the simple product add to cart area.
*
* @subpackage Product
*/
function booking_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
}
关键部分是这个 Action add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
将添加到购物车按钮放在正确的位置 - 要在您自己的自定义产品中使用它,请执行名为 woocommerce_YOURPRODUCTTYPE_add_to_cart
最佳答案
试图在测试服务器上测试您的代码,但您的预订产品未在后端显示。
您的问题可能来自这里:我认为您正在使用 public function add_to_cart() 一个现有的名称函数,您必须以不同的方式重命名它。
然后我重新访问了你的代码,based on this thread .
扩展 WC_Product_Simple 类可能比 WC_Product 类更好,因为您的产品使用简单的产品添加到购物车按钮模板:single-product/add-to-cart/simple.php。
有了这个自定义代码,我就不会像你一样面对多次添加到购物车部分。
— @Update1 —
But you can still keepWC_Productextended instead ofWC_Product_Simple. This way, as you requested in comment, your product type will be"simple_booking"as specified in 1st and 2nd functions. That does not mean that your product is simple (it's just a slug that you can change, changing at the same time in both places).So I have modified the code again and tested: It works…
这是我使用的代码(并且我已经测试过):
/**
* Register the custom product type after init
*/
function register_simple_booking_product_type() {
/**
* This should be in its own separate file.
*/
class WC_Product_Booking extends WC_Product{ // <= changed back to WC_product class
public function __construct( $product ) {
$this->product_type = 'simple_booking';
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
// As we extend simple product class, you may not need this anymore.
add_action('woocommerce_booking_add_to_cart', array($this, 'simple_booking_add_to_cart'),30);
}
}
}
add_action( 'init', 'register_simple_booking_product_type' );
// Registering the slug name (YOU can CHANGE IT)
function add_simple_booking_product( $types ){
// Key should be exactly the same as in the class product_type parameter
$types[ 'simple_booking' ] = __( 'Simple booking' );
return $types;
}
add_filter( 'product_type_selector', 'add_simple_booking_product' );
/**
* Show pricing fields for simple_booking product.
*/
function simple_booking_custom_js() {
if ( 'product' != get_post_type() ) :
return;
endif;
?><script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.options_group.pricing' ).addClass( 'show_if_simple_booking' ).show();
});
</script><?php
}
add_action( 'admin_footer', 'simple_booking_custom_js' );
// Not sure you will need that (because of the change of the extended class)
function simple_booking_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
在位于事件子主题 (或主题) 中的 function.php 文件上测试了此代码。
引用:Adding a custom WooCommerce product type
— @Update2 — The solution (find by the author of this question):
To puts the add-to-cart button once in the right place (to use it in your own custom product make an action called
woocommerce_YOURPRODUCTTYPE_add_to_cart), with this code:
if (! function_exists( 'woocommerce_booking_add_to_cart' ) ) {
/**
* Output the simple product add to cart area.
*
* @subpackage Product
*/
function booking_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
}
关于php - WooCommerce 自定义产品类型 - 多个添加到购物车部分问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432736/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search